019-Opencv笔记-Canny边缘检测

2020-03-19  本文已影响0人  赌二八定律
Canny算法

Canny算法介绍 – 五步 in cv::Canny
1.高斯模糊 - GaussianBlur
2.灰度转换 - cvtColor
3.计算梯度 – Sobel/Scharr
4.非最大信号抑制-->沿着梯度方向上进行非极大值的抑制
5.高低阈值输出二值图像
T1, T2为阈值,凡是高于T2的都保留,凡是小于T1都丢弃,从高于T2的像素出发,凡是大于T1而且相互连接的,都保留。最终得到一个输出二值图像。推荐的高低阈值比值为 T2: T1 = 3:1/2:1其中T2为高阈值,T1为低阈值

Canny(
InputArray src, // 8-bit的输入图像
OutputArray edges,// 输出边缘图像, 一般都是二值图像,背景是黑色
double threshold1,// 低阈值,常取高阈值的1/2或者1/3
double threshold2,// 高阈值
int aptertureSize,// Soble算子的size,通常3x3,取值3
bool L2gradient // 选择 true表示是L2来归一化,否则用L1归一化

#include "pch.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
Mat src, gray_src, dst;
int t1_value = 50;
int max_value = 255;
const char* OUTPUT_TITLE = "Canny Result";
void Canny_Demo(int, void*);
int main(int argc, char** argv) {
    src = imread("D:/girl.jpg");
    if (!src.data) {
        printf("could not load image...\n");
        return -1;
    }

    char INPUT_TITLE[] = "input image";
    namedWindow(INPUT_TITLE, 0);
    namedWindow(OUTPUT_TITLE, 0);
    imshow(INPUT_TITLE, src);

    cvtColor(src, gray_src, CV_BGR2GRAY);
    createTrackbar("Threshold Value:", OUTPUT_TITLE, &t1_value, max_value, Canny_Demo);
    Canny_Demo(0, 0);

    waitKey(0);
    return 0;
}

void Canny_Demo(int, void*) {
    Mat edge_output;
    blur(gray_src, gray_src, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
    Canny(gray_src, edge_output, t1_value, t1_value * 2, 3, false);

    //dst.create(src.size(), src.type());
    //src.copyTo(dst, edge_output);
    // (edge_output, edge_output);
    imshow(OUTPUT_TITLE, ~edge_output);
}
#include "pch.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
Mat src, gray_src, dest;
int t1_value = 10;
int max_value = 255;
const char* OUTPUT_TITLE = "Canny Result";
void Canny_Demo(int, void*);

int main(int argc, char** argv) {
    src = imread("D:/girl.jpg");
    if (!src.data) {
        printf("could not load image...\n");
        return -1;
    }

    char INPUT_TITLE[] = "input image";
    namedWindow(INPUT_TITLE, 1);
    namedWindow(OUTPUT_TITLE, 1);
    imshow(INPUT_TITLE, src);

    dest.create(src.size(), src.type());
    cvtColor(src, gray_src, CV_BGR2GRAY);
    createTrackbar("Threshold Value:", OUTPUT_TITLE, &t1_value, max_value, Canny_Demo);
    Canny_Demo(0, 0);

    waitKey(0);
    return 0;
}

void Canny_Demo(int, void*) {
    blur(gray_src, gray_src, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
    double lowt = t1_value / 3;
    Canny(gray_src, gray_src, lowt, t1_value * 2, 3, true);
    dest = Scalar::all(0);
    src.copyTo(dest, gray_src);
    imshow(OUTPUT_TITLE, dest);
}
上一篇下一篇

猜你喜欢

热点阅读