opencv-morphology application-extract the text in the identification code

table of Contents

principle

  • Dilation, the output pixel value is the maximum pixel value of the input image covered by the structural element
  • Corrosion, the output pixel value is the minimum pixel value of the input image covered by the structural element

Expansion will make white expand and black will shrink.
Corrosion will make white shrink and black will expand.

This is based on the principle. When the kernel is scanned, the expansion is to replace the anchor point with the maximum value in the area, and the corrosion is to replace the anchor point with the minimum
value in the area. The maximum value of 255 is white, and the minimum value of 0 is black.

  • Open operation: first corrosion and then expansion
  • Closing operation: first expansion and then corrosion

The opening operation first corrodes, removes the black dots, and then expands, and the black masses are restored to their original state.
The closing operation first expands to remove the white noise, and then expands to restore the white mass.

API

Convert binary image

adaptiveThreshold(
Mat src, // 输入的灰度图像
Mat dest, // 二值图像
double maxValue, // 二值图像最大值(255)
int adaptiveMethod // 自适应方法,只能其中之一 – 
		// ADAPTIVE_THRESH_MEAN_C , ADAPTIVE_THRESH_GAUSSIAN_C 
int thresholdType,// 阈值类型
int blockSize, // 块大小
double C // 常量C 可以是正数,0,负数
)

Such as

adaptiveThreshold(~gray, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);

Define structural elements

Mat getStructuringElement(
    int shape, 
    Size ksize, 
    Point anchor = Point(-1,-1)
    );

(1) Int type shape, element shape, can be one of cv::MorphShapes.

(2) The ksize of the Size type, the size of the structured element.

(3) Point type anchor, the default value (-1, -1), means that the anchor is positioned at the center. Note that only the shape of the cross-shaped element depends on the anchor position. In other cases, the anchor merely adjusts the amount of movement resulting from the morphological operation.

There are several options for the first parameter

-(1) MORPH_RECT: rectangular structure area.

-(2) MORPH_CROSS, cross-shaped structure area.

-(3) MORPH_ELLIPSE, ellipse structure area, filled ellipse inscribed in rectangle Rect (0, 0, esize.width, 0. esize.height).

Code

#include<opencv2\opencv.hpp>
#include<iostream>

		using namespace std;
		using namespace cv;


		int main()
		{
    
    
			Mat src;
			src = imread("C:/Users/86176/Pictures/pics/chars.png");
			if (!src.data)
			{
    
    
				printf("fail to read the pic\n");
			}
			namedWindow("src", CV_WINDOW_AUTOSIZE);
			imshow("src", src);

			/*转化为灰度图*/
			Mat gray;
			if (src.channels() == 3)
			{
    
    
				cvtColor(src, gray, CV_BGR2GRAY);
			}
			else
			{
    
    
				gray = src;
			}
			imshow("gray pic ", gray);
			//转化为二值图像
			Mat binImg;
			adaptiveThreshold(~gray, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);//如果不取反就选用闭操作
			imshow("binImg", binImg);

			//定义结构元素
			Mat kernel = getStructuringElement(MORPH_RECT, Size(3,3), Point(-1,-1) );
			
			//开操作
			Mat dst;
			morphologyEx(binImg, dst, CV_MOP_OPEN, kernel);

			//优化后输出
			Mat dsty, dstx;
			//blur(ydst, dsty, Size(3,3), Point(-1,-1));

			imshow("dst", dst);

			waitKey(0);
			return 0;
		}

effect

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

to sum up

If you open the operation to call the corrosion expansion API, and then debug their kernel size, the effect will be better

Guess you like

Origin blog.csdn.net/qq_28258885/article/details/112762564