使用opencv创建一张纯黑色的图片与其他图片进行合成

功能:使用opencv创建一张纯色的的照片,颜色以及大小尺寸自己设置。

说明:这里的cvSize控制创建图片的大小,就是长和宽。那么在使用for循环的时候注意i  j的范围。使用这种方法你可以创建任意大小和自己喜欢的颜色的图片做图片的底片用来和要被显示的图片进行和,把纯色当底片,根据显示器比例来设置你创建的图片比例。那么在全屏显示没和成之前的图片的时候图片缩放是根据显示器的比例进行缩放的,所以造成显示器两边或者上边有白色的部分,那么当把黑色的图片当底片与要显示的图片进行合成,那么在全屏显示的时候不是图片的部分就是黑色的。

创建一张黑色的图片

#include
//#include"highgui.h"
//#include"cv.h"
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main()
{
	IplImage* img1 = cvCreateImage(cvSize(320, 180), IPL_DEPTH_8U, 3);
	//uchar r1, g1, b1;
	for (int i = 0; i < img1->height; i++)
	{
		uchar *ptrImage = (uchar*)(img1->imageData + i * img1->widthStep);
		//uchar *ptrDst = (uchar*)(img->imageData + i * img->widthStep);

		for (int j = 0; j < img1->width; j++)
		{
			//b1 = ptrImage[3 * j + 0];
			//g1 = ptrImage[3 * j + 1];
			//r1 = ptrImage[3 * j + 2];

			//ptrDst[3 * (j + 400) + 0] = 0;
			//ptrDst[3 * (j + 400) + 1] = 0;
			//ptrDst[3 * (j + 400) + 2] = 0;
			ptrImage[3 * j + 0]=0;
			ptrImage[3 * j + 1]=0;
			ptrImage[3 * j + 2]=0;
		}
	}
	cvSaveImage("c://chenxun.jpg", img1);
}


代码如下:

//----------------------------------------------------
//author:chen(stallman)
//time:2015.1.10
//----------------------------------------------------

#include
#include
#include

using namespace std;
using namespace cv;

//合成图片
int main()
{
	clock_t start, finish;
	start = clock();
	double   duration;
	IplImage*img1 = cvLoadImage("1.jpg");
	//IplImage* img2 = cvLoadImage("2.jpg");
	IplImage*img = cvLoadImage("3200-1800.jpg", 1);

	//IplImage* img = cvCreateImage(cvSize(img1->width + img2->width, img1->height), img1->depth, 3);

	//cout << img1->widthStep << endl;
	//cout << img2->widthStep << endl;

	uchar r1, g1, b1;
	for (int i = 0; i < img1->height; i++)
	{
		uchar *ptrImage = (uchar*)(img1->imageData + i * img1->widthStep);
		uchar *ptrDst = (uchar*)(img->imageData + i * img->widthStep);

		for (int j = 0; j < img1->width; j++)
		{
			b1 = ptrImage[3 * j + 0];
			g1 = ptrImage[3 * j + 1];
			r1 = ptrImage[3 * j + 2];

			ptrDst[3 * (j+400) + 0] = b1;
			ptrDst[3 * (j+400) + 1] = g1;
			ptrDst[3 * (j+400) + 2] = r1;
		}
	}

	cvSaveImage("result.jpg", img);
	finish = clock();
	duration = (double)(finish - start) / CLOCKS_PER_SEC;
	cout << duration << endl;

	cvNamedWindow("img.jpg", 0);
	cvShowImage("img.jpg", img);
	waitKey();
	return 0;
}



你可能感兴趣的:(opencv进化之旅)