在另一个图像的顶部上放置不透明的图像. -OpenCV [英] Layer image with opacity over the top of another image. - OpenCV

查看:82
本文介绍了在另一个图像的顶部上放置不透明的图像. -OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改

遇到类似问题的人-我找到了另一个答案

Anybody with a similar problem - I found another SO answer here with a great python solution that exploits the speed of NumPy.

请考虑以下问题:

我有两个图像,两个图像大小相同.一个是具有不同不透明度层的红色正方形:

I have two images, both the same size. One is a red square with varying layers of opacity:

还有一个第二个正方形,一个蓝色的正方形,比红色的正方形小,周围不透明,只有白色.

And a second, a blue square, smaller than the red, with no opacity but white surrounding it.

到目前为止,我正在为此项目使用OpenCV的python绑定(在阅读了有关水印的

I am using OpenCV's python bindings for this project and so far (after reading about watermarking here I have this:

redSquare = cv2.imread('redSquare.png', cv2.IMREAD_UNCHANGED)
(rH, rW) = redSquare.shape[:2]

blueSquare = cv2.imread('blueSquare.png')
(h, w) = blueSquare.shape[:2]

blueSquare = np.dstack([blueSquare, np.ones((h,w), dtype = 'uint8') * 255])
overlay = np.zeros((h,w,4), dtype = 'uint8')
overlay[0:rH, 0:rW] = redSquare
output = blueSquare .copy()
cv2.addWeighted(overlay, 0.5, output, 0.5, 0, output)

cv2.imwrite('imageAdded.png', output)

将产生以下输出:

但是,所需的效果是:

However the desired effect is:

现在,我了解到通过使用加权加法,当我真的想要1.0的时候,我使用的是0.5的值,但是当我尝试增加两者的权重时,只有一个值增加了而另一个却减小了.

Now I understand by using weighted adding, I am using 0.5 of each, when I really want 1.0 of each, however when I try increasing the weighht of both, only one is increased and the other is decreased.

如果有人对我如何实现这一目标有所了解,最好是在Python中,但是如果您知道使用C ++的方式,那么我肯定可以复制它.

If anyone has some insight into how I can achieve this, preferably in Python, but if you know a way in C++ I am sure I can replicate it.

谢谢.

推荐答案

这是C ++代码,可以准确给出您想要的结果.

here is C++ code gives exactly the result you want.

// http://jepsonsblog.blogspot.be/2012/10/overlay-transparent-image-in-opencv.html
// https://gist.github.com/maximus5684/082f8939edb6aed7ba0a

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "iostream"

using namespace cv;
using namespace std;

void overlayImage(Mat* src, Mat* overlay, const Point& location)
{
    for (int y = max(location.y, 0); y < src->rows; ++y)
    {
        int fY = y - location.y;

        if (fY >= overlay->rows)
            break;

        for (int x = max(location.x, 0); x < src->cols; ++x)
        {
            int fX = x - location.x;

            if (fX >= overlay->cols)
                break;

            double opacity = ((double)overlay->data[fY * overlay->step + fX * overlay->channels() + 3]) / 255;

            for (int c = 0; opacity > 0 && c < src->channels(); ++c)
            {
                unsigned char overlayPx = overlay->data[fY * overlay->step + fX * overlay->channels() + c];
                unsigned char srcPx = src->data[y * src->step + x * src->channels() + c];
                src->data[y * src->step + src->channels() * x + c] = srcPx * (1. - opacity) + overlayPx * opacity;
            }
        }
    }
}

int main( int argc, char** argv )
{
    Mat overlay = imread("ZuWDz.png",IMREAD_UNCHANGED);
    Mat underlay = imread("CtBAe.png",IMREAD_UNCHANGED);

    if( underlay.empty() || overlay.empty() )
    {
        return -1;
    }

    overlayImage( &underlay, &overlay, Point() );
    imshow("underlay result",underlay);

    waitKey();

    return 0;
}

这篇关于在另一个图像的顶部上放置不透明的图像. -OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆