opencv从摄像头获取图片帧并做边缘检测

    技术2026-03-19  7

    // opencv6_videocapturefromcamera.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> //#include <opencv2/opencv.hpp> #include <opencv2/highgui.hpp> #include <opencv2/videoio.hpp> #include <opencv2/imgproc.hpp> using namespace cv; using namespace std; //debug下在lib文件的名称后加d,release下不加d。 int main() { VideoCapture capture(0); // 0为电脑自带摄像头 /*capture.open(0);*/ if (capture.isOpened()) { cout << "已打开摄像头" << endl; } else { cout << "摄像头打开失败" << endl; } while (1) { Mat frame; capture >> frame; //读取当前帧 if (frame.data == NULL) break; imshow("读取视频的当前帧", frame); Mat edges; cvtColor(frame, edges, COLOR_BGR2GRAY); //转为灰图 blur(edges, edges, Size(8, 8)); //降噪 Canny(edges, edges, 3, 9, 3); //边缘检测 imshow("边缘检测效果", edges); if (waitKey(30) >= 0) break; } capture.release(); return 0; }

    Processed: 0.009, SQL: 9