Get coordinates of points from an image

16 views (last 30 days)
Hello,
I would like to know how to obtain the coordinates of p number of random points from an image:
  • how to call an image ( do i have to specify the path of the folder and how?)
  • How to obtain the data of x axix and y axis corresponding to it
Shall i use the function getimage?
Thank you

Accepted Answer

Image Analyst
Image Analyst on 24 Dec 2020
Try this:
[rows, columns, numberofColorChannels] = size(yourImage);
randomRows = randi(rows, p);
randomColumns = randi(columns, p);
Not sure what "call an image" means, but perhaps you mean imread():
yourImage = imread(fileName);
  3 Comments
Image Analyst
Image Analyst on 24 Dec 2020
Do not use image as the name of a variable. It's a built-in function that you don't want to destroy.
Try
[rows, columns, numberofColorChannels] = size(yourImage);
[R, C] = meshgrid(1:rows, 1:columns); % R and C are 2-D matrices.
% To get a list of (row, column), turn into column vectors and stitch:
RC = [R(:), C(:)]; % Has all possible coordinates in the image.
Image Analyst
Image Analyst on 24 Dec 2020
I think your "Answer" below is actually a comment to my Answer.
Yes, you can use either reshape(array, [], 1) or (:) to turn any array into a column vector. A developer at Mathworks told me that useing (:) is efficient and actually does not allocate another chunk of memory for the array. Of course if you store in in a new variable, then it will. So
m = mean(array(:))
will not create any temporary or additional memory (other than 8 bytes for "m"). But
v = array(:);
will because you now have another variable "v" that has the same number of bytes as "array".
Just a note that many people forget, matrices are not indexed like array(x, y). They are indexed array(y, x) which is array(row, column). So specifically used R and C, instead of X and Y like many people would use, so you would know to use it the correct way. So just watch out for that constantly as you use MATLAB. Some functions use or return (row, column), like find(), and some functions like (x,y), like plot().
So if my answer worked (like you said it did), please click the "Accept this Answer" link. Thanks in advance.

Sign in to comment.

More Answers (1)

Matthew Worker
Matthew Worker on 24 Dec 2020
It works!
I have also tried to reshape the matrix C.
Thank you

Community Treasure Hunt

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

Start Hunting!