OPENCV对于有alpha通道的透明背景图片的读取和图片叠加

 

 

 

OPENCV对于有alpha通道的透明背景图片的读取和图片叠加  

不过PS是通过在BMP文件格式后追加一个ALPHA数据来实现的,因为BMP本身不支持alpha,所以不能用常规的在程序中直接new bitmap来做。

要先提取追加的这段数据。

 

 

Mat inimg = imread("dog.png", CV_LOAD_IMAGE_UNCHANGED); // 读取透明通道
// 输出RGBA数值
cout << (int)inimg.at(0,0)[0] << endl
     << (int)inimg.at(0,0)[1] << endl
     << (int)inimg.at(0,0)[2] << endl
     << (int)inimg.at(0,0)[3] << endl;

 

#include 
#include 
#include 

using namespace cv;
using namespace std;

int cvAdd4cMat_q(cv::Mat &dst, cv::Mat &scr, double scale);

int main()
{
	char str[16];
	Mat img1 = imread("bk.jpg"), img2 = imread("img.png", -1);
	Mat img1_t1(img1, cvRect(0, 0, img2.cols, img2.rows));
	cvAdd4cMat_q(img1_t1,img2,1.0);
	imshow("final",img1);
	waitKey(0);
	return 0;
}

int cvAdd4cMat_q(cv::Mat &dst, cv::Mat &scr, double scale)  
{  
	if (dst.channels() != 3 || scr.channels() != 4)  
	{  
		return true;  
	}  
	if (scale < 0.01)  
		return false;  
	std::vectorscr_channels;  
	std::vectordstt_channels;  
	split(scr, scr_channels);  
	split(dst, dstt_channels);  
	CV_Assert(scr_channels.size() == 4 && dstt_channels.size() == 3);  

	if (scale < 1)  
	{  
		scr_channels[3] *= scale;  
		scale = 1;  
	}  
	for (int i = 0; i < 3; i++)  
	{  
		dstt_channels[i] = dstt_channels[i].mul(255.0 / scale - scr_channels[3], scale / 255.0);  
		dstt_channels[i] += scr_channels[i].mul(scr_channels[3], scale / 255.0);  
	}  
	merge(dstt_channels, dst);  
	return true;  
}  

 

 

 

 

 

 

你可能感兴趣的:(opencv)