>_<" 边缘检测代码:
1 #include "opencv2/imgproc/imgproc.hpp" 2 #include "opencv2/highgui/highgui.hpp" 3 4 #include <stdio.h> 5 6 using namespace cv; 7 using namespace std; 8 9 int edgeThresh = 1; 10 Mat image, gray, edge, cedge; 11 12 // define a trackbar callback 13 void onTrackbar(int, void*) 14 { 15 blur(gray, edge, Size(3,3)); 16 17 // Run the edge detector on grayscale 18 Canny(edge, edge, edgeThresh, edgeThresh*3, 3); 19 cedge = Scalar::all(0); 20 21 image.copyTo(cedge, edge); 22 imshow("Edge map", cedge); 23 } 24 25 int main( int argc, const char** argv ) 26 { 27 28 image = imread("fruits.jpg", 1);//读取图片到mat 29 cedge.create(image.size(), image.type());//用image生成一个cedge 30 //void cvCvtColor( const CvArr* src, CvArr* dst, int code ); 31 //src 输入的 8-bit , 16-bit 或 32-bit 单倍精度浮点数影像. 32 //dst 输出的 8-bit , 16-bit 或 32-bit 单倍精度浮点数影像. 33 //code 色彩空间转换,通过定义 CV_<src_color_space>2<dst_color_space> 常数 (见下面). 34 //函数 cvCvtColor 将输入图像从一个色彩空间转换为另外一个色彩空间。 35 cvtColor(image, gray, CV_BGR2GRAY);//转换为灰度图[色彩空间转换] 36 37 // Create a window 38 namedWindow("Edge map", 1); 39 // create a toolbar 40 createTrackbar("Canny threshold", "Edge map", &edgeThresh, 100, onTrackbar); 41 // Show the image 42 onTrackbar(0, 0); 43 44 // Wait for a key stroke; the same function arranges events processing 45 waitKey(0); 46 return 0; 47 }
时间: 2024-10-04 14:32:15