加强版第十七章空间色彩转换
API:cvtColor(frame, hsv, COLOR_BGR2HSV);
将视频进行空间色彩转换
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argv, char** argc) {
//VideoCapture capture(0);
VideoCapture capture("C:/newword/image/21.mp4");
if (!capture.isOpened()) {
printf("0");
}
namedWindow("frame", WINDOW_AUTOSIZE);
int fps = capture.get(CAP_PROP_FPS);
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int heigh = capture.get(CAP_PROP_FRAME_HEIGHT);
int num_of_frame = capture.get(CAP_PROP_FRAME_COUNT);
int type = capture.get(CAP_PROP_FOURCC);
printf("frame size(f=%d,w=%d,h=%d,m=%d)", fps, width, heigh, num_of_frame);
Mat frame,hsv;
VideoWriter writer("C:/newword/image/232320.mp4", type, fps, Size(width, heigh), true);
while (true) {
bool ret = capture.read(frame);
if (!ret)break;
imshow("ret", frame);
cvtColor(frame, hsv, COLOR_BGR2HSV);
imshow("hsv",hsv);
writer.write(frame);
char c = waitKey(50);
if (c == 27) {
break;
}
}
waitKey(0);
destroyAllWindows();
}
API:inRange(hsv, Scalar(35, 43, 46), Scalar(77, 255, 255), mask);根据bgr修改高低值
API:bitwise_not(mask, mask);取反
API:bitwise_and(frame, frame, result, mask);根据mask来将人物提取出来
将视频转化为白底黑色人物后取反
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argv, char** argc) {
//VideoCapture capture(0);
VideoCapture capture("C:/newword/image/21.mp4");
if (!capture.isOpened()) {
printf("0");
}
namedWindow("frame", WINDOW_AUTOSIZE);
int fps = capture.get(CAP_PROP_FPS);
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int heigh = capture.get(CAP_PROP_FRAME_HEIGHT);
int num_of_frame = capture.get(CAP_PROP_FRAME_COUNT);
int type = capture.get(CAP_PROP_FOURCC);
printf("frame size(f=%d,w=%d,h=%d,m=%d)", fps, width, heigh, num_of_frame);
Mat frame,hsv,mask;
VideoWriter writer("C:/newword/image/232320.mp4", type, fps, Size(width, heigh), true);
while (true) {
bool ret = capture.read(frame);
if (!ret)break;
imshow("ret", frame);
cvtColor(frame, hsv, COLOR_BGR2HSV);
inRange(hsv, Scalar(35, 43, 46), Scalar(77, 255, 255), mask):
bitwise_not(mask, mask);
bitwise_and(frame, frame, result, mask);
imshow("mask", mask);
imshow("hsv",hsv);
writer.write(frame);
char c = waitKey(50);
if (c == 27) {
break;
}
}
waitKey(0);
destroyAllWindows();
}