OPENCV study 13th: Extract horizontal and vertical lines (remove interference lines)

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.