TIL Day 21

Jason Jongyoub Lee·2023년 10월 25일
0

OpenCV

목록 보기
3/11

2023.10.18

Today's agenda

  1. Camera image processing
  2. OpenCV drawing functions

1. Camera image processing

Using VideoCapture class, computer can do camera image processing

VideoCapture class

#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 class member functions

VideoCapture::VideoCapture(int index, int apiPreference = CAP_ANY);
bool VideoCapture::open(int index, int apiPreference = CAP_ANY);

parameters

  • index : Camera id(if one camera connected, value will 0)
    Not the only camera, video file or image and video stream can be opened
  • apiPreference : set prefer camera processing value

return value

  • VideoCapture constructor returns VideoCapture object
  • open() function returns true if success open camera, false if failed
bool VideoCapture::read(OutputArray image);
VideoCapture& VideoCapture::operator >> (Mat& image);

parameters

  • image : current frame. if read fails, set to empty image

return value

  • read() function returns true if success read, false if failed

** >> operator overloading is wrapper class of read()

double VideoCapture::get(int propId) const;
bool VideoCapture::set(int propId, double value);

parameters

  • propId : property flag // defined cv::VideoCaptureProperties

VideoWriter class

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 class member functions

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

  • filename : path to the file name for write video
  • fourcc : video codec
  • fps : frame per seconds
  • frameSize : video frame size
  • isColor : if false, write to the grey scale video

2. OpenCV drawing class

void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, 
			int thickness = 1, int lineType = LINE_8, int shift = 0);

paramters

  • img : input or output image(Mat type object)
  • pt1 : point to start draw line
  • pt2 : point to end draw line
  • color : line color
  • thickness : thickness of line
  • lineType : line type
    LINE_4, LINE8, LINE_AA(Anti Aliasing, prefered options)
void rectangle(InputOutputArray img, Rect rec, const Scalar& color,
				int thickness = 1, int lineType = LINE_8, int shift = 0);

paramters

  • img : input or output image(Mat type object)
  • color : rectangle line color
  • thickness : thickness of rectangle line
  • lineType : rectangle line type
    LINE_4, LINE8, LINE_AA(Anti Aliasing, prefered options)
void circue(InputOutputArray img, Point center, int radius, const scalar& color,
		int thickness = 1, int lineType = LINE_8, int shift = 0);

paramters

  • img : input or output image(Mat type object)
  • center : point of center
  • radius : radius of circle
  • color : rectangle line color
  • thickness : thickness of rectangle line
  • lineType : rectangle line type
    LINE_4, LINE8, LINE_AA(Anti Aliasing, prefered options)
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);
  • img : input or output image(Mat type object)
  • text : text to present to the image
  • org : point to text writing
  • fontFace : type of font
  • fontScale : size of font
  • color : rectangle line color
  • thickness : thickness of rectangle line
  • lineType : rectangle line type
    LINE_4, LINE8, LINE_AA(Anti Aliasing, prefered options)

0개의 댓글