尝试在给定阈值下将所有像素更改为黑白
Trying to change all the pixels to black and white at a given threshold
我试图使图像黑白,所以我把阈值100。 所有低于 100 的值都将为黑色,其余值将为白色。所以我遍历每个像素并检查其值是否低于 100,然后我将值更改为 0,否则我将其更改为 255。但是代码不起作用。当我打印图像的值时。图像的所有值都变成了 225。运行前的图像 输入图像,这是运行后的图像输出
int main()
{
int x;
Mat img = imread("Canny.png");
cout << depthToStr(img.depth()) << endl;
img.convertTo(img, CV_32S);
// threshold 100.
for (int z = 0; z < img.rows; z++)
{
for (int y = 0; y < img.cols; y++)
{
if (img.at<int>(z,y) >= 100);
{
img.at<int>(z, y) = 225;
}
}
}
// Print the images.
for (int z = 0; z < img.rows; z++)
{
for (int y = 0; y < img.cols; y++)
{
cout << img.at<int>(z, y) << "t";
}
cout << endl;
}
img.convertTo(img, CV_8U);
imshow(" ",img);
waitKey(0);
cin >> x;
waitKey(0);
return 0;
}
if
语句有一个错误。删除其末尾的分号。
if( img.at<int>(z,y) >= 100 ){
img.at<int>(z, y) = 255;
}else{
img.at<int>(z, y) = 0;
}
请注意,您很可能不希望遍历所有像素,因为它可能没有针对某些处理器进行很好的优化。使用opencv,您可以简单地编写
img = img > 100;
这将与您的周期相同。
另一种选择是opencv函数threshold
threshold(img, img, 100, 255, THRESH_BINARY)
相关文章:
- 如何在不产生任何垃圾的情况下获得C中的像素
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何在24位SDL_Surface上设置像素的颜色
- 如何从SDL_Surface获取特定像素的颜色
- 如何将CMSampleBufferRef/CIImage/UIImage转换为像素,例如uint8_t[]
- 在C++中查找像素 RGB 数据的最快方法是什么?
- 是否可以从 OpenGL 缓冲区获取原始大小的像素?
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- 如何使用 sdl2 快速绘制像素网格?
- 如何在C++中进行像素操作? - Linux
- 他们如何将红外锁定像素转换为镜头前方 1m 的正常平面上的位置
- 访问随机图像像素的快速方法,最多一次
- OpenCV 像素访问点与 at() - 不同的值
- 如何使用 freetype2 访问单色位图中的像素状态
- 尝试渲染像素坐标时,简单线条渲染失败
- 计算另一个图像像素满足条件的像素值的平均值
- 如何返回定义良好的内存部分?例如来自图像数据的像素的颜色值
- 在Qt-cpp中通过像素获取QString的子字符串
- 尝试在给定阈值下将所有像素更改为黑白
- 在 C 和 C++ 中从 JPEG 读取黑白像素作为数组