Using Java to develop the component structure of the OPENCV3 program -3.OPENCV and the Java version of the data structure

Source: Internet
Author: User
Tags scalar

OPENCV Component Structure

  About OpenCV's component structure "light ink" great God gave a detailed explanation, detailed reference: List of the mountains small: OpenCV 2.4.8 or OpenCV 2.4.9 component structure full analysis but in OpenCV3.1 still some changes in opencv/build/ All modules in the Include/opencv2 directory with OpenCV,

Altogether there are so many modules, in the light ink of the article also made a detailed introduction, some modules have not been. Then look at the modules that are contained in Java, and you can see the implemented modules from the Opencv.jar:

Obviously see with C + + version of a lot less modules, but the basic functional modules have already, the official should also be in the evening Java version of the interface, in the near future should also have the same features as the C + + version. You can view the implemented methods in the various modules, there are comments in the general method, and the names of the methods are the same as in C + +.

  We know that a picture is made up of different pixel points, and the value of each pixel represents a color, such as a grayscale image, from 0 (black) to 255 (white) to represent the depth of the color. And usually a color image is composed of red, green and blue Three colors, commonly known as RGB images. More about images and colors can refer to Digital image processing books, this is how OPENCV to represent different images. There are many constants in Core->cvtype that define the type of the image, and the basic structure is:

  cv_< number of digits >{u| s|  F}c (< channels >) number of =8|16|32|64 channels =1|2|3|4 up to four channels. The following is a channel to do a detailed description.

    • CV_8UC1 8-bit unsigned integer from 0 to 255
    • CV_8SC1 8-bit signed integer from 128 to 127
    • CV_16UC1 16-bit unsigned integer from 0 to 65536
    • CV_16SC1 16-bit signed integer from 32768 to 32767
    • CV_32SC1 32-bit signed integer from -2^ (32-1) to 2^ (32-1)-1
    • CV_32FC1 32-bit signed decimal
    • CV_64FC1 64-bit signed decimal
You can use this method to create a picture:

Mat image = New Mat (new Size (3,4), CVTYPE.CV_8UC3, New Scalar (new double[]{128,3,4}));

There are many methods of construction in the mat, here is just one, so the construction of the Mat object meaning is 3 pixels wide, height of 4 pixels, 8-bit unsigned three-channel image, the first channel is red all 128, the second channel Green all 3, the third channel Blue is all 4, We can use the following code to print out the information for this object and the data for each pixel point.

        Mat image = New Mat (New Size (3, 4), CVTYPE.CV_8UC3, New Scalar (New double[]{128, 3, 4});        SYSTEM.OUT.PRINTLN (image + "width=" + image.width () + "height=" + image.height ());        System.out.println (Image.dump ());

Using the ToString () method of the Mat object directly, the basic information of the Mat object will be printed out, and dump () returns the value of each pixel, noting that when the pixel width is too large, it can take a long time. Output Result:

Mat [4*3*cv_8uc3, Iscont=true, Issubmat=false, Nativeobj=0x50aaf0, dataaddr=0x4dce00] width=3 height=4[128,   3,   4, N,   3,   4, 4,   3,   3;.;   ......   3,   4, 3,   3,   4,,   4,   3, N,   4,   3,   ,, 4,, 3,   4]

Because the image is too small, the display is particularly small, the basic can not be seen, the width and height of the set will be larger and then display, is a dark blue, that is, the color of red=128,green=3,blue=4.

Some points to note:

There is no notation in Java, all the basic data types are signed, for example, for CV_8UC1, the pixel to represent is from 0 to 255, only one byte in C + + is enough, but Java does not use a byte to represent 0 to 255, So it is only the upward type that is converted. Let's look at a piece of code first:

        Mat image = New Mat (New Size (3, 4), CVTYPE.CV_8UC1, New Scalar (New double[]{234}));        Image.put (1, 2, New byte[]{123});        SYSTEM.OUT.PRINTLN (image);        System.out.println (Image.dump ());        Image.put (1, 2, New byte[]{200-256});        System.out.println (Image.dump ());

  This code first created a 3*4 8-bit single-channel unsigned image, each pixel is 234, and then [] This position (notice from [0,0] coordinates point start count) value to 123, and then print out pixels, no problem, 123<127 so can put the success, But if we want to change a value greater than 127, the byte data will be directly error, compile can not pass, that is not able to create a value greater than 127, of course not, will be greater than the value of 127 minus a 256, for example, now I want to set to 200, how to do it, directly with 200-256, That is, save a-56, and then print it out to see if it is possible. The result of course is yes, 200 has been set to succeed. If it is a CV_8SC1 image, it is how much directly set to how much can be. Why can I print 200, not a byte worth it? Take a look at the Image.get method, which returns a double[] type, which can be saved at 200, and dump returns a string that internally turns 56 to 200.

So there's no need to think too much about getting a pixel of an image, but if you want to set a pixel, and the data type or bit width of the pixel is less than the width you want to set, you have to be aware of it, but it's safer to use the double setting and automatically convert it internally. You will see in the Java version of the OpenCV method in many places are using a double, this is really a lot of waste of memory, many times we are dealing with 8-bit unsigned 3-channel color image, mapped to the Java program, if you want to dump the image data, That's 8 times times the memory to save (one byte in C + + is enough, but Java needs a double, and double takes 8 bits).

The next step is to formally begin the introduction of using OPENCV to process images.



Using Java to develop the component structure of the OPENCV3 program -3.OPENCV and the Java version of the data structure

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.