opencv字符分割流程是:1、图像单通道化,2、图像二值化;3、获取图像中的轮廓;4、实现分割。
代码如下:
void SegmentChar(const string strPic)
{
Mat img = imread(strPic, 0);
if (!img.data)
{
return;
}
Mat threshImg;
threshold(img, threshImg, 100, 255, cv::THRESH_BINARY_INV);
std::vector<std::vector<Point>> contours;
Mat hierarchy;
findContours(threshImg, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
drawContours(threshImg, contours, -1, Scalar(255, 0, 255), 1);
std::vector<std::vector<Point>>::const_iterator iter = contours.begin();
while (iter != contours.end())
{
Rect rc = boundingRect(*iter);
rectangle(img, rc, Scalar(0, 255, 255), 1);
iter++;
}
imshow("char", img);
waitKey(0);
}
效果如下
数字和字母的分割效率不错,但是中文字符的分割很差,还需要改进,有一篇不错的文档,可以参考一下https://www.cnblogs.com/chenzhefan/p/7629441.html