Learning opencv 2.4.9 (2) --- operating pixels

Source: Internet
Author: User

Cool liukun321

From: http://blog.csdn.net/liukun321


Essentially, an image is a matrix composed of numbers. Opencv 2.x represents an image by the data structure of CV: mat. Each element of the matrix represents a pixel. For color images, the element of the matrix is a ternary number. With this new understanding of the image, we can try to use opencv to process the image.

Let's take a look at the image to be processed today:


Today's topic is to access pixels. First, let's take a look at how to access pixel values. In fact, operations on pixel values can be directly or indirectly implemented by members in the CV: mat class. The CV: mat has several member functions that can obtain image data and attributes.

 

Method for operating a single pixel:

At (INT y, int X)

CV: mat member functions:At (INT y, int X)It can be used to access the element coordinates of the corresponding coordinates (x, y) in the image. Note that the data type of the image must be known during compilation, because CV: Mat can store any data type element. Therefore, the at method is implemented using template functions.

Usage: assume that the IMG data type of an image is unsigned char grayscale image (single channel), and you need to assign a value of 128 to the pixels whose coordinates are (10, 12, the corresponding operation is as follows:


img.at<uchar>(12,10) = 128;


If the IMG image to be operated is a color image of the same data type as the unsigned char, you must assign the pixel value of the coordinate (128) to again. This operation is a little different from the above, the need to assign values to each of the three channels of this pixel, the order of the three primary colors of the image in the memory of opencv for the B-G-R (see the notes below ), the procedure is as follows:

img.at<cv::Vec3b>(12,10) [0]= 128;//Bimg.at< cv::Vec3b >(12,10) [1]= 128;//Gimg.at< cv::Vec3b >(12,10) [2]= 128;//R

After understanding the usage of the at method, I will try to use the at method to make a simple processing of the image (adding salt and pepper noise to the image ). Salt and pepper noise is a special noise that randomly sets some pixels of an image to black or white.

Since grayscale images and color images have different operations on a single element, there is a process of determining the image type.

CV: mat image = CV: imread ("test.jpg"); If (image. channles () = 1) {// grayscale image} else {// color chart}

After understanding these processes, let's take a look at the implementation process of adding the salt and pepper noise point function:

#include <opencv2/opencv.hpp>#include<cstdlib>using namespace cv;void salt(Mat &img,int saltNum){int x,y;int i ;for(i = 0;i < saltNum; i++){x = rand()%img.cols;y = rand()%img.rows;if(img.channels() == 1){img.at<uchar>(y,x) = 255;}else if(img.channels() == 3){img.at<Vec3b>(y,x)[0] = 255;img.at<Vec3b>(y,x)[1] = 255;img.at<Vec3b>(y,x)[2] = 255;}}}int main(){Mat image = imread("../test.jpg"); Mat result;result = image.clone();salt(result,3000);namedWindow("src(http://blog.csdn.net/liukun321)" , CV_WINDOW_AUTOSIZE);imshow("src(http://blog.csdn.net/liukun321)", image);imshow("dst(http://blog.csdn.net/liukun321)", result);waitKey();return 0;}

After running the program:

Source image

 


Effect after adding salt and pepper noise


 

In fact, in addition to the at Method for Pixel operations, you can also use the class provided by opencv CV: mat _. CV: mat _Is a template subclass. This class defines many additional methods, but does not provide public member variables. If you know the type of the matrix, use CV: mat _It brings a lot of convenience. It is used as follows:

CV: mat _ <uchar> IMG = imread ("test.jpg"); IMG (128) =; // 10 rows and 12 Columns

Another method to operate pixels is to use the PTR () method of the mat class in combination with member variables such as cols, rows, step, and elemsize to directly perform pointer operations. Let's talk about these member variables first.

ColsNumber of columns representing the image

RowsHeight of the image

StepIndicates the image width in bytes.

ElemsizeIndicates the pixel size (for example, a three-channel uchar matrix with a return value of 3)

 

PRT ()The method is also a template class and requires the types of known pixels during compilation:

CV: mat _ <uchar> IMG = imread ("test.jpg"); uchar * ADDR = IMG. PTR <uchar> (10); // returns the address ADDR + = 12 for 10 rows; // a single channel grayscale map * ADDR = 128;


The operation on 10th rows and 12th columns of pixels is also completed. If the image is a three-channel color chart:


CV: mat _ <uchar> IMG = imread ("test.jpg"); uchar * ADDR = IMG. PTR <uchar> (10); // returns the address ADDR + = 12 * IMG of 10 rows. elemsize; // single-channel grayscale * ADDR = 128;

ADDR + = 12 * IMG. elemsize is used to store color images in memory: the first three bytes in the Image Buffer correspond to the three channel values of the first pixel in the upper left corner of the image, the next three bytes correspond to the second pixel of the first line, and so on. Note that opencv uses the BGR channel sequence by default.

This section describes how to operate pixels in an image in step 3. In addition to the three types, there is also an operation using the iterator. I will not introduce it again today.

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.