如何确定 OpenCV 中的像素是黑色还是白色
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 文档)。
img_otsu.at<uchar>(y,x)
将返回矩阵中该位置的元素值。请注意,您可能需要将uchar
更改为img_otsu
的任何类型的矩阵(例如,float
或double
)。获得值后,只需将其与 0 或 255 进行比较即可。
相关文章:
- Opengl 3.1 GLSL 140 在 C++ 年输出白色在片段着色器中
- 除非重新绘制大小,否则SDL_Texture在调整大小后呈现黑色
- 为什么我会得到黑色纹理?
- SFML白色矩形
- 帧缓冲纹理变为白色(片段着色器不会影响它)
- 具有 GPU 时间表的卤化物产生黑色图像
- OpenGL只是渲染一个黑色的方块
- 如何在SDL2窗口上使用Cairo绘制一条白色直线
- 为什么在使用VK_PRESENT_MODE_FIFO_KHR呈现模式调整交换链图像大小时出现白色闪烁
- 黑色纹理,如果图片大小在第二个 OpenGL 线程中> 1 * 1
- OpenGL 混合会在纹理周围创建白色边框
- 如何使白色透明
- 连接信号(Qstring)白色插槽(字符串)的2个不同类别
- 在 SFML 中不使用动态内存时显示白色纹理代替实际纹理
- 将未签名的字符转换为红黑色树的签名字符
- 在正方形矩阵中,每个单元格都是黑色或白色.设计了一个算法来寻找最大白色子平方
- C/C++LIBTIFF:需要从BW TIFF文件中读取白色和黑色像素的像素位置
- 如何检测肤色(黑色、棕色、白色)
- OpenGL位图程序只显示白色,黑色和黄色
- 如何确定 OpenCV 中的像素是黑色还是白色