1. The structure element
can be a structural element of any shape: rectangles, circles, lines, disk shapes, masonry shapes, and so on
2. Extract Step
Input Image Color Image Imread
Convert to Grayscale image Cvtcolor
Convert to two-valued image adaptivethreshold
Define structure element
Open operation (corrosion + expansion) extract horizontal and vertical lines
3.api
Adaptivethreshold-into a two-valued image (
Mat src (input grayscale image),
Mat dest (binary image),
Double maxValue (maximum value of binary image),
int Adaptivemeth OD (Adaptive method) (Adaptive_thresh_mean_c/adaptive_thresh_gaussian_c),
int thresholdtype (threshold type),
int blockSize (block size),
Double C (constant C can be positive, 0, negative)
Direct Extraction API
Morphologyex (binimage, DST, Cv_mop_open, hline);
4. Instance
code is as follows:
#include <opencv2/opencv.hpp> #include <iostream> using namespace CV;
using namespace Std;
int main (int argc, char** argv) {Mat src, DST;
src = imread ("f:/picture/remove interference line. png");
if (Src.empty ()) {printf ("could not image");
return-1;
} Mat gray_src;
Cvtcolor (SRC, gray_src, cv_bgr2gray);
Imshow ("Grey", GRAY_SRC);
Convert to two-valued image Mat Binimage;
Adaptivethreshold (~GRAY_SRC, Binimage, 255, Adaptive_thresh_mean_c, Cv_thresh_binary, 15,-2);
Imshow ("Two values", binimage);
Mat hline = getstructuringelement (Morph_rect, Size (src.cols/16,1), point (-1,-1));
Mat yline = getstructuringelement (Morph_rect, Size (1, SRC.ROWS/16), point (-1,-1));
Mat sline = getstructuringelement (Morph_rect, Size (3,3), point (-1,-1));
Mat temp;
Erode (Binimage, temp, sline);
Dilate (temp, DST, sline);
Morphologyex (Binimage, DST, Cv_mop_open, hline);
Blur (DST, DST, Size (3, 3), point (-1,-1));
Bitwise_not (DST, DST);Imshow ("Horizontal line", DST);
Imshow ("Test", SRC);
Waitkey (0);
return 0; }
The effect is as follows: