Dimensions of arrays being concatenated are not consistent. trying to zeropad the image

1 次查看(过去 30 天)
My aim is to crop the portion of image i desire using Drawrectangle and then zeropad the croped image in such a way that every time i crop the image of varaible sizes (no matter what size of image i crop) and the final image comes out to be the croped portion surrounded by zeropaded image to make the final image of size 480x640. eg my if my croped image is
4 5 6
6 5 8
7 8 9
then final image should be 0 0 0 0 0 0 0 0 0 having the size 480x640.
0 0 0 4 5 6 0 0 0
0 0 0 6 5 8 0 0 0
0 0 0 7 8 9 0 0 0
I = imread('pic.PNG');
imshow(I);
g=drawrectangle;
P=g.Position;
x=P(1,1);
y=P(1,2);
w=P(1,3);
h=P(1,4);
II = I(y:y+h,x:x+w);
S=size(II);
LengthII=S(1);
WidthII=S(2);
imshow(II);
zero_matrix = zeros(480-LengthII,640-WidthII);
III = [zero_matrix,II];
imshow(III)

回答(1 个)

Ameer Hamza
Ameer Hamza 2020-5-10
Use imcrop() and padarray()
I = imread('pears.png');
imshow(I);
g = drawrectangle;
II = imcrop(I, g.Position);
pad_size = [480 640]-size(II, [1 2]);
III = padarray(II, floor(pad_size/2), 'pre');
III = padarray(III, ceil(pad_size/2), 'post');
imshow(III)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by