我正在尝试将名片分段并按背景颜色拆分,以将它们视为不同的感兴趣区域。
例如,这样的一张卡片:
应该能够被拆分成两个图像,因为有两种背景颜色。有什么建议来解决这个问题吗?我尝试了一些轮廓分析,但效果不太好。
其他示例卡:
这张卡应该会给出3个分割,因为有三个部分,即使只有2种颜色(虽然2种颜色也可以)。
以上卡片应该只给出一个分割,因为它只有一个背景颜色。
我还没有考虑渐变背景。
我正在尝试将名片分段并按背景颜色拆分,以将它们视为不同的感兴趣区域。
例如,这样的一张卡片:
应该能够被拆分成两个图像,因为有两种背景颜色。有什么建议来解决这个问题吗?我尝试了一些轮廓分析,但效果不太好。
其他示例卡:
这张卡应该会给出3个分割,因为有三个部分,即使只有2种颜色(虽然2种颜色也可以)。
以上卡片应该只给出一个分割,因为它只有一个背景颜色。
我还没有考虑渐变背景。
-
编辑:您的两张新图片也显示了相同的模式。背景颜色占据了图像的大部分,没有噪点,也没有颜色渐变。因此,一个简单的方法可能如下所示:计算图像的直方图:参见http://docs.opencv.org/modules/imgproc/doc/histograms.html#calchist和http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html
找到直方图中最突出的颜色。如果您不想自己迭代Mat,则可以使用minMaxLoc(http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#minmaxloc),如calchist文档中所示(请参见上文),如果该颜色占足够的像素数量,请将其保存并将相应的bin在直方图中设置为零。重复此过程,直到达到所需百分比。然后,您将保存了一个最突出颜色列表,即背景颜色。
为每个背景颜色对图像进行阈值处理。参见:http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html
在结果阈值化的图像上找到与每个背景颜色相应的区域。参见:http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat src;
Mat src_gray;
int canny_thresh = 100;
int max_canny_thresh = 255;
int size_per_mill = 120;
int max_size_per_mill = 1000;
RNG rng(12345);
bool cmp_contour_area_less(const vector<Point>& lhs, const vector<Point>& rhs)
{
return contourArea(lhs) < contourArea(rhs);
}
void Segment()
{
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Canny(src_gray, canny_output, canny_thresh, canny_thresh*2, 3);
// Draw rectangle around canny image to also get regions touching the edges.
rectangle(canny_output, Point(1, 1), Point(src.cols-2, src.rows-2), Scalar(255));
namedWindow("Canny", CV_WINDOW_AUTOSIZE);
imshow("Canny", canny_output);
// Find the contours.
findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
// Remove largest Contour, because it represents always the whole image.
sort(contours.begin(), contours.end(), cmp_contour_area_less);
contours.resize(contours.size()-1);
reverse(contours.begin(), contours.end());
// Maximum contour size.
int image_pixels(src.cols * src.rows);
cout << "image_pixels: " << image_pixels << "\n";
// Filter the contours, leaving just large enough ones.
vector<vector<Point> > background_contours;
for(size_t i(0); i < contours.size(); ++i)
{
double area(contourArea(contours[i]));
double min_size((size_per_mill / 1000.0) * image_pixels);
if (area >= min_size)
{
cout << "Background contour " << i << ") area: " << area << "\n";
background_contours.push_back(contours[i]);
}
}
// Draw large contours.
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
for(size_t i(0); i < background_contours.size(); ++i)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255));
drawContours(drawing, background_contours, i, color, 1, 8, hierarchy, 0, Point());
}
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", drawing);
}
void size_callback(int, void*)
{
Segment();
}
void thresh_callback(int, void*)
{
Segment();
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
cout << "Please provide an image file.\n";
return -1;
}
src = imread(argv[1]);
cvtColor(src, src_gray, CV_BGR2GRAY);
blur(src_gray, src_gray, Size(3,3));
namedWindow("Source", CV_WINDOW_AUTOSIZE);
imshow("Source", src);
if (!src.data)
{
cout << "Unable to load " << argv[1] << ".\n";
return -2;
}
createTrackbar("Canny thresh:", "Source", &canny_thresh, max_canny_thresh, thresh_callback);
createTrackbar("Size thresh:", "Source", &size_per_mill, max_size_per_mill, thresh_callback);
Segment();
waitKey(0);
}