Opencv_常用方法

 

记录常用的opencv方法,包含读取,展示图片等.

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;

string videoname = "../file/test.mp4"; //打开的视频文件
VideoCapture capture;
capture.open(videoname);
namedWindow("detect");
while (true)
{
	Mat frame;
	capture >> frame; //读出每一帧的图像
	if (frame.empty())
		break;
	for (int i = 0; i < faces.face_num; i++)
	{
		face_rect.x = faces.face[i].x;
		face_rect.y = faces.face[i].y;
		face_rect.width = faces.face[i].w;
		face_rect.height = faces.face[i].h;
		cv::rectangle(frame, face_rect, CV_RGB(0, 0, 255), 4, 8, 0);
        char scoreBuf[64];
        snprintf(scoreBuf, 64, "%d", i);
        putText(result_cnn, scoreBuf, Point(x, y + h / 2), FONT_HERSHEY_SIMPLEX, 2, CV_RGB(0, 0, 255), 5);
	}
	imshow("detect", frame);
	char c = waitKey(1);
	if (c == 27)
		break;
}
capture.release();
destroyAllWindows();

Mat yuv_img, rgb_img;
yuv_img.create(h * 3 / 2, w, CV_8UC1);
memcpy(yuv_img.data, yuv_buf, buf_len * sizeof(unsigned char));
cvtColor(yuv_img, rgb_img, CV_YUV2BGR_NV12);
resize(rgb_img, rgb_img, cv::Size(w / sca, h / sca));

for (int i = 0; i < 4; i++)
{
	hconcat(rgb_imgs[i * 4 + 0], rgb_imgs[i * 4 + 1], tmp_h_2[i % 2]);
	hconcat(rgb_imgs[i * 4 + 2], rgb_imgs[i * 4 + 3], tmp_h_2[i % 2 + 1]);
	hconcat(tmp_h_2[i % 2], tmp_h_2[i % 2 + 1], tmp_h_4[i]);
}
vconcat(tmp_h_4[0], tmp_h_4[1], tmp_v_2[0]);
vconcat(tmp_h_4[2], tmp_h_4[3], tmp_v_2[1]);
vconcat(tmp_v_2[0], tmp_v_2[1], tmp);