二、图像处理

待完善

一、图片缩放

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);
    }
}
相关推荐
老艾的AI世界8 小时前
新一代AI换脸更自然,DeepLiveCam下载介绍(可直播)
图像处理·人工智能·深度学习·神经网络·目标检测·机器学习·ai换脸·视频换脸·直播换脸·图片换脸
凤枭香13 小时前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
浮生如梦_1 天前
Halcon基于laws纹理特征的SVM分类
图像处理·人工智能·算法·支持向量机·计算机视觉·分类·视觉检测
凤枭香2 天前
python opencv灰度变换
图像处理·人工智能·python·opencv
道不可以打折扣2 天前
【神经科学学习笔记】基于分层嵌套谱分割(Nested Spectral Partition)模型分析大脑网络整合与分离的学习总结
图像处理·人工智能·笔记·学习·计算机视觉
AI原吾2 天前
探索 Python 图像处理的瑞士军刀:Pillow 库
图像处理·python·ai·pillow
tt5555555555552 天前
图像处理-20241101
图像处理·人工智能·笔记·学习·计算机视觉
i嗑盐の小F2 天前
【SPIE出版 | ISSN: 0277-786X,EI检索稳定!】2024年计算机视觉与图像处理国际学术会议 (CVIP 2024,11月15-17日)
大数据·图像处理·人工智能·深度学习·神经网络·机器学习·计算机视觉
DogDaoDao2 天前
深度学习常用开源数据集介绍【持续更新】
图像处理·人工智能·深度学习·ai·数据集
emperinter2 天前
WordCloudStudio:AI生成模版为您的文字云创意赋能 !
图像处理·人工智能·macos·ios·信息可视化·iphone