OpenCV for Java Environment setup and function demonstration

Source: Internet
Author: User

OPENCV Overview

OpenCV as a powerful open-source framework for computer vision, with more than 500 algorithms implemented and growing, the latest version has been updated to 3.2. Its SDK supports Android and Java platform development, for the common image processing needs almost can be satisfied, it should be the vast majority of Java and Android Programmer's first image processing framework. The configuration and simplicity of using OPENCV in Java can be said to be almost 0 configurations.

One: Configuration

To configure the introduction of the OPENCV related jar package, first download the self-extracting version of OPENCV:
Http://opencv.org/opencv-3-2.html
Then pull to the bottom of the page to download the Windows self-extracting development package

Download OK after double-click Unzip to find the build path, shown as follows:

Double-click Open Java folder,

There is a jar directly into the new project in Eclipse, then copy the DLL file inside the x64 into the Java JDK bin and Jre/bin directory used in Eclipse. The environment is well-equipped, simple! Configuration of the final project structure:

Two: Loading image and pixel operation

Read in an image--a sentence to be done

Mat src = Imgcodecs.imread(imageFilePath);ifreturn;

Convert a Mat object to a BufferedImage object

Public BufferedImage Conver2image (Mat mat) {intwidth = Mat.cols ();intHeight = mat.rows ();intdims = Mat.channels ();int[] pixels = newint[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;intR=0, g=0, b=0; for(introw=0; row for(intCol=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) << -) | ((r&0xFF) << -) | ((g&0xFF) <<8) | b&0xFF; }if(dims = =1) {Index= row*width+ col; b = rgbdata[Index]&0xFF; Pixels[row*width+col] = ((255&0xFF) << -) | ((b&0xFF) << -) | ((b&0xFF) <<8) | b&0xFF; }}} Setrgb (image,0,0, width, height, pixels);returnImage;}

Convert a BufferedImage object to a Mat object

 PublicMat Convert2mat (bufferedimage image) {intwidth = Image.getwidth ();intHeight = image.getheight (); Mat src =NewMat (NewSize (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;intR=0, g=0, b=0; for(introw=0; row for(intCol=0; col<width; col++) {Index= Row*width + col; c = pixels[Index]; R = (c&0xff0000) >> -; 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);returnSRC;}

In particular, the RGB channel sequence for BufferedImage and Mat is not the same, just the opposite, in the Mat object the three-channel order is BGR and RGB in BufferedImage.

Read all pixels from mat (where image is mat type data)

int width = image.cols();int height = image.rows();int dims = image.channels();bytenewbyte[width*height*dims];image.get(00, data);

Traversing pixel manipulation and saving changes

intindex =0;intR=0, g=0, b=0; for(int row=0; row0; 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-a word can be done

Imgcodecs.imwrite(filePath, src);           
OPENCV Code Run and test
    • Adjust light and dark levels-brightness reduction

    • Adjust the intensity-brightness boost

    • Gaussian Blur

    • Sharpening

    • Gradient

    • Grayscale

The full Java code for the above effects is as follows:

 PackageCom.gloomyfish.opencvdemo;ImportOrg.opencv.core.Core;ImportOrg.opencv.core.CvType;ImportOrg.opencv.core.Mat;ImportOrg.opencv.core.Size;ImportOrg.opencv.imgproc.Imgproc; Public  class imagefilters {    /**-Inverse color processing-*/     PublicMatInverse(Mat image) {intwidth = Image.cols ();intHeight = image.rows ();intdims = Image.channels ();byte[] data =New byte[Width*height*dims]; Image.get (0,0, data);intindex =0;intR=0, g=0, b=0; for(introw=0; row for(intCol=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);returnImage } PublicMatBrightness(Mat image) {//Brightness boostMat DST =NewMat ();        Mat black = Mat.zeros (Image.size (), Image.type ()); core.addweighted (Image,1.2, Black,0.5,0, DST);returnDst } PublicMatDarkness(Mat image) {//Brightness reductionMat DST =NewMat ();        Mat black = Mat.zeros (Image.size (), Image.type ()); core.addweighted (Image,0.5, Black,0.5,0, DST);returnDst } PublicMatGray(Mat image) {//GrayscaleMat Gray =NewMat (); Imgproc.cvtcolor (image, Gray, Imgproc.color_bgr2gray);returnGray } PublicMatSharpen(Mat image) {//SharpeningMat DST =NewMat ();float[] Sharper =New float[]{0, -1,0, -1,5, -1,0, -1,0}; Mat operator =NewMat (3,3, CVTYPE.CV_32FC1); Operator.put (0,0, sharper); Imgproc.filter2d (image, DST,-1, operator);returnDst } PublicMatBlur(Mat image) {//Gaussian BlurMat DST =NewMat (); Imgproc.gaussianblur (image, DST,NewSize ( the, the),0);returnDst } PublicMatGradient(Mat image) {//GradientMat grad_x =NewMat (); Mat grad_y =NewMat (); Mat abs_grad_x =NewMat (); Mat abs_grad_y =NewMat (); 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 =NewMat (); Core.addweighted (abs_grad_x,0.5, Abs_grad_y,0.5,Ten, GRADXY);returnGRADXY; }}

Can say simple to cry, in addition OpenCV for Java supports a variety of image processing including morphological operations, binary image analysis, image feature detection and recognition, template matching, histogram-related functions and so on. Common machine learning algorithms and image analysis methods. Can be said to be one of the most powerful image processing SDK and development platform, I continue to explore the sharing!

Special attention
Before calling, be sure to add this sentence

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

The purpose is to load OPENCV API-related DLL support, without which it will not run correctly. The above code and feature implementations are based on JDK8 64-bit and OPENCV 3.2 versions.

You are welcome to continue to pay attention to this blog!

OpenCV for Java Environment setup and function demonstration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.