OpenCV实践:去除票据中的红色印章

现实生活中,一些票据(比如发票、车票)等都会有一个红色印章,有时会盖在某个关键区域,影响了其他的字符识别。因此,为了提高准确率,我们尽量会移除红色印章,具体实现方法如下:

  1. 对彩色图分离通道,拿到红色通道图
  2. 进行阈值分割
#include
#include
#include
#include
using namespace cv;
using namespace std;

int main()
{
	char*  path = "D:\\01.jpg";
	Mat src = imread(path);
	if (src.empty()){
		return -1;
	}
	imshow("original", src);
	Mat gray;
	cvtColor(src, gray, CV_BGR2GRAY);
	
	//全局二值化
	int thresh_value = 100;
	Mat binary;
	threshold(gray, binary, thresh_value, 255, CV_THRESH_BINARY);
	imshow("binary", binary);

	vector channes;
	split(src, channes);
	Mat red = channes[2];
	imshow("red", binary);

	Mat red_binary;
	threshold(red, red_binary, thresh_value, 255, CV_THRESH_BINARY);
	imshow("red+binary", red_binary);

	//Mat red_dilate;
	//Mat kernel = getStructuringElement(MORPH_RECT, Size(2, 2), Point(-1, -1));
	//dilate(red_binary, red_dilate, kernel);
	//Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(2, 2), Point(-1, -1));
	//morphologyEx(red_binary, red_dilate, MORPH_OPEN, kernel, Point(-1, -1));
	//imshow("red+binary+dilate", red_dilate);

	waitKey();
	return 0;
}

OpenCV实践:去除票据中的红色印章_第1张图片

你可能感兴趣的:(opencv)