opencv_ex19-圖像放大pyrUp、彩色轉灰階cvtColor、灰階圖像可調式邊緣檢測Canny、二值化圖像反向輸出
opencv_ex19-圖像放大pyrUp、彩色轉灰階cvtColor、灰階圖像可調式邊緣檢測Canny、二值化圖像反向輸出
GITHUB: https://github.com/jash-git/CPP_opencv249_ex
OpenCV canny
void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false)
src:輸入圖,單通道8位元圖。
dst:輸出圖,尺寸、型態和輸入圖相同。
threshold1:第一個閾值。
threshold2:第二個閾值。
apertureSize :Sobel算子的核心大小。
L2gradient :梯度大小的算法,預設為false。
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/ml/ml.hpp> #include <iostream> #include <cstdio> #include <sys/timeb.h> #if defined(WIN32) #define TIMEB _timeb #define ftime _ftime typedef __int64 TIME_T; #else #define TIMEB timeb typedef long long TIME_T; #endif using namespace cv; using namespace std; Mat src;//input image void Pause() { printf("Press Enter key to continue..."); fgetc(stdin); } int t1_value = 50; int max_value = 255; Mat gray_src; const char* OUTPUT_TITLE = "Canny Result"; void Canny_Demo(int, void*); int main() { Mat input; input = imread("Lena_original.jpg"); if (!input.data) { printf("could not load image...\n"); } else { //放大 pyrUp(input, src, Size(input.cols*2, input.rows*2)); Mat tmp; GaussianBlur(src, tmp, Size(3, 3), 0, 0);//高斯模糊GaussianBlur目的為了給圖像預處理時候減低噪聲(雜訊) cvtColor(tmp, gray_src, CV_BGR2GRAY);//彩色轉灰階 imshow("Lena_gray", gray_src); namedWindow(OUTPUT_TITLE, CV_WINDOW_AUTOSIZE); createTrackbar("Threshold Value:", OUTPUT_TITLE, &t1_value, max_value, Canny_Demo); Canny_Demo(0, 0); } waitKey(0); Pause(); return 0; } void Canny_Demo(int, void*) { Mat edge_output; Canny(gray_src, edge_output, t1_value, t1_value * 2, 3, false); imshow(OUTPUT_TITLE, ~edge_output); }