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);
}
}