2023.10.18
Today's agenda
Using VideoCapture class, computer can do camera image processing
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(){
VideoCapture cap;
cap.open(0);
if(!cap.isopened()){
cerr << "Camera open failed" << "\n";
return -1;
}
Mat frame;
while(true){
cap >> frame;
if(frame.empty()){
cerr << "Frame is empty" << "\n";
break;
}
}
imshow("frame", frame);
if(waitKey(1) == 27){
break;
}
cap.release();
destroyAllWindows();
}
VideoCapture::VideoCapture(int index, int apiPreference = CAP_ANY); bool VideoCapture::open(int index, int apiPreference = CAP_ANY);
parameters
return value
bool VideoCapture::read(OutputArray image); VideoCapture& VideoCapture::operator >> (Mat& image);
parameters
return value
** >> operator overloading is wrapper class of read()
double VideoCapture::get(int propId) const; bool VideoCapture::set(int propId, double value);
parameters
Write image data to video class
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(){
VideoCapture cap(0);
int fourcc = VideoWriter::fourcc('X', 'V', 'I', 'D');
double fps = 15;
Size sz = Size((int)cap.get(CAP_PROP_FRAME_WIDTH), (int)cap.get(CAP_FRAME_HEIGHT));
VideoWriter outputVideo("output.avi", fourcc, fps, sz);
int delay = cvRound(1000 / fps);
Mat frame;
while(true){
cap >> frame;
outputViodeo << frame;
imshow("frame", frame);
if(waitKey(delay) == 27)
break;
}
}
VideoWriter::VideoWriter(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true); bool Videowriter::open(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true);
parameters
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);
paramters
void rectangle(InputOutputArray img, Rect rec, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);
paramters
void circue(InputOutputArray img, Point center, int radius, const scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);
paramters
void putText(InputOutputArray img, const String& text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1, int lineType = LINE_8, bool bottonLeftOrigin = false);