水平竖直线及矩形方式提取结构

 

 

 

 

结构化膨胀:通过自定义提取兴趣的结构,在该结构覆盖下的最大值作为该取值。

 

 

 结构化腐蚀:通过自定义提取兴趣的结构,在该结构覆盖下的最小值作为该取值。

 

 

 

 

 

一、水平直线提取

代码如下:

 

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

using namespace cv;
int main(int argc, char** argv) {
    Mat src, dst;
    src = imread("L:/opencv_picture/14.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);

    Mat gray_src;
    cvtColor(src, gray_src, CV_BGR2GRAY);
    imshow("gray image", gray_src);

    Mat binImg;
    adaptiveThreshold(~gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
    //adaptiveThreshold参数:1.原图  2.生成图像  3.二值图像最大值 4.自适应方法 5.阈值类型  6.块大小 7.常量

    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;
    erode(binImg, temp, hline);
    dilate(temp, dst, hline);
    // morphologyEx(binImg, dst, CV_MOP_OPEN, vline);
    bitwise_not(dst, dst);
    //blur(dst, dst, Size(3, 3), Point(-1, -1));
    imshow("Final Result", dst);

    waitKey(0);
    return 0;
}

 

结果:

 

 

 

二、竖直线提取

仅提取参数代码改变:

 

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

using namespace cv;
int main(int argc, char** argv) {
    Mat src, dst;
    src = imread("L:/opencv_picture/14.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);

    Mat gray_src;
    cvtColor(src, gray_src, CV_BGR2GRAY);
    imshow("gray image", gray_src);

    Mat binImg;
    adaptiveThreshold(~gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
    //adaptiveThreshold参数:1.原图  2.生成图像  3.二值图像最大值 4.自适应方法 5.阈值类型  6.块大小 7.常量

    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;
    erode(binImg, temp, vline);
    dilate(temp, dst, vline);
    // morphologyEx(binImg, dst, CV_MOP_OPEN, vline);
    bitwise_not(dst, dst);
    //blur(dst, dst, Size(3, 3), Point(-1, -1));
    imshow("Final Result", dst);

    waitKey(0);
    return 0;
}

 

结果:

 

 

 

三、矩形提取

代码:

 

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

using namespace cv;
int main(int argc, char** argv) {
    Mat src, dst;
    src = imread("L:/opencv_picture/15.bmp");
    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);

    Mat gray_src;
    cvtColor(src, gray_src, CV_BGR2GRAY);
    imshow("gray image", gray_src);

    Mat binImg;
    adaptiveThreshold(~gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
    //adaptiveThreshold参数:1.原图  2.生成图像  3.二值图像最大值 4.自适应方法 5.阈值类型  6.块大小 7.常量

    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;
    erode(binImg, temp, kernel);
    dilate(temp, dst, kernel);
    // morphologyEx(binImg, dst, CV_MOP_OPEN, vline);
    bitwise_not(dst, dst);
    //blur(dst, dst, Size(3, 3), Point(-1, -1));
    imshow("Final Result", dst);

    waitKey(0);
    return 0;
}

 

 

 

 

posted @ 2019-09-12 15:18  量子与太极  阅读(299)  评论(0编辑  收藏  举报