[英]How do I determine if a pixel is black or white in OpenCV?
我在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
我想將此Python代碼轉換為C ++
這是我到目前為止的內容:
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
}
如何檢查C ++中的像素是黑色還是白色?
您可以將at()
函數用於Mat
對象(請參見OpenCV docs )。
img_otsu.at<uchar>(y,x)
將返回該位置矩陣中元素的值。 請注意,您可能必須將uchar
更改為img_otsu
矩陣的任何類型(例如float
或double
)。 獲得值后,只需將其與0或255比較即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.