【问题标题】:Want transparent image even after blending即使在混合后也想要透明图像
【发布时间】:2015-12-05 09:19:45
【问题描述】:

我正在尝试混合两个图像,如 here 所示。

这是我的全部代码

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

using namespace cv;

int main( int argc, char** argv )
{
 double beta; double input;

 Mat src1, src2, dst;

 /// Ask the user enter alpha
 std::cout<<" Simple Linear Blender "<<std::endl;
 std::cout<<"-----------------------"<<std::endl;

 src1 = imread("face.jpg");
 src2 = imread("necklace1.png");

 if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
 if( !src2.data ) { printf("Error loading src2 \n"); return -1; }

 double alpha = 0.1; // something
 int min_x = ( (src1.cols - src2.cols)/2 );
 int min_y = ( (src1.rows - src2.rows)/2 );
 int width, height;

 // min_x, min_y should be valid in src1 and [width height] = size(src2)
 cv::Rect roi = cv::Rect(min_x, min_y, src2.cols, src2.rows);  

 // "out_image" is the output ; i.e. src1 with a part of it blended with  src2
 cv::Mat out_image = src1.clone();

 // Set the ROIs for the selected sections of src1 and out_image (the same at the moment)
 cv::Mat src1_roi= src1(roi);
 cv::Mat out_image_roi = out_image(roi);

 // Blend the ROI of src1 with src2 into the ROI of out_image
 cv::addWeighted(src1_roi,alpha,src2,1-alpha,0.0,out_image_roi);

 imshow( "Linear Blend", out_image );

 waitKey(0);
 return 0;
}

我的输入图像是

但是得到的输出是

我希望这条项链像以前一样透明。怎么做?

我试过cvtColor( out_image_roi,out_image_roi, CV_BGR2BGRA );之前cv::addWeighted(src1_roi,alpha,src2,1-alpha,0.0,out_image_roi);

然后输出只是人脸图像。

【问题讨论】:

  • 输入图像是3通道图像吗?也许您只需要在混合之前将其转换为具有 4 个通道。 (例如克隆后):cv::Mat convert_out_img = cv::Mat(out_img,rows,cols,CV_8UC4);
  • @Gombat 如果我们做 cv::Mat convert_out_img = cv::Mat(out_img,rows,cols,CV_8UC4); convert_out_img 将是新的空白图像吗?
  • 对不起,你应该这样做:cv::Mat conv_img = cv::Mat(img.rows,img.cols,CV_8UC4)。此图像为空白。接下来定义如何填充它。 int from_to[] = {0,0,1,1,2,2,3,3};并填写:cv::mixChannels(&img,2,&conv_img,1,from_to,4);
  • @Gombat 它不工作。请尝试将您的逻辑插入我的代码并尝试.. 我认为现在转换后的图像和 src1 有指向不同地址的指针。所以混合是行不通的

标签: c++ opencv image-processing alpha alphablending


【解决方案1】:

您只能使用自定义循环混合 Alpha 通道大于 0 的像素。我认为代码比解释更清楚:

#include <opencv2\opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat3b bkg = imread("path_to_girl_image");
    Mat4b fgd = imread("path_to_necklace_image", IMREAD_UNCHANGED);

    int x = 150;
    int y = 500;
    double alpha = 0.5; // alpha in [0,1]

    Mat3b roi = bkg(Rect(x, y, fgd.cols, fgd.rows));

    for (int r = 0; r < roi.rows; ++r)
    {
        for (int c = 0; c < roi.cols; ++c)
        {
            const Vec4b& vf = fgd(r,c);
            if (vf[3] > 0) // alpha channel > 0
            {
                // Blending
                Vec3b& vb = roi(r,c);
                vb[0] = alpha * vf[0] + (1 - alpha) * vb[0];
                vb[1] = alpha * vf[1] + (1 - alpha) * vb[1];
                vb[2] = alpha * vf[2] + (1 - alpha) * vb[2];
            }
        }
    }

    imshow("Girl with necklace", bkg);
    waitKey();

    return 0;
}

结果:

【讨论】:

  • 漂亮的小胡子 ;-)
  • 如果将alpha 的定义移动到最里面的块double alpha = vf[3] / 255.;,则混合将尊重部分透明度,而不是将每个像素视为完全打开或关闭。 (如果需要,您可以进一步将 alpha 乘以 0.5 以淡化图像)
  • 如果您需要覆盖白色,请参阅stackoverflow.com/questions/46103731/…
猜你喜欢
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
相关资源
最近更新 更多