#include<opencv2/opencv.hpp>#include<iostream>usingnamespace cv;usingnamespace std;intmain(int argc,char** argv){// Read the image
Mat img =imread("images/cameraman.tif",0);// Check for failure in reading an Imageif(img.empty()){
cout <<"Could not open an image"<< endl;return-1;}//Name of the window
String win_name ="My First Opencv Program";// Create a windownamedWindow(win_name);// Show our image inside the created window.imshow(win_name, img);// Wait for any keystroke in the window waitKey(0);//destroy the created windowdestroyWindow(win_name);return0;}
3. 使用Opencv 创建图像
#include<opencv2/opencv.hpp>#include<iostream>usingnamespace cv;usingnamespace std;intmain(int argc,char** argv){//Create blanck black color Image with seze 256x256
Mat img1(256,256, CV_8UC1,Scalar(0));
String win_name1 ="Blank Image";namedWindow(win_name1,0);imshow(win_name1, img1);//Create blank blue color Image with size 256x256
Mat img(256,256, CV_8UC3,Scalar(255,0,0));
String win_name ="Blank Blue Color Image";namedWindow(win_name,0);imshow(win_name, img);waitKey(0);destroyWindow(win_name1);destroyWindow(win_name);return0;}
在空白图像上绘制形状:
#include<opencv2/opencv.hpp>#include<iostream>usingnamespace cv;usingnamespace std;intmain(int argc,char** argv){//create a new image which consists of //3 channels //image depth of 8 bits //800 x 600 of resolution (800 wide and 600 high)//each pixels initialized to the value of (100, 250, 30) for Blue, Green and Red planes respectively.
Mat img(512,512, CV_8UC3,Scalar(0,0,0));//画线line(img,Point(0,0),Point(511,511),Scalar(0,255,0),7);//矩形rectangle(img,Point(384,0),Point(510,128),Scalar(255,255,0),5);//画圆circle(img,Point(447,63),63,Scalar(0,0,255),-1);//椭圆ellipse(img,Point(256,256),Point(100,100),0,0,180,255,-1);//添加文字putText(img,"OpenCV!",Point(10,500), FONT_HERSHEY_SIMPLEX,3,Scalar(255,255,255),5,8);
String win_name ="Blank Blue Color Image";//Name of the windownamedWindow(win_name);// Create a windowimshow(win_name, img);// Show our image inside the created window.waitKey(0);// Wait for any keystroke in the windowdestroyWindow(win_name);//destroy the created windowreturn0;}
#include<opencv2/opencv.hpp>#include<iostream>usingnamespace cv;usingnamespace std;intmain(int argc,char* argv[]){//open the video file from PC
VideoCapture cap("images/rhinos.avi");// if not success, exit programif(cap.isOpened()==false){
cout <<"Cannot open the video file"<< endl;return-1;}
cout <<"Press Q to Quit"<< endl;
String win_name ="First Video";namedWindow(win_name);while(true){
Mat frame;// read a framebool flag = cap.read(frame);//Breaking the while loop at the end of the videoif(flag ==false){break;}//display the frame imshow(win_name, frame);//Wait for 100 ms and key 'q' for exitif(waitKey(100)=='q'){break;}}destroyWindow(win_name);return0;}
处理网络相机:
#include<opencv2/opencv.hpp>#include<iostream>usingnamespace cv;usingnamespace std;intmain(int argc,char* argv[]){//open the Webcam
VideoCapture cap(0);// if not success, exit programif(cap.isOpened()==false){
cout <<"Cannot open Webcam"<< endl;return-1;}//get the frames rate of the videodouble fps = cap.get(CAP_PROP_FPS);
cout <<"Frames per seconds : "<< fps << endl;
cout<<"Press Q to Quit"<<endl;
String win_name ="Webcam Video";namedWindow(win_name);//create a windowwhile(true){
Mat frame;bool flag = cap.read(frame);// read a new frame from video //show the frame in the created windowimshow(win_name, frame);if(waitKey(1)=='q'){break;}}return0;}