美文网首页
012-Opencv笔记-提取水平线和垂直线

012-Opencv笔记-提取水平线和垂直线

作者: 赌二八定律 | 来源:发表于2020-03-19 10:37 被阅读0次
#include "pch.h"
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
int main(int argc, char** argv) {
    Mat src, src2, dst, dst2;
    src = imread("D:/hv.png");
    src2 = imread("D:/a.png");
    if (!src.data) {
        printf("could not load image...\n");
        return -1;
    }

    char INPUT_WIN[] = "input image";
    char OUTPUT_WIN[] = "result image";
    namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE);
    imshow(INPUT_WIN, src);
    imshow("input", src2);

    //灰度图
    Mat gray_src;
    cvtColor(src, gray_src, CV_BGR2GRAY);
    Mat gray_src2;
    cvtColor(src2, gray_src2, CV_BGR2GRAY);
    //imshow("gray image", gray_src);
    
    //二值操作
    Mat binImg;
    adaptiveThreshold(~gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
    Mat binImg2;
    adaptiveThreshold(~gray_src2, binImg2, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
    //imshow("binary image", 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 kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
    //得到图像中的水平线
    Mat temp;

    morphologyEx(binImg, dst, CV_MOP_OPEN, vline);
    bitwise_not(dst, dst);


    //blur(dst, dst, Size(3, 3), Point(-1, -1));
    imshow("vline", dst);

    morphologyEx(binImg, dst, CV_MOP_OPEN, hline);
    bitwise_not(dst, dst);
    imshow("hline", dst);

    morphologyEx(binImg2, dst2, CV_MOP_OPEN, kernel);
    bitwise_not(dst2, dst2);
    imshow("ABCD", dst2);

    waitKey(0);
    return 0;
}
效果图

相关文章

网友评论

      本文标题:012-Opencv笔记-提取水平线和垂直线

      本文链接:https://www.haomeiwen.com/subject/jykqshtx.html