OpenCV 2.2 中的像素访问 [英] Pixel access in OpenCV 2.2
本文介绍了OpenCV 2.2 中的像素访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 opencv 告诉我一个空白和白色图像的像素值所以输出看起来像这样
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
这是我当前的代码,但我不确定如何访问 CV_GET_CURRENT
调用的结果..有什么帮助吗?
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
}
}
推荐答案
在 opencv 2.2 中,我会使用 C++ 接口.
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;
}
}
这篇关于OpenCV 2.2 中的像素访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文