二、图像处理

待完善

一、图片缩放

java 复制代码
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Size;

public class ImageResizer {

    public static void main(String[] args) {
        // 加载图片
        Mat image = opencv_imgcodecs.imread("E://app/images/222.jpg");
		// 创建Mat
        Mat resizedImage = new Mat();
        // 设置缩放大小
        Size size = new Size(100, 100);
        // 缩放图片, 参数分别是 原图片、新创建的mat、缩放大小
        opencv_imgproc.resize(image, resizedImage, size);
        // 写图片
        opencv_imgcodecs.imwrite("E://app/images/resized.jpg", resizedImage);

    }
}

二、图片旋转

java 复制代码
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Point2f;

public class ImageResizer {

    public static void main(String[] args) {
        // 加载图片
        Mat image = opencv_imgcodecs.imread("E://app/images/222.jpg");
        Mat rotatedImage = new Mat();

        double angle = 90.0;
        // 获取图片中心点
        Point2f center = new Point2f(image.cols() / 2, image.rows() / 2);
        // 三个参数: 中心点,旋转角度, 图片比例
        Mat rotationMatrix = opencv_imgproc.getRotationMatrix2D(center, angle, 1.0);
        // 旋转
        opencv_imgproc.warpAffine(image, rotatedImage, rotationMatrix, image.size());
	    // 写图片
        opencv_imgcodecs.imwrite("E://app/images/rotated3.jpg", rotatedImage);
    }
}

三、边缘检测

java 复制代码
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;

public class ImageResizer {

    public static void main(String[] args) {
        // 加载图片
        Mat image = opencv_imgcodecs.imread("E://app/images/222.jpg");
        Mat grayImage = new Mat();
        Mat cannyImage = new Mat();
		// 灰度设置
        opencv_imgproc.cvtColor(image, grayImage, opencv_imgproc.COLOR_BGR2GRAY);
        // 边缘检测
        opencv_imgproc.Canny(grayImage, cannyImage, 100, 200);

        opencv_imgcodecs.imwrite("E://app/images/edges.jpg", grayImage);
        opencv_imgcodecs.imwrite("E://app/images/edges33.jpg", cannyImage);
    }
}

四、图像腐蚀

java 复制代码
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Size;

public class ImageResizer {

    public static void main(String[] args) {
        // 加载图片
        Mat image = opencv_imgcodecs.imread("E://app/images/222.jpg");
        Mat grayImage = new Mat();
        // 腐蚀程度
        Mat element = opencv_imgproc.getStructuringElement(opencv_imgproc.MORPH_RECT, new Size(15, 15));
        // 腐蚀
        opencv_imgproc.erode(image, grayImage, element);
        opencv_imgcodecs.imwrite("E://app/images/eroded.jpg", grayImage);

    }
}

五、图像膨胀

java 复制代码
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Size;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageResizer {


    public static void main(String[] args) {
        try {
            // 加载图像
            BufferedImage img = ImageIO.read(new File("E://app/images/222.jpg"));
            Java2DFrameConverter java2DFrameConverter = new Java2DFrameConverter();
            Mat inputMat = new OpenCVFrameConverter.ToMat().convert(java2DFrameConverter.convert(img));
            // 定义核
            Mat kernel = opencv_imgproc.getStructuringElement(opencv_imgproc.MORPH_RECT, new Size(15, 15));
            // 膨胀操作
            Mat outputMat = new Mat();
            opencv_imgproc.dilate(inputMat, outputMat, kernel);
            // 保存图像
            Java2DFrameConverter java2DFrameConverterOutput = new Java2DFrameConverter();
            BufferedImage outputImg = java2DFrameConverterOutput
            					.convert(new OpenCVFrameConverter.ToMat().convert(outputMat));
            ImageIO.write(outputImg, "png", new File("E://app/images/output.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

六、裁剪图片

java 复制代码
public class ImageResizer {


    public static void main(String[] args) {

        // 读取图像
        Mat src = opencv_imgcodecs.imread("E://app/images/111.jpg");

        // 定义矩形区域
        Rect roi = new Rect(200, 100, 500, 500);
        Rect roi2 = new Rect(100, 100, 500, 500);
        // 裁剪图像
        Mat cropped = new Mat(src, roi);
        Mat cropped2 = new Mat(src, roi2);
        // 保存图像
        opencv_imgcodecs.imwrite("E://app/images/output444.png", cropped);
        opencv_imgcodecs.imwrite("E://app/images/output555.png", cropped2);
    }
}

七、翻转图像

java 复制代码
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.opencv_core.Mat;

public class ImageResizer {


    public static void main(String[] args) {

        // 读取图像
        Mat src = opencv_imgcodecs.imread("E://app/images/222.jpg");
        // 翻转图像
        Mat dst = new Mat();
        opencv_core.flip(src, dst, 1);

        opencv_imgcodecs.imwrite("E://app/images/output555.png", dst);
    }
}

八、添加文字

java 复制代码
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Point;
import org.bytedeco.opencv.opencv_core.Scalar;

import static org.bytedeco.opencv.global.opencv_imgproc.FONT_HERSHEY_DUPLEX;

public class ImageResizer {


    public static void main(String[] args) {

        // 读取图像
        Mat image1 = opencv_imgcodecs.imread("E://app/images/111.jpg");
        // 设置文字内容和位置
        String text = "Hello, World!";
        Point textOrg = new Point(50, 50);

        // 在图像上添加文字
        //img -- 显示文字所在图像.
        //text -- 待显示的文字.
        //org -- 文字在图像中的左下角 坐标.
        // fontFace -- 字体类型, 可选择字体:FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX,FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, orFONT_HERSHEY_SCRIPT_COMPLEX,以上所有类型都可以配合 FONT_HERSHEY_ITALIC使用,产生斜体效果。
        //fontScale -- 字体大小,该值和字体内置大小相乘得到字体大小
        //color -- 文本颜色(默认颜色存储通道为BGR)
        //thickness -- 写字的线的粗细,类似于0.38的笔尖和0.5的笔尖
        // lineType -- 线性.
        // boo
        opencv_imgproc.putText(image1, text, textOrg, FONT_HERSHEY_DUPLEX, 2.0, new Scalar(250), 1,8, false);
        // 保存带有文字的图像
        opencv_imgcodecs.imwrite("E://app/images/output.png", image1);

    }
}

九、添加水印

java 复制代码
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Size;

public class ImageResizer {


    public static void main(String[] args) {

        // 读取图像
        Mat image = opencv_imgcodecs.imread("E://app/images/111.jpg");
        // 读取水印图像
        Mat watermark = opencv_imgcodecs.imread("E://app/images/666.jpg");
        
        Size size = new Size(1000, 1000);
        // 两张图片大小必须一致
        Mat resizedImage = new Mat();
        Mat waterResizedImage = new Mat();
        opencv_imgproc.resize(image, resizedImage, size);
        opencv_imgproc.resize(watermark, waterResizedImage, size);

        // 将水印图像复制到原始图像上
        Mat image_with_watermark = new Mat();
        opencv_core.addWeighted(resizedImage, 0.7, waterResizedImage, 0.3, 0, image_with_watermark);
        // 保存带有文字的图像
        opencv_imgcodecs.imwrite("E://app/images/output.png", image_with_watermark);

    }
}

十、模糊

java 复制代码
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Size;


public class ImageResizer {
    
    public static void main(String[] args) {
        // 读取图像
        Mat image = opencv_imgcodecs.imread("E://app/images/222.jpg");
        Mat dst = new Mat();
        opencv_imgproc.GaussianBlur(image, dst, new Size(15, 15), 0, 0, opencv_core.BORDER_DEFAULT);
        // 保存带有文字的图像
        opencv_imgcodecs.imwrite("E://app/images/output.jpg", dst);
    }
}

十一、锐化

java 复制代码
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Size;


public class ImageResizer {

    public static void main(String[] args) {
        // 读取图像
        Mat image = opencv_imgcodecs.imread("E://app/images/222.jpg");
        Mat dst = new Mat();

        Mat sharpen = new Mat();

        opencv_imgproc.GaussianBlur(image, sharpen, new Size(0, 0), 3);
        opencv_core.addWeighted(image, 1.5, sharpen, -0.5, 0, dst);
        // 保存带有文字的图像
        opencv_imgcodecs.imwrite("E://app/images/output.jpg", dst);
    }
}

十二、颜色

java 复制代码
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;


public class ImageResizer {

    public static void main(String[] args) {
        // 读取图像
        Mat image = opencv_imgcodecs.imread("E://app/images/222.jpg");
        Mat dst = new Mat();

        opencv_imgproc.cvtColor(image, dst, opencv_imgproc.COLOR_RGB2XYZ);
        // 保存带有文字的图像
        opencv_imgcodecs.imwrite("E://app/images/output.jpg", dst);
    }
}
相关推荐
Schwertlilien18 小时前
图像处理-Ch1-数字图像基础
图像处理·人工智能·算法
游客52021 小时前
opencv中的常用的100个API
图像处理·人工智能·python·opencv·计算机视觉
我感觉。1 天前
【图像处理lec8】彩色图像处理
图像处理·彩色图像处理
gloomyfish2 天前
【开发实战】QT5+ 工业相机 + OpenCV工作流集成演示
图像处理·深度学习·qt·opencv·计算机视觉
是娜个二叉树!2 天前
图像处理基础 | 查看两张图像的亮度差异,Y通道相减
图像处理·opencv·计算机视觉
研一计算机小白一枚2 天前
图像处理中的图像配准方法
图像处理·人工智能·计算机视觉
游客5202 天前
opencv中的各种滤波器简介
图像处理·人工智能·python·opencv·计算机视觉
handsomestWei2 天前
ISP图像处理简介
图像处理
黑色叉腰丶大魔王2 天前
基于 MATLAB 的图像增强技术分享
图像处理·人工智能·计算机视觉
禁默3 天前
2024年图像处理、多媒体技术与机器学习
图像处理·人工智能·microsoft