1.读图像imread()
#include<opencv2\opencv.hpp>using namespace cv;int main(int argc,char* argv[]){ Mat img; //-- 1 --双右斜线法 //string imgpath = "C:\\Users\\bingbuyu\\Pictures\\photo\\miao1.jpg"; //-- 2 --双左斜线法 //string imgpath = "C://Users//bingbuyu//Pictures//photo//miao1.jpg"; //-- 3 --单左斜线法 //string imgpath = "C:/Users/bingbuyu/Pictures/photo/miao1.jpg"; //-- 4 --以上三种混合法 //string imgpath = "C:/Users//bingbuyu\\Pictures//photo//miao1.jpg"; //-- 5 --相对路径法 //string imgpath = "miao.jpg"; //-- 6 --命令行参数法 string imgpath = argv[1]; img = imread(imgpath, 1); imshow("img", img); waitKey(0); return 0;}
2.显示图像imshow()
void cv::imshow (const String& winname,InputArray mat ) Python:None = cv.imshow(winname, mat)
3.保存图像imwirte()
#include <iostream>#include <opencv2\opencv.hpp>using namespace std;using namespace cv;void AlphaMat(Mat &mat){ CV_Assert(mat.channels() == 4); for (int i = 0; i < mat.rows; ++i) { for (int j = 0; j < mat.cols; ++j) { Vec4b& bgra = mat.at<Vec4b>(i, j); bgra[0] = UCHAR_MAX; // 蓝色通道 bgra[1] = saturate_cast<uchar>((float(mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // 绿色通道 bgra[2] = saturate_cast<uchar>((float(mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // 红色通道 bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha通道 } }}int main(int agrc, char** agrv){ // Create mat with alpha channel Mat mat(480, 640, CV_8UC4); AlphaMat(mat); vector<int> compression_params; compression_params.push_back(IMWRITE_PNG_COMPRESSION); //PNG格式图像压缩标志 compression_params.push_back(9); //设置最高压缩质量 bool result = imwrite("alpha.png", mat, compression_params); if (!result) { cout << "保存成PNG格式图像失败" << endl; return -1; } cout << "保存成功" << endl; return 0;}
4.读视频VideoCapture()
#include <opencv2\opencv.hpp>#include <iostream>using namespace std;using namespace cv;int main(){ system("color F0"); //更改输出界面颜色 VideoCapture video("cup.mp4"); if (video.isOpened()) { cout << "视频中图像的宽度=" << video.get(CAP_PROP_FRAME_WIDTH) << endl; cout << "视频中图像的高度=" << video.get(CAP_PROP_FRAME_HEIGHT) << endl; cout << "视频帧率=" << video.get(CAP_PROP_FPS) << endl; cout << "视频的总帧数=" << video.get(CAP_PROP_FRAME_COUNT); } else { cout << "请确认视频文件名称是否正确" << endl; return -1; } while (1) { Mat frame; video >> frame; if (frame.empty()) { break; } imshow("video", frame); waitKey(1000 / video.get(CAP_PROP_FPS)); } waitKey(); return 0;}
5.保存视频VideoWriter()
#include <opencv2\opencv.hpp>#include <iostream>using namespace cv;using namespace std;int main(){ Mat img; VideoCapture video(0); //使用某个摄像头 //读取视频 //VideoCapture video; //video.open("cup.mp4"); if (!video.isOpened()) // 判断是否调用成功 { cout << "打开摄像头失败,请确实摄像头是否安装成功"; return -1; } video >> img; //获取图像 //检测是否成功获取图像 if (img.empty()) //判断有没有读取图像成功 { cout << "没有获取到图像" << endl; return -1; } bool isColor = (img.type() == CV_8UC3); //判断相机(视频)类型是否为彩色 VideoWriter writer; int codec = VideoWriter::fourcc('M', 'J', 'P', 'G'); // 选择编码格式 //OpenCV 4.0版本设置编码格式 //int codec = CV_FOURCC('M', 'J', 'P', 'G'); double fps = 25.0; //设置视频帧率 string filename = "live.avi"; //保存的视频文件名称 writer.open(filename, codec, fps, img.size(), isColor); //创建保存视频文件的视频流 if (!writer.isOpened()) //判断视频流是否创建成功 { cout << "打开视频文件失败,请确实是否为合法输入" << endl; return -1; } while (1) { //检测是否执行完毕 if (!video.read(img)) //判断能都继续从摄像头或者视频文件中读出一帧图像 { cout << "摄像头断开连接或者视频读取完成" << endl; break; } writer.write(img); //把图像写入视频流 //writer << img; imshow("Live", img); //显示图像 char c = waitKey(50); if (c == 27) //按ESC案件退出视频保存 { break; } } // 退出程序时刻自动关闭视频流 //video.release(); //writer.release(); return 0;}
6.调用摄像头