OpenCV 2.2中的像素访问

mac*_*thy 12 opencv pixel image-processing computer-vision

嗨,我想用opencv告诉我空白图像的像素值,所以输出看起来像这样

10001
00040
11110   
00100
Run Code Online (Sandbox Code Playgroud)

这是我当前的代码,但我不知道如何访问CV_GET_CURRENT调用的结果..任何帮助?

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

  }
}
Run Code Online (Sandbox Code Playgroud)

eta*_*ion 23

在opencv 2.2中,我使用的是C++接口.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)