2016-05-07 4 views
0

編集別の画像の上に不透明度のあるレイヤー画像。 - 同様の問題とOpenCVの

誰もが - 私は別見つかっSO numpyのの速度を活用素晴らしいのpython溶液でhereに答えます。私は両方とも同じサイズ、2枚の画像を持っている

は、次のような問題を考慮してください。

enter image description here

そして第二に、ブルーno不透明度を有する赤色よりも小さい正方形が、それを囲む白:一つは、不透明度の変化する層を有する赤の正方形です。

enter image description here

私はこのプロジェクトのためのOpenCVのPythonバインディングを使用して、これまで(午前透かしhereについて読んだ後、私はこの持っている: enter image description here

:次の出力を生成し

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) 

をしかし、所望の効果は: enter image description here

私は重み付け加算を使用することで理解していますが、それぞれ0.5を使用していますが、実際にはそれぞれ1.0が必要ですが、両方の重みを増やしてみると1つだけ増え、もう1つは減少します。

誰かが私がこれをどのようにして達成できるかについての洞察力がありますが、Pythonの方が望ましいですが、C++の方法を知っていれば、私はそれを複製できます。

ありがとうございました。

+0

アルファチャンネルで赤い画像を入力してください。 – sturkmen

+0

確かに、私に秒を与えて、私は編集します。 – Aphire

答えて

2

ここはC++コードで、あなたが望む結果が得られます。

// 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; 
} 
+0

このコードをOpenCVライブラリに追加する方法と、OpenCVを再構築できる場合はPythonから呼び出す方法を示すこともできます。 – sturkmen

+0

それはすばらしい、ありがとう! – Aphire

+0

しかし、OpenCV 3.1.0を使用していますが、opencv2が含まれているように見えますが、それによって問題が発生しますか? – Aphire

関連する問題