Add a colum and a row of blank (white) values in an Image

6 ビュー (過去 30 日間)
Dimitris M
Dimitris M 2012 年 6 月 4 日
Hello
I was thinking what would be the easiest way to add an extra column and row with values (255) in my image. I was thinking that is possible by reading the image size and adding an extra value to the dimensions but is there a easier and faster way ?
Thank you in advance

採用された回答

Dimitris M
Dimitris M 2012 年 6 月 6 日
Hello
Thank you very much for the info ! I managed to do it in an easier way using matlab's function padarray() , where you can also specify the value you want to add !
  1 件のコメント
Image Analyst
Image Analyst 2012 年 6 月 6 日
That's what I thought of first but then I thought you meant that the extra row be "added" so I didn't mention padarray because that puts an extra layer all around - it doesn't just increase the dimension size by 1.

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

その他の回答 (2 件)

Geoff
Geoff 2012 年 6 月 4 日
Your best options (as far as I know) are:
1. Resize the image twice (once for new row, once for new column)
im(end+1, :) = 255; % new row at end
im(:, end+1) = 255; % new column at end
2. Create a new image, and copy your subimage in.
im1 = 255 * ones(size(im)+1);
im1( 1:end-1, 1:end-1 ) = im;
% Or, if large image, avoid unnecessary multiplications
im1 = zeros(size(im)+1);
im1( 1:end-1, 1:end-1 ) = im;
im1( end, : ) = 255;
im1( :, end ) = 255;
If adding data at the end, I would probably opt for (1). At worst, MatLab will create a new one and copy the data. At best, it'll expand the memory block and efficiently shuffle your data correctly. Otherwise, if you need to add rows/columns anywhere but the end, I would opt for (2).

Image Analyst
Image Analyst 2012 年 6 月 4 日
Try this little demo:
yourImage = magic(6) % Just a demo image.
[rows columns numberOfColorChannels] = size(yourImage);
if numberOfColorChannels == 1
% Monochrome (grayscale) image.
yourImage(rows+1, :) = 255
yourImage(:, columns+1) = 255
else
% For a color image. I think this should work but I didn't test it.
for c = 1 : numberOfColorChannels
yourImage(rows+1, :, c) = 255
yourImage(:, columns+1, c) = 255
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by