图片腐蚀
#include <iostream>#include "opencv2/core.hpp"#include <opencv2/imgcodecs.hpp>#include <opencv2/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>using namespace std;using namespace cv;int main(int argc, char** argv){ //载入原图 Mat srcImage = imread("E:\\opencv test image\\a.jpg"); //显示原图 imshow("原图", srcImage); //进行腐蚀操作 Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); Mat dstImage; erode(srcImage, dstImage, element); //显示效果 imshow("腐蚀操作", dstImage); while (1) { waitKey(0); } return 0;}
图像模糊
#include <iostream>#include "opencv2/core.hpp"#include <opencv2/imgcodecs.hpp>#include <opencv2/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>using namespace std;using namespace cv;int main(int argc, char** argv){ //载入原图 Mat srcImage = imread("E:\\opencv test image\\a.jpg"); //显示原图 imshow("原图", srcImage); //进行滤波操作 Mat dstImage; blur(srcImage, dstImage, Size(7, 7)); //显示效果 imshow("均值滤波", dstImage); while (1) { waitKey(0); } return 0;}
canny边缘检测
#include <iostream>#include "opencv2/core.hpp"#include <opencv2/imgcodecs.hpp>#include <opencv2/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>using namespace std;using namespace cv;int main(int argc, char** argv){ //载入原图 Mat srcImage = imread("E:\\opencv test image\\a.jpg"); //显示原图 imshow("原图", srcImage); Mat dstImage, edge, grayImage; //创建与src同类型和大小的矩阵 dstImage.create(srcImage.size(), srcImage.type()); //将原图像转换为灰度图像 cvtColor(srcImage, grayImage, COLOR_BGR2GRAY); blur(grayImage, edge, Size(3, 3)); Canny(edge, edge, 3, 9, 3); imshow("Canny边缘检测", edge); while (1) { waitKey(0); } return 0;}
视频读取
#include <iostream>#include "opencv2/core.hpp"#include <opencv2/imgcodecs.hpp>#include <opencv2/highgui.hpp>#include<opencv2/opencv.hpp>#include<opencv2/imgproc/imgproc.hpp>using namespace std;using namespace cv;int main(int argc, char** argv){ //读取视频 VideoCapture capture("1.avi"); //循环显示每一帧 while (1) { Mat frame; capture >> frame; imshow("读取视频", frame); waitKey(30); } while (1) { waitKey(0); } return 0;}
调用摄像头采集图像
#include <iostream>#include "opencv2/core.hpp"#include <opencv2/imgcodecs.hpp>#include <opencv2/highgui.hpp>#include<opencv2/opencv.hpp>#include<opencv2/imgproc/imgproc.hpp>using namespace std;using namespace cv;int main(int argc, char** argv){ //从摄像头读入视频 VideoCapture capture(0); Mat edges; //循环显示每一帧 while (1) { Mat frame; capture >> frame; //转换为灰度图像 cvtColor(frame, edges, CV_BGR2GRAY); //使用3*3内核降噪2*3+1=7 blur(edges, edges, Size(7, 7)); //进行canny边缘检测后并显示 Canny(edges, edges, 0, 30, 3); imshow("canny后的视频", edges); waitKey(30); } while (1) { waitKey(0); } return 0;}