opencv-morphology application-extraction and extraction of horizontal and vertical lines

Principle Method

When performing image morphology operations, you can use custom structural elements to realize that the structural elements are sensitive to some objects in the input image and insensitive to other objects, so that the sensitive objects change and the insensitive objects retain the output.
By using the two most basic morphological operations-expansion and erosion, different structural elements are used to achieve the operation of the input image and obtain the desired result.

  • 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

For example, when the operation is started, it is first corroded and then swelled, and the white pieces are removed.
Insert picture description here

Structural element

  • Any structural elements can be used in the expansion and corrosion process
  • Common shapes: various custom shapes such as rectangles, circles, straight lines, disk shapes, masonry shapes, etc.
    Insert picture description here
    If the original image is rectangular, then the rectangular structural elements will not affect its original shape.
    If the original image is circular, then circular structural elements should be selected

Extraction steps

  • Input image color image imread
  • Convert to grayscale image – cvtColor
  • Convert to binary image – adaptiveThreshold
  • Define structural elements
  • Open operation (corrosion + expansion) to extract horizontal and vertical lines
  • The last step is determined based on the binary image

Related 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/lines.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 hline = getStructuringElement(MORPH_RECT, Size(src.cols/16,1), Point(-1,-1) );//横着的结构元素
			Mat vline = getStructuringElement(MORPH_RECT, Size(1,src.rows / 16), Point(-1, -1));//竖着的结构元素

			//开操作
			Mat xdst;
			Mat ydst;
			morphologyEx(binImg, xdst, CV_MOP_OPEN, hline);
			morphologyEx(binImg, ydst, CV_MOP_OPEN, vline);
			
			//优化后输出
			Mat dsty, dstx;
			blur(ydst, dsty, Size(3,3), Point(-1,-1));
			blur(ydst, dstx, Size(3, 3), Point(-1, -1));
			imshow("xline", xdst);
			imshow("yline", ydst);

			waitKey(0);
			return 0;
		}

phenomenon

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

Insert picture description here
Insert picture description here

Guess you like

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