ImageMagick与二进制图像上的OpenCV [英] ImageMagick versus OpenCV on binary image
问题描述
根据这个问题,我正在使用OpenCV和Imagemagick工具进行一些测试。
Based on this question, I'm doing some tests with OpenCV and Imagemagick tools.
考虑以下IM代码及其输出:( rect.png
)
Consider the following IM code and its output: (rect.png
)
convert -size 100x60 xc:white -fill black -draw "rectangle 20,10 80,50" rect.png
convert rect.png -format %c histogram:info:-
2501: ( 0, 0, 0) #000000 gray(0)
3499: (255,255,255) #FFFFFF gray(255)
请注意 2501 + 3499 = 6000 = 100 * 60
,与图像尺寸兼容。因此IM确认图像仅包含黑白像素。
Note that 2501 + 3499 = 6000 = 100 * 60
, compatible with image size. So IM confirms that the image contains only B/W pixels.
请考虑以下代码应该打印非B / W像素:
Consider the following code which should print the non B/W pixels:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int, char** argv)
{
Mat img = imread(argv[1]); // CV_LOAD_IMAGE_GRAYSCALE
Mat image = img;
for (int y=0; y<img.rows;y++)
{
for (int x=0; x<img.cols;x++)
{
if (static_cast<int>(img.at<uchar>(x,y)) != 0 && static_cast<int>(img.at<uchar>(x,y)) != 255)
{
cout << "(" << x <<","<< y <<")="<< static_cast<int>(img.at<uchar>(x,y)) << endl;
}
}
}
}
上面创建的 rect.png
上的输出是:
Its output on rect.png
created above is:
(64,3)=240
(63,5)=240
(65,5)=127
(65,7)=240
(64,11)=127
(65,11)=240
(64,13)=240
(64,17)=240
(63,19)=240
(65,19)=127
(65,21)=240
(60,24)=97
(64,25)=127
(65,25)=240
(64,27)=240
(64,31)=240
(60,32)=112
(60,33)=89
(63,33)=240
(65,33)=127
(60,34)=4
(60,35)=1
(65,35)=240
(64,39)=127
(65,39)=240
(60,40)=112
(60,41)=89
(64,41)=240
(60,42)=4
(60,43)=1
(64,45)=240
(63,47)=240
(65,47)=127
(65,49)=240
(60,52)=2
(64,53)=127
(65,53)=240
(64,55)=240
(60,56)=160
(60,57)=89
(60,58)=4
(60,59)=1
(64,59)=240
Que stion:发生了什么事?图像是二进制还是不是?
Question: what is happening? Is the image binary or not?
推荐答案
除了在RGB与BGR方面是从前到后,OpenCV是在索引方面也是回到前面: - )
As well as being back-to-front in terms of RGB vs BGR, OpenCV is also back-to-front in terms of indexing too :-)
你的行:
if (static_cast<int>(img.at<uchar>(x,y)) != 0 && static_cast<int>(img.at<uchar>(x,y)) != 255)
应
if (static_cast<int>(img.at<uchar>(y,x)) != 0 && static_cast<int>(img.at<uchar>(y,x)) != 255)
我所说的是行索引在列索引之前。
What I am saying is that the row index goes before the column index.
这篇关于ImageMagick与二进制图像上的OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!