OpenCV learning basic image operations (7): horizontal and horizontal line extraction, verification code to background

PS: This article uses two small cases to demonstrate the application of morphological operations in image processing.

Extract the horizontal and water straight lines in a picture

AIP introduction

Adaptive threshold segmentation

void adaptiveThreshold(InputArray src, OutputArray dst,  
                        double maxValue, int adaptiveMethod,  
                       int thresholdType, int bolckSize, double C)  

Parameter 1: InputArray type src, input image, fill in single channel, single 8-bit floating point type Mat.
Parameter 2: The result of the function operation is stored here. It is the output image (the same size and type as the input image).
Parameter 3: preset the maximum value that satisfies the condition, and the normal image is 255.
Parameter 4: Specify the adaptive threshold algorithm. You can choose ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C.
Parameter 5: Specify the threshold type. You can choose THRESH_BINARY or THRESH_BINARY_INV. (That is, binary threshold or inverse binary threshold).
Parameter 6: Indicates the size of the neighborhood block, used to calculate the area threshold, and an odd number is selected.
Parameter 7: Parameter C represents a parameter related to the algorithm. It is a constant extracted from the mean or weighted mean, and it can be a negative number.

 

ADAPTIVE_THRESH_MEAN_C is the average value of the local neighborhood block. The algorithm first finds the average value in the block and then subtracts the constant C.

ADAPTIVE_THRESH_GAUSSIAN_C is the Gaussian weighted sum of local neighborhood blocks. The algorithm is to weight the pixels around (x, y) in the area according to the Gaussian function according to their distance from the center point, and then subtract the constant C.

Process and ideas

  • First convert the picture to grayscale

  • Use adaptive threshold segmentation to process the picture into two-to-picture (note: when the foreground is darker than the background, use the closed operation for foreground processing; when the foreground is brighter than the background, use the open operation for foreground processing);

  • The design calculation core is used to extract the horizontal and vertical connected lines:
    • If extracting horizontal and horizontal lines, the shape of the core should be Size(5,1); if extracting vertical lines, the shape of the core should be Size(1,5). In short, in the direction to be extracted, the core should be larger than the other direction.
    • In the extraction direction, the larger the span of the core, the higher the degree of extraction, that is, the higher the degree of connectivity in this direction is required to be extracted.
    • In the non-extraction direction, set it to "1" to ensure that the connectivity in the other direction is blocked from interference in the extraction direction.

Code and practice

#include <iostream>
#include <math.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
	//src = imread("src.jpg");
	Mat src = imread("1.PNG");
	if (!src.data)
	{
		cout << "cannot open image" << endl;
		return -1;
	}
	namedWindow("input image", WINDOW_AUTOSIZE);
	imshow("input image",src);
	
	Mat gray;
	cvtColor(src,gray ,COLOR_BGR2GRAY);
	imshow("gray", gray);
	adaptiveThreshold(gray, gray, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, 10);
	imshow("bineray", gray);
	
	Mat Hline = getStructuringElement(MORPH_RECT, Size(1, 5), Point(-1, -1));
	Mat Vline = getStructuringElement(MORPH_RECT, Size(5, 1), Point(-1, -1));
	Mat H_res, V_res;

	morphologyEx(gray,H_res,MORPH_CLOSE,Hline);
	morphologyEx(gray,V_res,MORPH_CLOSE,Vline);

	imshow("H-res", H_res);
	imshow("V-res", V_res);

	waitKey(0);
	return 0;
}

Remove the background interference in the captcha image

Process and ideas (refer to the example above)

  • First convert the picture to grayscale
  • Use adaptive threshold segmentation to process the picture into two-to-picture (note: when the foreground is darker than the background, use the closed operation for foreground processing; when the foreground is brighter than the background, use the open operation for foreground processing);
  • Adjust the size of the computing core according to the size of the noise, so that it can filter out background interference, but preserve the connectivity of characters. Generally, the aspect ratio of the core size is the same as the aspect ratio of the verification code image. Otherwise, it should be adjusted according to the actual situation to ensure that the computing core is as large as possible while maintaining character connectivity, and the larger the noise, the stronger the filtering.

Code and practice

#include <iostream>
#include <math.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
	
	Mat src = imread("chars.png");
	if (!src.data)
	{
		cout << "cannot open image" << endl;
		return -1;
	}
	namedWindow("input image", WINDOW_AUTOSIZE);
	imshow("input image",src);
	
	Mat gray;
	cvtColor(src,gray ,COLOR_BGR2GRAY);
	imshow("gray", gray);
	adaptiveThreshold(gray, gray, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, 10);
	imshow("bineray", gray);
	
	Mat Kernel = getStructuringElement(MORPH_RECT, Size(3, 5), Point(-1, -1));
        //改变核在总想和横向的范围,来保证字符的连通性
	Mat Result;
	morphologyEx(gray,Result,MORPH_CLOSE,Kernel);

	imshow("Result", Result);

	waitKey(0);
	return 0;
}

Guess you like

Origin blog.csdn.net/fan1102958151/article/details/107092562
Recommended