1. Pixel resolution
2. Total number of bits to store a digital image
3. Read an image
4. Read a video
5. Read a video from a webcam
6. Display an image
7. Display a video
8. Resize an image
Pixel resolution
- The number of pixels in an image
VGA | 640*480 |
HD | 1280*720 --> 1k |
FHD | 1920*1080 --> 2k |
QHD | 2560*1440 |
UHD | 3840*2160 --> 4k |
Total number of bits to store a digital image
M: the number of rows(height)
N : the number of columns(width)
k : the number of bits
--> b = M*N*k
ex) Intensity level : 256 / Color video / FHD pixel resolution / 1 Hour / 30 fps
What is the total amoung of bits?
--> 8bits/color * 3colors/pixel * (1920*1080 pixels) / frame * 30frames/second * 3600 seconds/hour = 625.70GB
Read an image
Mat imread(const string& filename, int flags = 1)
- flag value as 1 : color / 0 : gray
ex) Mat grey_image = imread("lena.png", 0);
Read a video
VideoCapture
ex)
VideoCapture cap;
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);
CAP_PROP_POS_MSEC | 프레임 재생 시간, ms 반환 |
CAP_PROP_POS_FRAMES | 현재 프레임. 비디오 파일 안의 프레임들 중 해당 프레임의 인덱스 반환. |
CAP_PROP_FRAME_WIDTH | 프레임의 너비 |
CAP_PROP_FRAME_HEIGHT | 프레임의 높이 |
CAP_PROP_FPS | 프레임 속도 |
CAP_PROP_FRAME_COUNT | 비디오 파일 안의 총 프레임 수 |
Read a video from a webcam
#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 an image
void imshow(const string &winname, InputArray mat)
winname : Name of the window
mat: image to be shown
ex) imshow("grey image", grey_image);
Display a video
ex)
#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);
}
while (1) {
cap >> frame;
if (frame.empty()) {
cout << "end of video" << endl;
break;
}
imshow("video", frame);
waitKey(33);
}
}
int waitKey(int delay =0)
: 딜레이 in 밀리세컨즈. 0은 forever을 뜻하는 값.
Resize an image
resize(Mat src, Mat dst, Size(cols, rows)
src: input image
dst: output image
Size(cols, rows) : Size of image to convert
ex)
resize(img, resize_img, Size(200, 200));