How to analyze one image 100 times using for loop?
1 view (last 30 days)
Show older comments
Warid Islam
on 5 Sep 2019
Commented: Walter Roberson
on 13 Dec 2020
Hello,
I have an image. I want to divide the image into 100 frames and analyze each frame separately. Please find my code below:
img = imread('1F1.jpg');
k = 1;
for j = 1: 100
dblSubtractedImage = img(j) - img(k);
s = dblSubtractedImage*5;
L = mat2gray(s);
imshow(L);
im = imclearborder(255-imclearborder(255-s));
hc = sum(im,2);
mask = hc >= 14500;
fr = find(mask, 1, 'first');
lr = find(mask, 1, 'last');
cr = im(fr:lr, :);
D = std2(cr);
meanIntensityValue = mean2(cr);
% imshow(cr)
K = mat2gray(cr);
min_image = min(K(:));
max_image = max(K(:));
% figure
imshow(K)
[rows, columns, numberOfColorBands] = size(K);
if numberOfColorBands > 1
grayImage = K(:, :, 2);
end
binaryImage = grayImage~= 40;
area2 = sum( binaryImage,'all');
end
AverageArea = (sum(area2))/100;
AverageStandardDeviation = (sum(D))/100;
AverageIntensityValue = (sum(meanIntensityValue))/100;
The first frame is used as a reference. The first frame is subtracted from all the other frames subsequently. The resulting images are then masked using a threshold which is shown above. Few other parameters such as the mean, standard deviation and the area of each of the resulting image are found. At the end of the for loop, the average value of the parameters above are found. However, I am getting the error message below. Any help would be appreciated. Thank you.
>> f
Index exceeds the number of array elements (0).
Error in mat2gray (line 38)
if limits(2)==limits(1) % Constant Image
Error in f (line 17)
K = mat2gray(cr);
4 Comments
Accepted Answer
Gaurav Garg
on 17 Sep 2019
Hi,
I understand that you wish to analyze each frame separately. However, there’s some problem in the code you have written.
In line 4, you subtract the 2 images and store the output in sblSubtractedImage. Now, when you display sblSubtractedImage, you get 0 as output. Hence, the variables you use thereafter are also 0 and variable cr stores a 0 ×1 empty uint8 column vector. And therefore, you get error while using mat2gray.
What is happening in your case is, you have saved the file normally (using Windows option to save file) and you are reading subtracting first 2 pixels which are both white. However, if you use imwrite (MATLAB function) to save the image you will get the desired portion and this should solve your error.
2 Comments
Saher77
on 13 Dec 2020
I am doing my first experiment in deep learning. I tried some code where I am unable an error using mat2gray. Here's my code
%Extract training features using CNN
%get the network weights for the 2nd convolutional layer
w1=net.Layers(2).Weights;
%scale and resize the weights for visualization
w1=mat2gray(w1);
w1=imresize(w1,5);
Error is :
Index exceeds the number of array elements (0).
Error in mat2gray (line 38)
if limits(2)==limits(1) % Constant Image
Error in trial1 (line 63)
w1=mat2gray(w1);
Can anyone help me get out o this?
Walter Roberson
on 13 Dec 2020
You would get that error under the circumstance that net.Layers(2).Weights was empty -- in such a case w1 would be empty, and the attempt to find min() and max() of the array would come out empty.
You can fix the immediate problem by using
w1 = rescale(w1);
as rescale() does not give an error message when the input is empty.
... After that you would get an error in the imresize() because imresize() does not work on empty arrays. But you would have fixed the mat2gray() problem.
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!