aligning overlapping images and averaging the overlap

8 ビュー (過去 30 日間)
james
james 2011 年 3 月 10 日
i have an empty matrix
canvas = uint8(zeros(10));
images are placed inside
image1=uint8(magic(3));
canvas(2:4,2:4)=image1;
a second image obviously overwrites the first
image2=uint8(magic(3));
canvas(3:5,3:5)=image2;
I would like to average the overlapping region
my attempt was to inspect each element in canvas to see if it was zero or non zero and then average each element(of subsequent images) one by one if it was non zero. sadly my code is really slow and doesn't work properly..
please help

採用された回答

David Young
David Young 2011 年 3 月 10 日
I'm not quite sure what you want to do in the non-overlapping region, but if you want to leave the original images as they are, then you can do it like this:
canvas1 = uint8(zeros(10));
canvas2 = canvas1;
image1=uint8(magic(3));
canvas1(2:4,2:4)=image1;
image2=uint8(magic(3));
canvas2(3:5,3:5)=image2;
canvas = canvas1 + canvas2;
overlap = canvas1 & canvas2;
canvas(overlap) = canvas(overlap)/2;
I don't think this corresponds exactly to your code though. If you could say what the result in your example should be, it would be possible to be more certain of what to do.
  2 件のコメント
james
james 2011 年 3 月 10 日
this does what i want, it should be much faster than the for loops.
thank you
David Young
David Young 2011 年 3 月 10 日
Just a thought - using uint8 for all the arithmetic might cause you problems. For example, it will mean that no value in the overlapping region can exceed 128. It might be wise to do all the arithmetic in floating point, if possible, and convert back to uint8 when definitely need the image in that format.

サインインしてコメントする。

その他の回答 (3 件)

james
james 2011 年 3 月 10 日
this method doesn't work..
canvas = uint8(zeros(10));
image1=uint8(magic(3));
image2=uint8(magic(3));
canvas(2:4,2:4)=image1;
for rowcounter=1:3;
for colcounter=1:3;
if canvas(rowcounter,colcounter)==0;
canvas(2+rowcounter,2+colcounter)=image2(rowcounter,colcounter);
elseif canvas(rowcounter,colcounter)~=0;
canvas(2+rowcounter,2+colcounter)=((canvas(2+rowcounter,2+colcounter))+(image2(rowcounter,colcounter)))/2;
end
end
end
image(canvas)

Sean de Wolski
Sean de Wolski 2011 年 3 月 10 日
To start: -use CONV2 with a ones kernel to figure out how many zeros are touching.
  2 件のコメント
james
james 2011 年 3 月 10 日
can you put that in the context of the ^^ code?
Sean de Wolski
Sean de Wolski 2011 年 3 月 10 日
No, since your above code "doesn't work" for you. I would guess what is wrong with your above code is that you're updating things and then checking the updates but I don't really know. CONV will do the averaging for you; you can use logical indexing to find if an element is zero. I don't totally understand what you're trying to do though, so more explanation would be better.

サインインしてコメントする。


james
james 2011 年 3 月 10 日
this works, could it be faster?
canvas = uint8(zeros(10));
image1=uint8(magic(3));
image2=uint8(magic(3));
canvas(2:4,2:4)=image1;
for rowcounter=1:3;
for colcounter=1:3;
if canvas(2+rowcounter,2+colcounter)==0;
canvas(2+rowcounter,2+colcounter)=image2(rowcounter,colcounter);
end
if canvas(2+rowcounter,2+colcounter)~=0;
canvas(2+rowcounter,2+colcounter)=((canvas(2+rowcounter,2+colcounter))+(image2(rowcounter,colcounter)))/2;
end
end
end
image(canvas);
  2 件のコメント
Sean de Wolski
Sean de Wolski 2011 年 3 月 10 日
You do realize that the it will add image2 twice since you're using two if statements not an else-if statement right?
james
james 2011 年 3 月 10 日
never assume! no i didn't realise thanks.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDeep Learning Toolbox についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by