• 直线检测


     1 #include <opencv2/opencv.hpp>
     2 #include <iostream>
     3 #include <math.h>
     4 
     5 using namespace cv;
     6 using namespace std;
     7 
     8 int max_count = 255;
     9 int threshold_value = 100;
    10 const char* output_lines = "Hough Lines";
    11 Mat src, roiImage, dst;
    12 void detectLines(int, void*);
    13 void morhpologyLines(int, void*);
    14 int main(int argc, char** argv) {
    15     src = imread("直线检测.jpg", IMREAD_GRAYSCALE);
    16     if (src.empty()) {
    17         printf("could not load image...
    ");
    18         return -1;
    19     }
    20     namedWindow("input image", CV_WINDOW_AUTOSIZE);
    21     imshow("input image", src);
    22     namedWindow(output_lines, CV_WINDOW_AUTOSIZE);
    23     Rect roi = Rect(10, 10, src.cols - 20, src.rows - 20);
    24     roiImage = src(roi);
    25     imshow("ROI image", roiImage);
    26     // createTrackbar("threshold:", output_lines, &threshold_value, max_count, detectLines);
    27     // detectLines(0, 0);
    28     morhpologyLines(0, 0);
    29 
    30     waitKey(0);
    31     return 0;
    32 }
    33 
    34 //直接使用霍夫变换
    35 void detectLines(int, void*) {
    36     Canny(roiImage, dst, threshold_value, threshold_value * 2, 3, false);
    37     //threshold(roiImage, dst, 0, 255, THRESH_BINARY | THRESH_OTSU);
    38     vector<Vec4i> lines;
    39     HoughLinesP(dst, lines, 1, CV_PI / 180.0, 30, 30.0, 0);
    40     cvtColor(dst, dst, COLOR_GRAY2BGR);
    41     for (size_t t = 0; t < lines.size(); t++) {
    42         Vec4i ln = lines[t];
    43         line(dst, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);
    44     }
    45     imshow(output_lines, dst);
    46 }
    47 
    48 
    49 //先形态学
    50 void morhpologyLines(int, void*) {
    51     // binary image
    52     Mat binaryImage, morhpImage;
    53     threshold(roiImage, binaryImage, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);//取反再二值化
    54     imshow("binary", binaryImage);
    55 
    56     // morphology operation
    57     //用直线的结构元素,开操作去掉除直线以外的对象
    58     Mat kernel = getStructuringElement(MORPH_RECT, Size(20, 1), Point(-1, -1));//用直线型的结构元素
    59     morphologyEx(binaryImage, morhpImage, MORPH_OPEN, kernel, Point(-1, -1));//开操作
    60     imshow("morphology result", morhpImage);
    61 
    62     // dilate image
    63     //膨胀操作,使得直线变得明显
    64     kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
    65     dilate(morhpImage, morhpImage, kernel);
    66     imshow("morphology lines", morhpImage);
    67 
    68     // hough lines
    69     vector<Vec4i> lines;
    70     HoughLinesP(morhpImage, lines, 1, CV_PI / 180.0, 30, 20.0, 0);
    71     Mat resultImage = roiImage.clone();
    72     cvtColor(resultImage, resultImage, COLOR_GRAY2BGR);
    73     for (size_t t = 0; t < lines.size(); t++) {
    74         Vec4i ln = lines[t];
    75         line(resultImage, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);
    76     }
    77     imshow(output_lines, resultImage);
    78     return;
    79 }
  • 相关阅读:
    Java如何遍历二维数据
    Java标识符中常见的命名规则
    Java中常量的概念
    Java的数据类型
    Java中的方法是什么以及方法的书写格式
    Java中什么是构造方法
    Java中继承的概念
    Java中的匿名对象代码实例
    Java集合案例(产生不重复随机数)
    Java中集合的初等案例
  • 原文地址:https://www.cnblogs.com/long5683/p/9741670.html
Copyright © 2020-2023  润新知