change an image from rectangular to square by adding white area

Dears, i have a dataset that im working on but all of them in rectangular shape, i wand them to be square by adding white area ( raws or coloumns depending on the image) many thanks in advanced

2 件のコメント

Rik
Rik 2022 年 11 月 17 日
What have you tried so far? This type of padding should not be too hard. Note that the value that you need to add may depend on the data type and value range of the image.
hamsa dhia
hamsa dhia 2022 年 11 月 17 日
編集済み: hamsa dhia 2022 年 11 月 17 日
i tried this code but it add black margin to the image , i need to fill the lack of pixels in white not black please
im = imread('1.jpg');
nrows = size(im,1);
ncols = size(im,2);
d = abs(ncols-nrows); % difference between ncols and nrows:
if(mod(d,2) == 1) % if difference is an odd number
if (ncols > nrows) % we add a row at the end
im = [im; zeros(1, ncols)];
nrows = nrows + 1;
else % we add a col at the end
im = [im zeros(nrows, 1)];
ncols = ncols + 1;
end
end
if ncols > nrows
im = padarray(im, [(ncols-nrows)/2 0]);
else
im = padarray(im, [0 (nrows-ncols)/2]);
end
figure; imshow(im)

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

回答 (2 件)

Rik
Rik 2022 年 11 月 17 日
編集済み: Rik 2022 年 11 月 17 日

1 投票

The documentation for padarray states it will pad with 0. If you don't want that, you will have to provide the value as the third argument. You probably want 255, but you can use the code below to determine that dynamically.
switch class(im)
case 'uint8'
padval = uint8(255);
case 'double'
padval = 1;
otherwise
error('check what value you need')
end
Edit: a much better solution that this code is to use the function that @DGM showed in his answer: getrangefromclass.
range = getrangefromclass(im)
padval = range(2);

3 件のコメント

hamsa dhia
hamsa dhia 2022 年 11 月 17 日
unfortuenatly not working , the black are still there i want them white
Rik
Rik 2022 年 11 月 17 日
What intensity value does the white part have in your image?
hamsa dhia
hamsa dhia 2022 年 11 月 17 日
still not working :(

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

DGM
DGM 2022 年 11 月 17 日
編集済み: DGM 2022 年 11 月 17 日
I don't normally use getrangefromclass(), but that's one way to get the white value. Of course, it's impossible to tell that the result is padded here because it's displayed on a white background
inpict = imread('peppers.png');
%inpict = rot90(inpict); % will pad either dimension
outpict = squarewhitematting(inpict);
imshow(outpict)
function outpict = squarewhitematting(inpict)
% add symmetric white padding to the smaller dimension of the image geometry
% so that the resulting image is square
% INPICT is an image of any standard image class, with any number of channels.
% multiframe images are supported
% get nominal black and white value
valuerange = getrangefromclass(inpict);
[h,w,~] = size(inpict);
if h == w
% nothing to do
outpict = inpict;
return;
else
if w > h % wide image; pad top/bot
padamt = [(w-h)/2 0];
else % tall image; pad l/r
padamt = [0 (h-w)/2];
end
% use floor/ceil to deal with odd differences
outpict = padarray(inpict,floor(padamt),valuerange(2),'pre');
outpict = padarray(outpict,ceil(padamt),valuerange(2),'post');
end
end

4 件のコメント

Rik
Rik 2022 年 11 月 17 日
If you use this code instead of imshow you can verify that there is a white border added.
image(outpict),daspect([1 1 1])
DGM
DGM 2022 年 11 月 17 日
That's a good point
hamsa dhia
hamsa dhia 2022 年 11 月 17 日
this line gives error
image(outpict),daspect([1 1 1])
this line is worked
imshow(outpict)
thank you so much for you both
Rik
Rik 2022 年 11 月 17 日
Something strange is going on if that line doesn't work. image is the primitive that is called by imshow (just like plot calls line and surf calls patch).
But if you have a working solution now, that should do it. There is no fundamental difference between the code I suggested and what DGM implemented (other than that DGM provided you with a full implementation).

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

カテゴリ

ヘルプ センター および File ExchangeConvert Image Type についてさらに検索

質問済み:

2022 年 11 月 17 日

コメント済み:

Rik
2022 年 11 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by