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

  1. 对彩色图分离通道,拿到红色通道图
  2. 进行阈值分割
#include<iostream>
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<stdlib.h>
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<Mat> 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;
}

GitHub 加速计划 / opencv31 / opencv
162
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
796adf5d Avoid adding value to nullptr 16 小时前
1a6ef7e0 Fixed Android build with Vulkan support. 19 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐