[英]How do I determine if a pixel is black or white in OpenCV?
I have this code in Python: 我在Python中有以下代码:
width = cv.GetSize(img_otsu)[0]
height = cv.GetSize(img_otsu)[1]
#print width,":",height
for y in range(height):
for x in range(width):
if(img_otsu[y,x]==(255.0)):
CountPixelW+=1
if(img_otsu[y,x]==(0.0)):
CountPixelB+=1
I want to convert this Python code to C++ 我想将此Python代码转换为C ++
This is what I have so far: 这是我到目前为止的内容:
cv::threshold(img_gray,img_otsu,0.0,255.0,cv::THRESH_BINARY+cv::THRESH_OTSU);
for(int y =0;y<=img_otsu.size().height;y++)
for(int x=0;x<=img_otsu.size().width;x++)
{
//Check Pixel 0 or 255 This is Problem
}
How to I check if the pixel is black or white in C++? 如何检查C ++中的像素是黑色还是白色?
You can use the at()
function for Mat
objects (see OpenCV docs ). 您可以将
at()
函数用于Mat
对象(请参见OpenCV docs )。
img_otsu.at<uchar>(y,x)
will return the value of the element in the matrix at that position. img_otsu.at<uchar>(y,x)
将返回该位置矩阵中元素的值。 Note that you may have to change uchar
to whatever type of matrix img_otsu
is (eg, float
or double
). 请注意,您可能必须将
uchar
更改为img_otsu
矩阵的任何类型(例如float
或double
)。 Once you get the value, simply compare it to 0 or 255. 获得值后,只需将其与0或255比较即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.