How to know maximal x and y cordinate in image
Afficher commentaires plus anciens
Hi I have an image,I want to find x and y coordinate of the maximum value in pixel , how can i do that? Thanks a lot before
7 commentaires
Walter Roberson
le 19 Juin 2019
images are stored in MATLAB as arrays, either two dimensional or three dimensional (color)
pixels are a display matter. You might have displayed a 640 x 480 image in an axes area that is (say) 960 x 720, in which case each array element becomes represented as about 1.5 pixels on the screen. And you might have zoomed in to a portion of the image. A single source array element might be replicated into multiple display pixels.
Could you confirm that your question is to examine the display to find the current image, and figure out what the zoom factor is, and what the screen resolution is, and to figure out which screen pixel holds the maximum value (out of all of the ones currently being displayed for the image) ?
The matter is also complicated by the processing of High DPI displays for Windows and Mac, for which the unit "pixel" is defined as 1/72 physical inches for Mac and 1/96 physical inches for Windows, so you also have to do a conversion between MATLAB pixel coordinates and screen pixel coordinates.
Once you have the information about which screen pixel holds the maximum value, what are you going to do with it?
Might I suggest that you would be a lot more likely to care about the row and column coordinates of the maximum of the array that is being sent to display?
Sipher17
le 19 Juin 2019
KALYAN ACHARJYA
le 19 Juin 2019
Modifié(e) : KALYAN ACHARJYA
le 19 Juin 2019
@Sipher In RGB Image, every pixel has 3 values, such as R,G,B, in what condition you decide the value of pixel, is it average of all or some specific?
If image is gray, you can easily do that, there may be multiple maximum same values?
Clarify?
Sipher17
le 19 Juin 2019
Walter Roberson
le 19 Juin 2019
"When i get the maximum pixel i need for x and y cordinate, then i will check their RGB for each cordinate serially,"
You would need to do a screen capture to read out the rgb value. Remember that pixels are display hardware output, not the array inputs that you are sending to image() or imagesc() or imshow().
Image Analyst
le 20 Juin 2019
Sipher, I thought I gave a pretty thorough answer below. Did you ever actually scroll down to the Answers section below to look at any answers, or have you just been looking up here at comments?
Walter Roberson
le 20 Juin 2019
What do you want to do if the location with maximum green contribution is not the same as the location with maximum red contribution. For example, if there is one pixel that is (255,0,0) and a different pixel that is (0,255,0) then which of the two is the "maximum" ?
I would remind you again that the pixels are hardware display items, and that the values stored in your arrays that you are sending to the display routines might be quite different. For example if you have an array which is alternating red and green pixel columns, and you send it to be displayed to a window that has only half as many pixels wide as the array has columns, then the display hardware would have to average the adjacent red and green array elements into single pixels, which would give you a yellow display. You would find that that yellow was the "maximum" pixel by taking a screen capture and examining the screen capture, but the yellow that you pick out that way has only an indirect relationship to the array elements you asked to be displayed.
Réponses (2)
drummer
le 19 Juin 2019
Hi Sipher.
The example below is for a simple 3x3 matrix. But you can change to your image instead (as an image is a matrix).
The fprintf part just shows the ouput of the coordinates in the command window.
clc, clear all;
image = [2 3 1; 5 4 8; 10 11 20];
[x, y] = size(image);
max_value = max(image(:))
for i=1:x
for j=1:y
if(image(i,j)== max_value)
i;
j;
end
end
end
fprintf('The maximum value in the matrix is %d\n', max_value);
fprintf('The x cordinate is %d', i);
fprintf(' and the y-coordinate is %d\n', j);
The maximum value is 20, located at image(3,3).
Cheers
1 commentaire
Adam
le 19 Juin 2019
You would need a break in your if statement in that example, else it would not print the correct coordinates if the maximum value where anywhere other than the last element. Also don't call a variable image as it over-rides the very important function of the same name.
[~,idx] = max( img(:) );
[y,x] = ind2sub( [size( img)], idx );
should work fine.
Image Analyst
le 19 Juin 2019
To get the maximum pixel coordinate for an image, that's simply the number of rows and columns.
[rows, columns, numberOfColorChannels] = size(yourImage);
The min is just simply 1.
To see the coordinates and image values as you mouse around, use impixelinfo:
hp = impixelinfo;
You can set the position property of hp to move it to a location on the figure you like, otherwise it's at the lower left.
To spatially calibrate your image in real world units instead of pixels you can use xdata and ydata properties in imshow().
grayImage = imread('moon.tif');
imshow(grayImage, 'XData', 1:2777, 'YData', 1:4166);
axis('on', 'image');
% Display coordinates and image intensity/color as you move the cursor over the image:
hp = impixelinfo;
hp.Units = 'Normalized';
hp.Position = [.01, .95, 0.4, 0.03];
You can also see my attach spatial calibration demo where you can measure things in pixels and then multiply measurements by a spatial calibration factor that you create by drawing a distance in your image and telling it how many real world units long it is.
Catégories
En savoir plus sur Computer Vision Toolbox dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!