OpenCV Overview
As a powerful open source computer vision framework, OpenCV contains more than 500 algorithm implementations, and is still increasing. Its latest version has been updated to 3.2. Its SDK supports the development of Android and Java platforms, and can almost meet common image processing requirements. It should become the first image processing framework for Java and Android programmers. The configuration of using OpenCV in Java is very simple, and it can be said that it is almost zero configuration without any politeness.
1: Configuration
To configure the introduction of OpenCV-related jar packages, you must first download the self-extracting version of OpenCV. The download address is: http://opencv.org/opencv-3-2.html
Then pull to the bottom of the web page and download the Windows Self-decompression Development Package
After downloading, double-click to unzip the build path and find the following:
Double-click to open the Java folder.
There is a jar that is directly imported into the new project in Eclipse, and then copy the dll file in x64 into the Java JDK bin and jre/bin directories used in Eclipse. The environment is configured, it’s simple! The configured final project structure:
Two: Loading images and pixel operations
Read an image--, do it in one sentence
Mat src = Imgcodecs.imread(imageFilePath);if(src.empty()) return;
Convert Mat object to BufferedImage object
public BufferedImage conver2Image(Mat mat) { int width = mat.cols(); int height = mat.rows(); int dims = mat.channels(); int[] pixels = new int[width*height]; byte[] rgbdata = new byte[width*height*dims]; mat.get(0, 0, rgbdata); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); int index = 0; int r=0, g=0, b=0; for(int row=0; row<height; row++) { for(int col=0; col<width; col++) { if(dims == 3) { index = row*width*dims + col*dims; b = rgbdata[index]&0xff; g = rgbdata[index+1]&0xff; r = rgbdata[index+2]&0xff; pixels[row*width+col] = ((255&0xff)<<24) | ((r&0xff)<<16) | ((g&0xff)<<8) | b&0xff; } if(dims == 1) { index = row*width + col; b = rgbdata[index]&0xff; pixels[row*width+col] = ((255&0xff)<<24) | ((b&0xff)<<16) | ((b&0xff)<<8) | b&0xff; } } } setRGB( image, 0, 0, width, height, pixels); return image;}Convert BufferedImage object to Mat object
public Mat convert2Mat(BufferedImage image) { int width = image.getWidth(); int height = image.getHeight(); Mat src = new Mat(new Size(width, height), CvType.CV_8UC3); int[] pixels = new int[width*height]; byte[] rgbdata = new byte[width*height*3]; getRGB( image, 0, 0, width, height, pixels ); int index = 0, c=0; int r=0, g=0, b=0; for(int row=0; row<height; row++) { for(int col=0; col<width; col++) { index = row*width + col; c = pixels[index]; r = (c&0xff0000)>>16; g = (c&0xff00)>>8; b = c&0xff; index = row*width*3 + col*3; rgbdata[index] = (byte)b; rgbdata[index+1] = (byte)g; rgbdata[index+2] = (byte)r; } } src.put(0, 0, rgbdata); return src;}In particular, the order of the RGB channel of BufferedImage and Mat is different. On the contrary, the order of the three channels in the Mat object is BGR and RGB in the BufferedImage.
Read all pixels from Mat (where image is Mat type data)
int width = image.cols();int height = image.rows();int dims = image.channels();byte[] data = new byte[width*height*dims];image.get(0, 0, data);
Traversing pixel operations and saving changes
int index = 0;int r=0, g=0, b=0;for(int row=0; row<height; row++) { for(int col=0; col<width*dims; col+=dims) { index = row*width*dims + col; b = data[index]&0xff; g = data[index+1]&0xff; r = data[index+2]&0xff; r = 255 - r; g = 255 - g; b = 255 - b; data[index] = (byte)b; data[index+1] = (byte)g; data[index+2] = (byte)r; }}image.put(0, 0, data);Save Mat object as image file - can be done in one sentence
Imgcodecs.imwrite(filePath, src);
OpenCV code running and testing
Adjust the degree of light and darkness - reduce brightness
Adjust the degree of light and darkness - brightness increase
Gaussian blur
Sharpen
gradient
Grayscale
The complete Java code for the above effects is as follows:
package com.gloomyfish.opencvdemo;import org.opencv.core.Core;import org.opencv.core.CvType;import org.opencv.core.Mat;import org.opencv.core.Size;import org.opencv.imgproc.Imgproc;public class ImageFilters { /** - Inverse color processing- */ public Mat inverse(Mat image) { int width = image.cols(); int height = image.rows(); int dims = image.channels(); byte[] data = new byte[width*height*dims]; image.get(0, 0, data); int index = 0; int r=0, g=0, b=0; for(int row=0; row<height; row++) { for(int col=0; col<width*dims; col+=dims) { index = row*width*dims + col; b = data[index]&0xff; g = data[index+1]&0xff; r = data[index+2]&0xff; r = 255 - r; g = 255 - g; b = 255 - b; data[index] = (byte)b; data[index+1] = (byte)g; data[index+2] = (byte)r; } } image.put(0, 0, data); return image; } public Mat brightness(Mat image) { // Brightness increase Mat dst = new Mat(); Mat black = Mat.zeros(image.size(), image.type()); Core.addWeighted(image, 1.2, black, 0.5, 0, dst); return dst; } public Mat darkness(Mat image) { // Brightness decrease Mat dst = new Mat(); Mat black = Mat.zeros(image.size(), image.type()); Core.addWeighted(image, 0.5, black, 0.5, 0, dst); return dst; } public Mat gray(Mat image) { // Grayscale Mat gray = new Mat(); Imgproc.cvtColor(image, gray, Imgproc.COLOR_BGR2GRAY); return gray; } public Mat sharpen(Mat image) { // Sharpen Mat dst = new Mat(); float[] sharper = new float[]{0, -1, 0, -1, 5, -1, 0, -1, 0}; Mat operator = new Mat(3, 3, CvType.CV_32FC1); operator.put(0, 0, sharper); Imgproc.filter2D(image, dst, -1, operator); return dst; } public Mat blur(Mat image) { // Gaussian fuzzy Mat dst = new Mat(); Imgproc.GaussianBlur(image, dst, new Size(15, 15), 0); return dst; } public Mat gradient(Mat image) { // Gradient Mat grad_x = new Mat(); Mat grad_y = new Mat(); Mat abs_grad_x = new Mat(); Mat abs_grad_y = new Mat(); Imgproc.Sobel(image, grad_x, CvType.CV_32F, 1, 0); Imgproc.Sobel(image, grad_y, CvType.CV_32F, 0, 1); Core.convertScaleAbs(grad_x, abs_grad_x); Core.convertScaleAbs(grad_y, abs_grad_y); grad_x.release(); grad_y.release(); Mat gradxy = new Mat(); Core.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 10, gradxy); return gradxy; }}It can be said to be so simple. In addition, OpenCV For Java supports various image processing including morphological operations, binary image analysis, image feature detection and recognition, template matching, histogram-related functions, etc. Common machine learning algorithms and image analysis methods. It can be said to be one of the most powerful image processing SDK and development platforms. I will continue to explore and share!
Pay special attention
Before calling, be sure to add this sentence
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
The purpose is to load DLL support related to OpenCV API, which will not run correctly without it. The above code and function implementation is based on JDK8 64-bit and OpenCV version 3.2.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.