Pixel Access in OpenCV 2.2

Hi, I want to use opencv to tell me the pixel values ​​of the blank and white images so the output will look like this:

10001 00040 11110 00100 

Here is my current code, but I'm not sure how to access the results of calling CV_GET_CURRENT .. any help?

 IplImage readpix(IplImage* m_image) { cout << "Image width : " << m_image->width << "\n"; cout << "Image height : " << m_image->height << "\n"; cout << "-----------------------------------------\n"; CvPixelPosition8u position; CV_INIT_PIXEL_POS(position, (unsigned char*)(m_image->imageData), m_image->widthStep, cvSize(m_image->width, m_image->height), 0, 0, m_image->origin); for(int y = 0; y < m_image->height; ++y) // FOR EACH ROW { for(int x = 0; x < m_image->width; ++x) // FOR EACH COL { CV_MOVE_TO(position, x, y, 1); unsigned char colour = *CV_GET_CURRENT(position, 1); // I want print 1 for a black pixel or 0 for a white pixel // so i want goes here } cout << " \n"; //END OF ROW } } 
+12
pixel image-processing opencv computer-vision
Jan 20 2018-11-11T00
source share
3 answers

In opencv 2.2, I would use the C ++ interface.

 cv::Mat in = /* your image goes here, assuming single-channel image with 8bits per pixel */ for(int row = 0; row < in.rows; ++row) { unsigned char* inp = in.ptr<unsigned char>(row); for (int col = 0; col < in.cols; ++col) { if (*inp++ == 0) { std::cout << '1'; } else { std::cout << '0'; } std::cout << std::endl; } } 
+23
Jan 20 2018-11-11T00:
source share

The IplImage structure has a variable char* imageData - it's just a buffer of all pixels. To read it correctly, you must know your image format. For example, for RGB888 image 3, the first characters in the imageData array will represent the r, g, b values ​​of the first pixel in the first row. If you know the image format, you can read the data correctly. The image format can be restored by reading other values ​​of the IplImage structure:

http://opencv.willowgarage.com/documentation/basic_structures.html

I also find it more efficient to write a loop like this:

 uchar r,g,b; for (int y = 0; y < cvFrame->height; y++) { uchar *ptr = (uchar*) (cvFrame_->imageData + y*cvFrame_->widthStep); for (int x = 0; x < cvFrame_->width; x++) { r = ptr[3*x]; g = ptr[3*x + 1]; b = ptr[3*x + 2]; } } 

This code is for RGB888 image

+2
Jan 20
source share

IplImage is an old image format. You should use the new CvMat format, which can store arbitrary matrices. After all, this is just a matrix.

You can then access the pixels using the cvGet2D function, which returns CvScalar.

+2
Jan 20 '11 at 1:01
source share



All Articles