質問

NumpyとPILを使用して2つの画像を一緒に追加しようとしています。私がこれをする方法 マトラブ 次のようなものです:

>> M1 = imread('_1.jpg');
>> M2 = imread('_2.jpg');
>> resM = M1 + M2;
>> imwrite(resM, 'res.jpg');

私はこのようなものを手に入れます:

Alt Text http://www.deadlink.cc/matlab.jpg

合成プログラムを使用して画像を追加すると、Matlabの結果が正しいようです。

Pythonでは、このようなことをしようとしています。

from PIL import Image
from numpy import *

im1 = Image.open('/Users/rem7/Desktop/_1.jpg')
im2 = Image.open('/Users/rem7/Desktop/_2.jpg')

im1arr = asarray(im1)
im2arr = asarray(im2)

addition = im1arr + im2arr

resultImage = Image.fromarray(addition)
resultImage.save('/Users/rem7/Desktop/a.jpg')

そして、私はこのようなものを手に入れます:

Alt Text http://www.deadlink.cc/python.jpg

なぜ私はそれらすべてのファンキーな色を得るのですか?私も使用してみました ImageMath.eval("a+b", a=im1, b=im2), 、しかし、RGBがサポートされていないことについてのエラーが発生します。

また、あることもわかりました Image.blend() しかし、それにはアルファが必要です。

私が探しているものを達成するための最良の方法は何ですか?

ソース画像(画像が削除されました):

altテキストhttp://www.deadlink.cc/_1.jpg Alt Text http://www.deadlink.cc/_2.jpg

Humm、OK、まあ、私はADD画像アイコンを使用してソース画像を追加しましたが、投稿を編集しているときに表示されますが、何らかの理由で画像が投稿に表示されません。

(画像が削除されました)2013 05 09

役に立ちましたか?

解決

誰もがすでに提案しているように、あなたが観察している奇妙な色はオーバーフローです。そして、あなたが指摘するように Schnaaderの答えのコメント あなた それでもオーバーフローを取得します このような画像を追加する場合:

addition=(im1arr+im2arr)/2

このオーバーフローの理由は、あなたのnumpyアレイ(im1arr im2arruint8 タイプ(すなわち8ビット)。これは、アレイの各要素を最大255しか保持できないことを意味します。そのため、合計が255を超えると、0の周りをループします。

>>>array([255,10,100],dtype='uint8') +  array([1,10,160],dtype='uint8')
array([ 0, 20,  4], dtype=uint8)

オーバーフローを避けるために、アレイは255を超える値を含めることができるはずです。 それらをフロートに変換します たとえば、ブレンド操作を実行します 結果をUINT8に変換します:

im1arrF = im1arr.astype('float')
im2arrF = im2arr.astype('float')
additionF = (im1arrF+im2arrF)/2
addition = additionF.astype('uint8')

君は いけない これを行う:

addition = im1arr/2 + im2arr/2

ブレンド情報を実行する前に、画像のダイナミクス(効果的に画像を7ビットにする)をつぶすことで、情報を失うと。

matlabノート: :Matlabでこの問題が見られない理由は、おそらくMatlabがその機能の1つで暗黙的にオーバーフローを処理するためです。

他のヒント

0.5のアルファ値を持つPILのブレンド()を使用すると、(IM1ARR + IM2ARR)/2に相当します。ブレンドでは、画像にアルファ層があることは必要ありません。

これを試して:

from PIL import Image
im1 = Image.open('/Users/rem7/Desktop/_1.jpg')
im2 = Image.open('/Users/rem7/Desktop/_2.jpg')
Image.blend(im1,im2,0.5).save('/Users/rem7/Desktop/a.jpg')

投稿したコードは、256よりも大きい値と値があふれているようです。 「(a + b) / 2」または「min(a + b、256)」のようなものが必要です。後者は、Matlabの例が行う方法のようです。

To clamp numpy array values:

>>> c = a + b
>>> c[c > 256] = 256

Your sample images are not showing up form me so I am going to do a bit of guessing.

I can't remember exactly how the numpy to pil conversion works but there are two likely cases. I am 95% sure it is 1 but am giving 2 just in case I am wrong. 1) 1 im1Arr is a MxN array of integers (ARGB) and when you add im1arr and im2arr together you are overflowing from one channel into the next if the components b1+b2>255. I am guessing matlab represents their images as MxNx3 arrays so each color channel is separate. You can solve this by splitting the PIL image channels and then making numpy arrays

2) 1 im1Arr is a MxNx3 array of bytes and when you add im1arr and im2arr together you are wrapping the component around.

You are also going to have to rescale the range back to between 0-255 before displaying. Your choices are divide by 2, scale by 255/array.max() or do a clip. I don't know what matlab does

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top