Reading Multiple Images Using For Loop

Hi,
I am trying to use a for loop to read 4 images (of the format 1.jpg,2.jpg etc...) and then calculate the value and location of the minimum pixel of each image. Below is the code I have so far:
RGB=cell(1,4);
for i=1:4
RGB{i}=imread(sprintf('%d.jpg',i)); %Converting the image in to a RGB matrix
gray{i}=rgb2gray(RGB{i}); %Converting the RGB image to grayscale
gray_double{i}=double(gray{i}); %Changing from uint8 format to double
Min_pixel{i}=min(gray_double{i}(:)); %Finding the minimum pixel value
[row, column] = find(gray_double == Min_pixel,1);%Finding location of first minimum
end
The code works up until Line 8 where it struggles to obtain the minimum pixel value. Is there an easier way to do this?
Once again, any help would be much appreciated!
Thank you

 Accepted Answer

dpb
dpb on 25 Oct 2020
Edited: dpb on 25 Oct 2020
RGB=cell(4,1);
gray=cell(4,1);
Min_loc=cell(4,1);
for i=1:4
RGB{i}=imread(sprintf('%d.jpg',i));
gray{i}=rgb2gray(RGB{i}); % Convert the RGB image to grayscale
[Min_pixel{i},i_loc]=min(gray{i}(:)); % Find the minimum pixel value, location of first
[r,c]=ind2sub(size(gray{i}),i_loc); % Convert linear index to row, column
Min_loc{i}=[r,c]; % return to cell array for image
end
There's no need to convert to double(); integer values will be the same as floats as as integers.
Just let min return the location for you using optional second output argument.

6 Comments

Thank you for your response, I got the following error message in Line 9 when I tried to run the code:
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Missing the "=" operator...
Min_loc{i}=[r,c];
Works perfectly, thank you very much!
If I then wanted to find the distance between the rows and columns (i.e. row2-row1,column2-column1 etc...) in subsequent images, how would that work? I've tried inserting another for loop, but it doesn't work.
Ultimately I want to track how far (in terms of pixels) the minimum pixel has moved in the x-y plane from image to image using coordinate geometry.
RGB=cell(4,1);
gray=cell(4,1);
Min_loc=cell(4,1);
for i=1:4
RGB{i}=imread(sprintf('%d.jpg',i));
gray{i}=rgb2gray(RGB{i}); % Convert the RGB image to grayscale
[Min_pixel{i},i_loc]=min(gray{i}(:)); % Find the minimum pixel value, location of first
[r,c]=ind2sub(size(gray{i}),i_loc); % Convert linear index to row, column
Min_loc{i}=[r,c];
for i=1:3
Distance(i)=r(i+1)-r(i)
end
end
Again, many thanks for your help, it is much appreciated indeed.
dpb
dpb on 25 Oct 2020
Edited: dpb on 25 Oct 2020
You couldn't execute that loop until after the first is done. Plus, you've reused i for a second loop index inside the first loop.
You could, compare each in turn to the previous inside the loop if you only begin the comparison on the second time through the loop.
NB: What you're locating here is the FIRST minimum in the image in column-major order -- presuming that there's necessarily a correlation between that location in subsequent images that relate to some specific feature is a stretch/major assumption of how the images may change.
Also, what is returned for the location is [r,c], the row/column of the location for the minimum. You would need to look at both row and column; the difference between them would be how many rows/columns difference there is between images but a distance would need be the vector magnitude between those points.
This again, however, still assumes that the location that is found searching serially through the image has some relationship to each location on the subsequent images. That still seems a strong assumption to make.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 25 Oct 2020

Commented:

dpb
on 25 Oct 2020

Community Treasure Hunt

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

Start Hunting!