前言
上节已经讲了图像处理中Canny边缘检测算法原理
https://blog.csdn.net/Aidam_Bo/article/details/86099421
这节主要依据原理,代码佐证
话不多说,直接上码
一、源码
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;//指向原始图像和处理后图像的指针
IplImage *OriginalImage, *GrayImage, *CannyImage;
//原始图像窗口和后期窗口名字
const char *OriginalWindowsNmae = "边缘检测Img_Original";
const char *GrayWindowsName = "边缘检测Img_Gray";
const char *CannyWindowsName = "边缘检测Img_Canny";//trackbar回调函数,当trackbar滑条值发生改变时就会触发该函数
void on_trackbar(int Threshold)
{cvCanny(GrayImage, CannyImage, Threshold, Threshold * 3, 3);cvShowImage(CannyWindowsName, CannyImage);
}
int main()
{//图像(彩色)char *OriginalImageName = "nini2.jpg";char *WindowsToolBar = "Threshold";//原始图像(0:灰度图)OriginalImage = cvLoadImage(OriginalImageName, 260);GrayImage = cvLoadImage(OriginalImageName, 0);CannyImage = cvCreateImage(cvGetSize(GrayImage), IPL_DEPTH_8U, 1);//创建显示原图的窗口cvNamedWindow(OriginalWindowsNmae, 1);//创建显示原图的窗口cvNamedWindow(GrayWindowsName, 1);//创建显示处理后图像的窗口cvNamedWindow(CannyWindowsName, 1);//创建滑条int ThresholdEdge = 1;cvCreateTrackbar(WindowsToolBar, CannyWindowsName, &ThresholdEdge, 100, on_trackbar);//显示原始图像(彩色图)cvShowImage(OriginalImageName,OriginalImage);//显示原始图像(灰度图) cvShowImage(GrayWindowsName, GrayImage);on_trackbar(1);cvWaitKey();//销毁窗口cvDestroyWindow(OriginalWindowsNmae);cvDestroyWindow(GrayWindowsName);cvDestroyWindow(CannyWindowsName);//释放图像cvReleaseImage(&OriginalImage);cvReleaseImage(&GrayImage);cvReleaseImage(&CannyImage);return 0;
}
二、效果图
对 (倪妮版) >=-=<
原图:
灰度图:
Canny算法处理后:
阀值=15时
.阀值=30时
阀值=60时
阀值=100时
完工~