python中alpha_python – 如何获得正确的alpha值,以完美地融合两个图像?

这是我一般的方式:

int main(int argc,char* argv[])

{

cv::Mat input1 = cv::imread("C:/StackOverflow/Input/pano1.jpg");

cv::Mat input2 = cv::imread("C:/StackOverflow/Input/pano2.jpg");

// compute the vignetting masks. This is much easier before warping,but I will try...

// it can be precomputed,if the size and position of your ROI in the image doesnt change and can be precomputed and aligned,if you can determine the ROI for every image

// the compression artifacts make it a little bit worse here,I try to extract all the non-black regions in the images.

cv::Mat mask1;

cv::inRange(input1,cv::Vec3b(10,10,10),cv::Vec3b(255,255,255),mask1);

cv::Mat mask2;

cv::inRange(input2,mask2);

// now compute the distance from the ROI border:

cv::Mat dt1;

cv::distanceTransform(mask1,dt1,CV_DIST_L1,3);

cv::Mat dt2;

cv::distanceTransform(mask2,dt2,3);

// now you can use the distance values for blending directly. If the distance value is smaller this means that the value is worse (your vignetting becomes worse at the image border)

cv::Mat mosaic = cv::Mat(input1.size(),input1.type(),cv::Scalar(0,0));

for (int j = 0; j < mosaic.rows; ++j)

for (int i = 0; i < mosaic.cols; ++i)

{

float a = dt1.at(j,i);

float b = dt2.at(j,i);

float alpha = a / (a + b); // distances are not between 0 and 1 but this value is. The "better" a is,compared to b,the higher is alpha.

// actual blending: alpha*A + beta*B

mosaic.at<:vec3b>(j,i) = alpha*input1.at<:vec3b>(j,i) + (1 - alpha)* input2.at<:vec3b>(j,i);

}

cv::imshow("mosaic",mosaic);

cv::waitKey(0);

return 0;

}

基本上您计算从您的ROI边界到对象中心的距离,并从两个混合掩码值计算alpha.因此,如果一个图像与边框有较高距离,而另一个图像距离边框较远,则您更喜欢靠近图像中心的像素.对于扭曲图像的大小不一样的情况,对这些值进行归一化将会更好.

但是,更好和更有效的是预先计算混合面具并扭曲它们.最好的是知道您的光学系统的渐晕,并选择和相同的混合面罩(通常较低的边框值).

从以前的代码,你会得到这些结果:

ROI面具:

混合面具(就像印象一样,必须是浮点矩阵):

图像镶嵌:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值