opencv学习笔记(二)
摘要:学习资料主要参考于毛星云主编《opencv3编程入门》
1、图像显示
#include<opencv2/opencv.hpp>
using namespace cv; //使用命名空间
void main(){
Mat srcImage = imread('1.jpg');//载入图像
imshow('图像标题',srcImage);//显示图像
waitKey(0);//等待按键按下
}
2、图像腐蚀
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat srcImage = imread("E:\\4.JPG");
imshow("原图--腐蚀操作", srcImage);
Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
Mat dsImage;
erode(srcImage, dsImage, element); //腐蚀函数
imshow("效果图", dsImage);
waitKey(0);
return 0;
}
3、图像模糊
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat srcImage = imread("E:\\4.JPG");
imshow("原图--腐蚀操作", srcImage);
Mat dsImage;
blur(srcImage, dsImage, Size(7, 7)); //进行滤波操作
imshow("效果图", dsImage);
waitKey(0);
return 0;
}
4、边缘检测
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat srcImage = imread("E:\\4.JPG");
imshow("原图--腐蚀操作", srcImage);
Mat dsImage,grayImage,edge;
//创建与原图同类型和大小的矩阵dst
dsImage.create(srcImage.size(), srcImage.type());
//将原图转化为灰度图像
cvtColor(srcImage, grayImage, CV_BGR2GRAY);
//使用3*3内核降噪
blur(grayImage, edge, Size(3, 3));
//运行canny算子
Canny(edge,edge,3,9,3);
imshow("效果图", edge);
waitKey(0);
return 0;
}
5、读取视频或调用摄像头
int main()
{
//初始化
VideoCapture capture("E://1.avi");
//调用摄像头
//VideoCapture capture(0);
//循环每一帧,相当一张图
while (1) {
Mat frame;
capture >> frame;//存储每一帧
imshow("视频", frame);
waitKey(50);//50ms
}
return 0;
}
6、写视频文件
int main() {
IplImage *iplImg = cvLoadImage("/home/huanglei/wr.jpg", 1);
IplImage *iplImgNew = cvCreateImage(cvSize(320,240),8,3) ; //改变图片大小适应视频
cvResize(iplImg,iplImgNew,CV_INTER_LINEAR) ;
CvVideoWriter* video = cvCreateVideoWriter("/home/huanglei/tree1.avi",
CV_FOURCC('P', 'I', 'M', '1'), 20,
cvSize(iplImgNew->width, iplImgNew->height), 1);
/*
* 逐帧读取视频,并保存到另外一个视频文件中
*/
CvCapture *cap;
cap = cvCreateFileCapture("/home/huanglei/tree.avi");
IplImage *imgVideo ;
int q ;
while (imgVideo = cvQueryFrame(cap)) {
q = cvWriteFrame(video, imgVideo);
}
/*
* 在新的视频文件的末尾插入图片
*/
int i;
for (int x = 0; x < 50; x++) {
i = cvWriteFrame(video, iplImgNew);
}
/*
* 释放指针
*/
if (video) {
cvReleaseVideoWriter(&video);
cvReleaseImage(&imgVideo);
cvReleaseImage(&imgVideo);
cvReleaseImage(&iplImg);
}
return 0;
}
博主已搬家到csdn了