Image/video

David8·2022년 9월 3일
0

컴퓨터비전

목록 보기
2/17

read image

Mat imread( const string& filename, int flags=1)

  1. Flag 1: 컬러
  2. flag 0: 흑백

read video

  1. videoCapture class 사용
  2. get() 메소드 --> 여러가지 정보 가져올 수 있음

    CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
    CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
    • CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
    • CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
    • CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
    • CAP_PROP_FPS Frame rate.
    • CAP_PROP_FOURCC 4-character code of codec.
    • CAP_PROP_FRAME_COUNT Number of frames in the video file.
    • CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
    • CAP_PROP_MODE Backend-specific value indicating the current capture mode.
    • CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
    • CAP_PROP_CONTRAST Contrast of the image (only for cameras).
    • CAP_PROP_SATURATION Saturation of the image (only for cameras).
    • CAP_PROP_HUE Hue of the image (only for cameras).
    • CAP_PROP_GAIN Gain of the image (only for cameras).
    • CAP_PROP_EXPOSURE Exposure (only for cameras).
    • CAP_PROP_CONVERT_RGB Boolean flags indicating whether images
    should be converted to RGB.
    • CAP_PROP_WHITE_BALANCE Currently not supported
    • CAP_PROP_RECTIFICATION Rectification flag for stereo cameras
    (note: only supported by DC1394 v 2.x backend currently)

#include "cv.hpp" #include <iostream>
using namespace cv; 
using namespace std;

int main() {
  Mat frame; 
  VideoCapture cap;
  // check if file exists. if none program ends if (cap.open("background.mp4") == 0) {
  cout << "no such file!" << endl;
  waitKey(0); 
  }
  double fps = cap.get(CAP_PROP_FPS);
  double time_in_msec = cap.get(CAP_PROP_POS_MSEC); int total_frames = cap.get(CAP_PROP_FRAME_COUNT);
 
}
  1. web video

    1. 노트북에 설치된 web cam은 인덱스 0을 가짐
    #include "cv.hpp" 
    #include <iostream>
    
    using namespace cv; 
    using namespace std;
    
    int main() {
      Mat frame;
      // capture from webcam
      // whose device number=0 VideoCapture cap(0);
    }
    

display image

void imshow(const string &winname, InputArray mat) // winname=window name

 #include "cv.hpp" 
 #include <iostream>
 using namespace cv; 
 using namespace std;

 int main() {
     Mat gray_image, color_image;
     // 0 on the 2nd parameter means read img in grayscale gray_image = imread("lena.png", 0);
     // blank 2nd parameter means 1, which means read img in colors color_image = imread("lena.png");
     imshow("gray image", gray_image); imshow("color image", color_image);
     waitKey(0);
     return 0; 
 }

display video

  #include "cv.hpp" 
  #include <iostream>

  using namespace cv; 
  using namespace std;

  int main() {
    Mat frame;
    int fps;
    int delay; 
    VideoCapture cap;

    // check if file exists. if none program ends if (cap.open("background.mp4") == 0) {
    cout << "no such file!" << endl;
    waitKey(0); }
    fps = cap.get(CAP_PROP_FPS); 
    delay = 1000 / fps;

    while (1) {
      cap >> frame;
      if (frame.empty()) {
      cout << "end of video" << endl;
      break; 
     }
    imshow("video", frame);
    waitKey(delay);
      } 
  }
 

image/video 조작

  1. waitKey(int delay=0)
  1. delay in milliseconds
  2. 0은 forever 의미
  1. resize(Mat src, Mat dst, Size(cols, rows) // dst: output image

0개의 댓글