四通道图片颜色叠加,逆运算

/// 
void ProcessSrcImg()
{

	vector<int> compression_params;
	compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
	compression_params.push_back(9);

	CvScalar csWhite = cvScalar(255, 255, 255, 255);
	CvScalar csEmpty = cvScalar(255, 255, 255, 0);
	cv::Mat srcMat = cv::imread("D:\\img\\Radar.png", -1);
	cv::Mat pMat = cv::imread("D:\\img\\img3.png", -1);
	cv::Mat dstmat = cv::imread("D:\\img\\img3.png", -1);
	int r = srcMat.rows;
	int c = srcMat.cols;

	//cv::namedWindow("11", cv::WINDOW_AUTOSIZE);
	//cv::imshow("11", srcMat);
	//cv::waitKey(0);
	if (r == 0 || c == 0)
	{
		return;
	}
	CvScalar scCL,scCLT;
	for (int i = 0; i < r; ++i)
	{
		for (int j = 0; j < c; ++j)
		{
			if (j == 400 && i == 720)
			{
				int a = 0;
			}
			GraphicsSystem::OpenCVUtils::GetPixelColor(srcMat, j, i, scCL);
			GraphicsSystem::OpenCVUtils::GetPixelColor(pMat, j, i, scCLT);
			/*if ( scCL.val[0] == 255 && scCL.val[1] == 255 && scCL.val[2] == 255)
			{
			GraphicsSystem::OpenCVUtils::SetPixelColor(srcMat, j, i, csEmpty);
			}*/

			int b = scCL.val[0];
			int g = scCL.val[1];
			int r = scCL.val[2];
			int alpha = scCL.val[3];

			int b1 = scCLT.val[0];
			int g1 = scCLT.val[1];
			int r1 = scCLT.val[2];
			int alpha1 = scCLT.val[3];

			//d=(des);
			//d<<=8;
			//des1=(des*256-(des-srcu)*alpha)/256;
			//alpha=(des1*256-des*256)/(srcu-des);
			//d = 255-(((255-(*des))*(255-(*srcu)))>>8);
			//des1=(des*255-(des-srcu)*alpha)/255;
			//srcu=des-(des*255-des1*255)/alpha;

			int blackalpha,bluealpha = 0,haveblue=0, alphaNew;
			//blackalpha=255-g;
			blackalpha=g;

			if(g!=b){//have blue
				bluealpha= 255 - r;//(r*255.0-0*255.0)/(255-0.0);
				haveblue=1;
				if(b<255)
				{
					haveblue=2;
				}
			}
			else haveblue=0;

			if(haveblue){//have blue
				if(bluealpha)
				{
					b1=max(0, min(255,255-(255*255-b1*255)/bluealpha));
					r1=max(0, min(255,0-(0*255-r1*255)/bluealpha));
					g1=max(0, min(255,0-(0*255-g1*255)/bluealpha));	
				}

			}
			if(blackalpha != 0)
			{
				b1= max(0, min(255,0-(0*255-b1*255)/blackalpha));
				r1= max(0, min(255,0-(0*255-r1*255)/blackalpha));
				g1= max(0, min(255,0-(0*255-g1*255)/blackalpha));

			}

			alphaNew = 255;
			if(r1 == 0 && g1 == 0)
			{
				b1 = 0;
			}
			GraphicsSystem::OpenCVUtils::SetPixelColor(dstmat, j, i, cvScalar(b1, g1, r1, alphaNew));
		}
	}	
	// 转透明度, 补边界值
	/*cv::namedWindow("22", cv::WINDOW_AUTOSIZE);
	cv::imshow("22", dstmat);
	cv::waitKey(0);*/


	try {
		cv::imwrite("D:\\img\\withbord.png", dstmat, compression_params);
	}
	catch (runtime_error& ex) {
		fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
	}

	//return;


	int TR, TG, TB, TA;
	for (int w = 0; w < c; ++w)
	{
		for (int h = 0; h < r; ++h)
		{
			if (w > 1034 && h > 437)
			{
				GraphicsSystem::OpenCVUtils::SetPixelColor(dstmat, w, h, cvScalar(0, 0, 0, 0));
				continue;
			}

			if (w > 820 && h < 102)
			{
				GraphicsSystem::OpenCVUtils::SetPixelColor(dstmat, w, h, cvScalar(0, 0, 0, 0));
				continue;
			}
			if (w <= 1 || h <= 1 || w >= c -2 || h >= r - 2)
			{
				GraphicsSystem::OpenCVUtils::SetPixelColor(dstmat, w, h, cvScalar(0, 0, 0, 0));
				continue;
			}
			GraphicsSystem::OpenCVUtils::GetPixelColor(dstmat, w, h, scCL);
			if (scCL.val[0] == scCL.val[1] && scCL.val[2] == scCL.val[1])
			{
				GraphicsSystem::OpenCVUtils::SetPixelColor(dstmat, w, h, cvScalar(0, 0, 0, 0));
			}
			else
			{
				continue;
			}

			continue;
			int index[24] = {w-1, h, w-1, h-1, w+1, h-1,  w, h-1, w+1, h, w-1, h+1, w+1, h+1,  w, h+1, w-2, h, w+2, h, w, h-2, w, h+2};
			TR = TG = TB = 0;
			for(int k = 0; k < 12; k++)
			{
				GraphicsSystem::OpenCVUtils::GetPixelColor(dstmat, index[k*2], index[k*2+1], scCLT);
				if (scCL.val[0] != scCL.val[1] || scCL.val[2])//颜色为黑色、灰色、白色
				{

					TR = scCL.val[2];//((TR == 0 ? scCL.val[2] : TR) + scCL.val[2])/2;
					TG = scCL.val[1];//((TG == 0 ? scCL.val[1] : TG) + scCL.val[1])/2;
					TB = scCL.val[0];//((TB == 0 ? scCL.val[0] : TB) + scCL.val[0])/2;
					break;
				}
			}
			//if (TR > 0 || TB > 0 || TG >0)
			{
				GraphicsSystem::OpenCVUtils::SetPixelColor(dstmat, w, h, cvScalar(TB, TG, TR, 255));
			}
			/*else
			{
			GraphicsSystem::OpenCVUtils::SetPixelColor(dstmat, w, h, cvScalar(0, 0, 0, 0));
			} */
		}
	}
	/*vector<int> compression_params;
	compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
	compression_params.push_back(9);*/

	double std_left = 110.0, std_right = 118.0, std_top = 37.5, std_bottom = 31.0;
	double mct_left = 0, mct_top = 0, mct_right = 0, mct_bottom = 0;
	CoordinateConvert::TransFormGeoToMercator(std_left, std_top, mct_left, mct_top);
	CoordinateConvert::TransFormGeoToMercator(std_right, std_bottom, mct_right, mct_bottom);
	cv::Mat mctMat = cv::imread("D:\\img\\img2.png", -1);
	try {
		cv::imwrite("D:\\img\\lnglat.png", dstmat, compression_params);
	}
	catch (runtime_error& ex) {
		fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
	}
	// 墨卡托转换
	double geox = 0, geoy = 0, x = 0, y = 0;
	CvScalar csColor;
	for (int i = 0; i < dstmat.cols; ++i)
	{
		for (int j = 1; j < dstmat.rows; ++j)
		{
			GraphicsSystem::OpenCVUtils::SetPixelColor(mctMat, i, j, csEmpty);
			x = i * (mct_right - mct_left)/dstmat.cols + mct_left;
			y = mct_top - j * (mct_top - mct_bottom)/dstmat.rows;
			CoordinateConvert::TransFormMercatorToGeo(x, y, geox, geoy);
			int src_i = (geox - std_left) * dstmat.cols/(std_right - std_left);
			int src_j = (std_top - geoy) * dstmat.rows/(std_top - std_bottom);

			//srcMat.ptr<uchar>(newj)[newi] = srcMat.ptr<uchar>(j)[i];
			// set color

			if (src_i < 0 || src_j < 0)
			{
				continue;
			}
			GraphicsSystem::OpenCVUtils::GetPixelColor(dstmat, src_i, src_j, csColor);
			if (csColor.val[0] == 255 && csColor.val[1] == 255 && csColor.val[2] == 255)
			{
				continue;
			}
			GraphicsSystem::OpenCVUtils::SetPixelColor(mctMat, i, j, csColor);
		}
	}


	try {
		cv::imwrite("D:\\img\\mct.png", mctMat, compression_params);
	}
	catch (runtime_error& ex) {
		fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
	}
	//cv::imwrite("D:\\img\\process.png", dstmat);

	int a = 0;

	return;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要从一张单通道图片中提取出叠加在上面的颜色数据,可以尝试使用掩码(mask)来实现。 具体来说,你可以先读取原始图片并将其转换为灰度图像: ``` I = imread('image.jpg'); gray = rgb2gray(I); ``` 然后,你可以创建一个掩码(mask)来提取出叠加在上面的颜色数据。掩码是一个二值图像,其中像素值为 1 的部分表示需要提取的颜色数据,像素值为 0 的部分表示不需要提取的部分。 创建掩码的方法有很多种,这里介绍一种简单的方法:使用阈值处理来提取需要的颜色数据。具体来说,你可以先对灰度图像进行阈值处理,得到一个二值图像,然后将其中像素值为 1 的部分作为掩码。 ``` threshold = 128; % 阈值可以根据具体情况进行调整 binary = gray > threshold; % 二值化 mask = uint8(binary); % 转换为掩码 ``` 这里的阈值可以根据实际情况进行调整,以得到需要的颜色数据。 `binary` 是一个二值图像,其中像素值大于阈值的部分为 1,小于阈值的部分为 0。 `mask` 是一个掩码,其中像素值为 1 的部分表示需要提取的颜色数据,像素值为 0 的部分表示不需要提取的部分。 最后,你可以使用掩码来提取原始图像中的颜色数据: ``` color = bsxfun(@times, I, mask); ``` 这里使用了 `bsxfun` 函数来将原始图像和掩码进行逐元素相乘,得到提取出来的颜色数据。 `color` 是一个与原始图像大小相同的矩阵,其中仅包含掩码中像素值为 1 的部分对应的颜色数据,其它部分都为 0。 需要注意的是,这里提取的颜色数据是叠加在上面的所有颜色数据,如果需要提取特定颜色的数据,可能需要使用其它方法来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值