【发布时间】:2017-06-13 12:13:05
【问题描述】:
我正在处理灰度图像。我正在寻找一种方法,从那些主要具有灰色像素的图像中过滤出主要具有接近黑色和接近白色像素的图像。
目前,我正在做:
- 应用阈值从灰色像素中过滤黑白像素。
- 将黑色计数加上白色计数与图像的总像素数进行比较。
有没有其他方法可以解决这个问题?也许是现有的 OpenCV 方法?
编辑这是我对上述内容的实现。
bool isBinary(cv::Mat img)
{
cv::Mat whites, blacks;
unsigned int count_whites=0, count_blacks=0, count_pixels=0;
// White enough goes to full white (255)
cv::threshold(img, whites, 250, 255, THRESH_BINARY);
// Not black enough goes to full white (255)
cv::threshold(img, blacks, 5, 255, THRESH_BINARY);
cv::MatIterator_<unsigned char> it_whites = whites.begin<unsigned char>(), it_whites_end = whites.end<unsigned char>();
cv::MatIterator_<unsigned char> it_blacks = blacks.begin<unsigned char>();
for (; it_whites != it_whites_end; ++it_whites, ++it_blacks) {
unsigned char current_white=*it_whites, current_black=*it_blacks;
// Checking white
if ((int)*it_whites == 255)
count_whites++;
// Checking black
if ((int)*it_blacks == 0)
count_blacks++;
count_pixels++;
}
// Let's say 80% its predominantly binary for me
return (count_blacks + count_whites) > 0.8*count_pixels;
}
【问题讨论】:
-
你好像忘了问问题。
-
不清楚你在问什么?您的“目前我正在尝试”解决方案失败了吗?工作?错误?
-
您在 2. 中的方法不起作用吗?这似乎是合理的。 “主要”对您意味着什么,由您决定。也许这对你来说很有趣:stackoverflow.com/questions/24716932/…
-
1- 计算包含 255 个 bin 的直方图 2- 计算 bin[0]+bin[254] / sum(bin) 的比率