1. 程式人生 > >OpenCV函式cvAddWeighted調整alpha和gamma值

OpenCV函式cvAddWeighted調整alpha和gamma值

建立兩個trackerbar,一個用來調整alpha,一個用來調整gamma值。程式碼如下:

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

#pragma comment(lib, "cv.lib")
#pragma comment(lib, "cxcore.lib")
#pragma comment(lib, "highgui.lib")

IplImage *srcImg = 0, *dstImg = 0;
int gAlpha = 50;
int gGamma = 0;

void on_change(int arg)
{
	// dstImg = alpha*src1 + beta*src2 + gamma
	cvAddWeighted(srcImg, (gAlpha / 50.0), srcImg, 0, gGamma, dstImg);
	cvShowImage("dst", dstImg);
}

int main(int argc, char* argv[])
{
	srcImg = cvLoadImage("D:\\Testing_Images\\view.png");
	dstImg = cvCreateImage(cvGetSize(srcImg), 8, 3);

	cvNamedWindow("src");
	cvShowImage("src", srcImg);
	// 建立trackbar: gAlpha=[0, 100], gGamma=[0, 100]
	cvCreateTrackbar("alpha", "src", &gAlpha, 100, on_change);
	cvCreateTrackbar("gamma", "src", &gGamma, 100, on_change);
	on_change(0);

	cvNamedWindow("dst");
	cvWaitKey(0);

	return 0;
}
演示結果如下: