Resize image to a square centred on a certain pixel

13 vues (au cours des 30 derniers jours)
Harry Cantor
Harry Cantor le 9 Mar 2015
Commenté : Image Analyst le 9 Mar 2015
Hi,
I have a camera that captures images with a size of 960 by 1280 pixels. I wanted to resize this image so that it is squared on the pixel of highest value, and has dimensions of 960 by 960. Could anyone suggest any codes to achieve this? The image format I am using is .bmp if that helps.
Thanks
Harry
  2 commentaires
David Young
David Young le 9 Mar 2015
Modifié(e) : David Young le 9 Mar 2015
It will help with making a good answer if you could clarify a couple of points.
  • What do you mean by highest value? Presumably your image is a colour image, so each pixel has r, g and b values. Do you mean the pixel that has the highest sum, r+b+g, or the highest g, or some other measure?
  • What do you want to do about the fact that the square will go outside the original image? That is, suppose the pixel with the highest value is at x=100, y=100. A square centred on this will have a corner at x=-330, y=-330. Do you want to fill the undefined pixels with zeros or something, or resample the image so that a 960x960 square fits in, or something else?
By the way, it doesn't matter how your image is stored on disk. You just use imread to read it into an array in main memory and work on that.
Harry Cantor
Harry Cantor le 9 Mar 2015
I have converted the image to a grayscale image, so I want it to be centred on the pixel with the highest value on that scale.
I hadn't thought of your second point about if it goes outside the image. I have tried my best to get the image roughly central, so maybe to avoid this happening I will try making the square a bit smaller. Say 600x600. If it does extend beyond the limits of my image then I would fill it in with zeros.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 9 Mar 2015
Find the location first. Remember that there may be more than one pixel with the max value, but I assume you can figure this part out. Then, assuming get the edges. Let's say you want 960 by 960 centered on (y,x) = (rowMax, colMax).
[rows, columns, numberOfColorChannels] = size(grayImage);
width = 960;
halfWidth = width /2;
row1 = rowMax - halfWidth;
row2 = row1 + width - 1;
col1 = colMax - halfWidth;
col2 = col1 + width - 1;
if row1 < 1
row1 = 1;
end
if row2 > rows
row2 = rows;
end
if col1 < 1
col1 = 1;
end
if col2 > columns
col2 = columns;
end
newImage = grayImage(row1:row2, col1:col2);
  2 commentaires
Harry Cantor
Harry Cantor le 9 Mar 2015
Thanks that works really well!
Image Analyst
Image Analyst le 9 Mar 2015
Great. Not bad for untested, off the top of my head code. Thanks for Accepting the answer.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by