Video Fusion Based on opencv

The first exercise in Chapter 4:

Create a new image with the same height as the original image and 3 times the width of the original video frame. Copy the three images to the new image respectively:

I make a class:

#pragma once
#include "highgui.hpp"
using namespace cv;
using namespace std;
class opencv_4_1
	CvCapture * capture;
	IplImage * img;
	int fps;
	IplImage *gray;
	IplImage *canny;
	IplImage *all;
	CvFont word;
	void initCapture();
	void ans_1();
	void ans_2();

Head file:

void opencv_4_1::ans_2() {
	cvInitFont(&word, CV_FONT_HERSHEY_SCRIPT_COMPLEX, 1, 1, 1.0, 1);
	while (1) {
		img = cvQueryFrame(capture);
		if (!img)break;
		gray = cvCreateImage(CvSize(cvGetSize(img)), img->depth, 1);
        cvConvertImage(img, gray, CV_BGR2GRAY);
		canny = cvCloneImage(gray);
		cvCanny(gray, canny, 10, 30, 3);
        cvPutText(gray, "gray", CvPoint(250, 250), &word, CvScalar(255));
		cvPutText(img, "original", CvPoint(250,250), &word, CvScalar(255));
		cvPutText(canny, "canny", CvPoint(250, 250),&word, CvScalar(255));
		all = cvCreateImage(CvSize(img->width * 3, img->height), img->depth, img->nChannels);
        //Pointer method
		IplImage * part_1 = cvCreateImageHeader(CvSize(cvGetSize(img)), img->depth, img->nChannels);
		IplImage * part_2 = cvCreateImageHeader(CvSize(cvGetSize(img)), img->depth, img->nChannels);
		IplImage * part_3 = cvCreateImageHeader(CvSize(cvGetSize(img)), img->depth, img->nChannels);
		part_1->origin = part_2->origin = part_3->origin = all->origin;
		part_1->widthStep = part_2->widthStep = part_3->widthStep = all->widthStep;

		part_1->imageData = (char *)cvPtr2D(all, 0, 0);
		part_2->imageData = (char *)cvPtr2D(all, 0, img->width);
		part_3->imageData = (char *)cvPtr2D(all, 0, img->width*2);

		cvCopy(img, part_1);
		cvConvertImage(gray, part_2, CV_GRAY2BGR);
		cvConvertImage(canny, part_3, CV_GRAY2BGR);
		cvSetImageROI(all, CvRect(0, 0, img->width, img->height));
		all->nChannels = img->nChannels;
		cvCopy(img, all);

		cvSetImageROI(all, CvRect(img->width, 0, gray->width, gray->height));
		all->nChannels = gray->nChannels;
		cvCopy(gray, all);

		cvSetImageROI(all, CvRect(img->width*2, 0, canny->width, canny->height));
		all->nChannels = canny->nChannels;
		cvCopy(canny, all);
		if (cvWaitKey(fps) == 27)break;


After fusion, the pointer method is adopted, and all functions are changed into three channel functions. The effect chart is as follows:

Using ROI method:

As a result, it automatically becomes a single channel image, and the first one naturally has no color effect,

Added by cgchris99 on Thu, 26 Dec 2019 23:46:42 +0200