My opencv Study Notes (2): operate on each pixel

Source: Internet
Author: User

First, we recommend a book: The English version of opencv 2 computer vision application programming Cookbook can be downloaded from the Internet. It seems that there is no translation. This book is characterized by the fact that the program in it is not a process-oriented small program written to demonstrate function functions, but a large program written with an object-oriented approach, but he taught you to write it step by step, and you should not put too much pressure on it.

Any image processing algorithm starts from operating on each pixel. Even if you don't use the various image processing functions provided by opencv, you can write programs with the same functions as long as you understand the basic principles of image processing algorithms. Opencv provides a method to access each pixel: The at method, the iterator, And the pointer.

These three methods differ slightly in access speed. In debug mode, this difference is very obvious, but in release mode, this difference is not obvious. We use a set of programs to illustrate these methods. The purpose of the program is to reduce the number of colors in the image. For example, the original image is a color of 256, if I want to change it to 64 colors, I just need to divide the original color by 4 (Division) and multiply it by 4.

First look at the main program:

# Include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> # include <iostream> using namespace STD; using namespace CV; void colorreduce (MAT & inputimage, mat & outputimage, int Div); int main () {mat image = imread ("D:/picture/IMG. TIF "); imshow (" source image ", image); MAT result; // used to save the result. create (image. rows, image. cols, image. type (); // its size and type match double duration with the original image; duration = static_cast <double> (CV: gettickcount (); colorreduce (image, result, 64); duration = static_cast <double> (CV: gettickcount ()-duration; duration/= CV: gettickfrequency (); // The elapsed time in mcout <"time is" <duration <Endl; imshow ("display result", result); waitkey (0 );}

The colorreduce function is called in the main program to reduce the color:

The following is the colorreduce function using the at method. This method is concise and clear, and meets your intuitive understanding of pixels. The runtime is 0.334131.

void colorReduce(Mat& inputImage, Mat& outputImage, int div){outputImage = inputImage.clone();int rows = outputImage.rows;int cols = outputImage.cols;for(int i = 0;i < rows;i++){for(int j = 0;j < cols;j++){outputImage.at<Vec3b>(i,j)[0] =  outputImage.at<Vec3b>(i,j)[0]/div*div + div/2;outputImage.at<Vec3b>(i,j)[1] =  outputImage.at<Vec3b>(i,j)[1]/div*div + div/2;outputImage.at<Vec3b>(i,j)[2] =  outputImage.at<Vec3b>(i,j)[2]/div*div + div/2;}}}

The following is the colorreduce function of the iterator. This method is similar to the STL library usage. The running time is 0.375629.

Void colorreduce (MAT & inputimage, mat & outputimage, int Div) {outputimage = inputimage. clone (); // The template must specify the Data Type mat _ <vec3b >:: iterator it = inputimage. begin <vec3b> (); MAT _ <vec3b>: iterator itend = inputimage. end <vec3b> (); // you can specify the cimage type to avoid writing the type mat _ <vec3b> cimage = outputimage; MAT _ <vec3b> :: iterator itout = cimage. begin (); MAT _ <vec3b>: iterator itoutend = cimage. end (); For (; it! = Itend; it ++, itout ++) {(* itout) [0] = (* It) [0]/Div * Div + DIV/2; (* itout) [1] = (* It) [1]/Div * Div + DIV/2; (* itout) [2] = (* It) [2]/Div * Div + DIV/2 ;}}

The last step is to use the pointer method, which is the fastest but a little abstract. The runtime is 0.00705378!

void colorReduce(Mat& inputImage, Mat& outputImage, int div){outputImage = inputImage.clone();int rows = outputImage.rows;int cols = outputImage.cols*outputImage.channels();for(int i = 0;i < rows;i++){ uchar* data = inputImage.ptr<uchar>(i); uchar* dataout = outputImage.ptr<uchar>(i); for(int j = 0;j < cols;j++) {dataout[j] = dataout[j]/div*div + div/2; }}}
 

By the way, color images in opencv are not stored in RGB order, but BGR, so outputimage in the program. at <vec3b> (I, j) [0] indicates the B component of the point. Similarly, (* It) [0].

 

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.