Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

image processing - Pixel access in OpenCV 2.2

Hi I want to use opencv to tell me the pixel values of a blank and white image so the output would look like this

10001
00040
11110   
00100

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

IplImage readpix(IplImage*  m_image) {


  cout << "Image width  : " << m_image->width << "
"; 
  cout << "Image height : " << m_image->height << "
"; 
  cout << "-----------------------------------------
"; 


  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 << " 
"; //END OF ROW

  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In opencv 2.2, I'd 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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...