Read-in and simple processing of images

Source: Internet
Author: User

This time we mainly learn to use OPENCV images to read.

OPENCV provides the Imread function to quickly read the image, you can receive it with the mat type object, and the prototype of the Imread function is:

1 Mat imread (conststringint flags);

The first parameter is the file name the second is the way to read, the Imread function uses is:

1 Mat img = imread (filename);

If you read in a JPG file, a 3-channel image is created by default. If you need a grayscale (single-channel) image, use the following statement:

Mat img = imread (filename,0);

Now you will read the image, then the next step is to know how to process the image, to be processed you must first know how to traverse the image, will traverse the image then you will be the complex algorithm and logic applied to the image you want to process, and then you can complete the function you want to implement.

Back to the point, if you want to manipulate the image, you must first use a pointer to the address of the image you want to traverse. Then iterate through each pixel of the image (where we do the line traversal, which is a row of scans per pixel), yet we know that each pixel of the picture is made up of three channels (corresponding to the three primary colors of nature

RGB) composition, in the OpenCV the order of the three channels is not RGB but BGR, as for why, the amount ~, I do not know, later know the complement. The computer then uses the RGB (R,G,B) function to find a value that is the value of the RGB color space, which is the value of the point pixel. So when we traverse the pixel, if we need to

A channel to operate, it must be the number of columns. If you do not need to manipulate the channel, then the columns and rows of the image are the values you see in Windows Explorer with your mouse (xxx*xxx).

Well, having said so much about the basics of the image, we've implemented an example where this example is available on the web, and the function is to use the color space reduction method to improve the efficiency of the image operation. By dividing an existing color space value by one of the input values to get a smaller number of colors. For example, a color value of 0 to 9 is preferable to a new value of 0, 10 to 19 for 10, and so on.

Our example provides two approaches, one that reads directly on its own, and one that is processed using OPENCV's library of functions. 22 just can be used to compare. First look at the example:

1#include <opencv2/opencv.hpp>2 3 using namespacestd;4 using namespaceCV;5 #defineDividewith 506mat& Scanimageandreduceiterator (mat& I,Constuchar*table);7 intMainintargcChar*argv[])8 {9 Mat I, J;Ten     Const Char* ImageName =".. /test.jpg"; One  A     //! Read a image -I =Imread (imagename, cv_load_image_color); -     //! Read Image fail the     if(I.empty ()) -     { -fprintf (stderr,"Can not load image%s\n", imagename); -         return-1; +     } -     //! Show Image +Imshow ("Image", I); A Waitkey (); at     //! Define a table to store the preprocessed data -Uchar table[ the]; -      for(inti =0; I < the; ++i) -Table[i] = dividewith* (I/dividewith);//example:iold=14; inew= (IOLD/10) *10= (14/10) *10=1*10=10; -     DoubleT; -T = (Double) GetTickCount (); in     //Use the iterator (safe) method to realize this function -J =Scanimageandreduceiterator (I.clone (), table); tot = +* ((Double) GetTickCount ()-T)/gettickfrequency (); +cout <<"Time of reducing with the iterator:"<< T <<"milliseconds."<<Endl; -Imshow ("Image1", J); the Waitkey (); *     //Use the lookuptable which provided by OPENCV to realize this function $T = (Double) GetTickCount ();Panax NotoginsengMat LookupTable (1, the, cv_8u); -uchar* p =Lookuptable.data; the      for(inti =0; I < the; ++i) +P[i] =Table[i]; A LUT (I, lookuptable, J); thet = +* ((Double) GetTickCount ()-T)/gettickfrequency (); +cout <<"Time of Use lookuptable:"<< T <<"milliseconds."<<Endl; -Imshow ("Image2", J); $ Waitkey (); $System"Pause"); -     return 0; - } the //! The iterator (safe) method -mat& Scanimageandreduceiterator (mat& I,Constuchar*Consttable)Wuyi { the     //! Accept only char type matrices -Cv_assert (i.depth ()! =sizeof(Uchar)); Wu  -     Const intChannels =i.channels (); About     Switch(Channels) $     { -      Case 1: -     { -Matiterator_<uchar>it, end; A          for(it = i.begin<uchar> (), end = I.end<uchar> (); it! = end; + +it) +*it = table[*it]; the          Break; -     } $      Case 3: the     { theMatiterator_<vec3b>it, end; the          for(it = i.begin<vec3b> (), end = I.end<vec3b> (); it! = end; + +it) the         {
-(*it) [0] = table[(*it) [0]]; in(*it) [1] = table[(*it) [1]]; the(*it) [2] = table[(*it) [2]]; the } About } the } the the returnI; +}

In the above example , Scanimageandreduceiterator function is to traverse the image, channels is the number of channels we said above, which adds the judgment, the different channels of the image of different processing, such as a single channel number directly with the Uchar (0~256) Received, and the three channels used the vector template <Vec3b> This is a good understanding, that is, every pixel has a vector, the vector contains three elements that is three channels, so the pixel can be obtained by <Vec3b> Easy to operate. matiterator_<vec3b> This is an iterator, it is convenient to specify the i.begin<vec3b> () header, and the i.end<vec3b> () tail equivalent to the for loop operation. So the traversal of the image is done.

It says that the function of our program is to achieve the compression of the image color, the compression formula is:, so we put the processed data into a table[256], and then re-assigned to each pixel. As you can see, we first use Mat's constructor new to lookuptable the Mat object, and then assign the table[256 that we handled earlier] directly to the data in the LookupTable object, as follows:

1 Mat lookuptable (1, cv_8u); 2 uchar* p = lookuptable.data; 3      for (int0, + +i) 4         P[i] = Table[i]; 5 LUT (I, lookuptable, J);

Then call the Lut (the original image, look up the table, output the image); directly to our effect, good we traverse the same effect. Let's see the results ~

Original:

1, self-scan assignment

2. Lut function Processing

Time is:, so we have a lot of efficiency, so we better be able to use the pointer to operate, I will achieve the ~

Finally, I have an idea is the next I will write a series, about image processing, the specific idea is to first the basic image operation to achieve once, and then my research and everyone to share ~ I hope you can help me correct mistakes.

Read-in and simple processing of images

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.