Empty for loop index variable
4 views (last 30 days)
Show older comments
Abhilash Nair
on 7 Jan 2020
Commented: Abhilash Nair
on 8 Jan 2020
Hello everyone,
I am relatively new to Matlab and am using the R2015a version. I am trying to identify the lowermost boundary of a dark region which is spreading over a white background (Image attached for representation). The boundary is defined by spotting the intensity gradient in the image. Below is the code which has been working satisfactorily for me.
w=imread('C:\Users\Downloads\Spilt ink.jpg');
row_no=size(w,1); %image height
col_no=size(w,2); %image width
w_red=w(:,:,1);
count=0;
term=0;
axis on
subplot(1,2,1), imshow(w), title('Original linked image', 'FontSize', 14);
subplot(1,2,2), imshow(w_red), title('Image with line mask', 'FontSize', 14);
for i=(col_no-10):-1:2 %no. of columns (counted horizontally)
for j=(row_no-10):-1:2 %no. of rows (counted vertically)
if(abs(w_red(j,i)-w_red(j,i-1))>12)
count=count+1;
end
if(count>(0.05*row_no))
hold on
burnedImage = w;
% Creating line mask over the already existing second image
subplot(1, 2, 2);
hLine = imline(gca,[i i],[(row_no-10) 10]);
term=1;
end
if(term==1)
i
break % To get out of inner loop
end
end
count=0;
if(term==1)
break % To get out of upper for loop
end
end
I want to implement the same code for a video. So far, I have extracted the individual frames from the video and I use the code above to display images of a line burned into the original frame- which didn't happen. When I tried troubleshooting the code, I noticed this.
Name Size Bytes Class Attributes
i 0x0 0 double
The variable i (now inside another for loop meant for iterating frames) is completely blank, which never happened in the previous code.
This is what the already mentioned code looks like inside the loop. Kindly note that this a truncated part of the code.
for frame = 1 : numberOfFrames
thisFrame = read(videoObject, frame);
hImage = subplot(1, 2, 2);
w=image(thisFrame);
row_no=size(w,1); %image height
col_no=size(w,2); %image width
w_red=w(:,:,1);
count=0;
term=0;
for i=(col_no-10):-1:2 %no. of columns (counted horizontally)
for j=(row_no-10):-1:2 %no. of rows (counted vertically)
if(abs(w_red(j,i)-w_red(j,i-1))>12)
count=count+1;
end
if(count>(0.05*row_no))
hold on
burnedImage = w;
% Creating line mask over the already existing second image
subplot(1, 2, 2);
hLine = imline(gca,[i i],[(row_no-10) 10]);
term=1;
end
if(term==1)
i
break % To get out of inner loop
end
end
count=0;
if(term==1)
break % To get out of upper for loop
end
end
The remainder of the code is derived from an existing program linked here (https://in.mathworks.com/matlabcentral/fileexchange/47726-video-processing-tutorial)
I can't point where I'm going wrong, hence would really appreciate some help.
2 Comments
Guillaume
on 7 Jan 2020
Obvious question: What is the value of col_no when the code fails. If it's less than 12, i will indeed be empty.
On the other hand, the double loop is completely unneeded. You can replace the whole lot with:
%Note: In the diff call below, I'm using the same bounds as your for loops. It's not clear why the first row and last 10 rows or the last 10 columns are ignored
gradient = diff(w_red(2:end-10, 1:end-10), [], 2); %difference betweeen pixel and preceding one in each row
abovethreshold = sum(abs(gradient) > 12) > 0.05 * size(w, 1);
selectedcolumn = find(abovethreshold, 1, 'last') + 1; %only want the last column that is above threshold
%draw the line as you did
subplot(1, 2, 2);
hline = imline(gca, [selectedcolumn, selectedcolumn], [size(w, 1) - 10, 10]);
Accepted Answer
Walter Roberson
on 8 Jan 2020
Just before title('Redscale image','FontSize', 14); you switch to subplot axes 1. Then you imshow(w_red); which draws an image in that subplot 1 axes. Then
if(count>(0.05*row_no))
hold on
so you turn hold on for subplot 1 axes.
Now, inside the double nested for loop over rows and columns, you do not create any additional images in subplot 1 axes. But eventually you finish those two loops, and you proceed to the next for frame iteration, where you do
himage=subplot(1, 2, 1);
title('Redscale image','FontSize', 14);
imshow(w_red);
so you switch to subplot 1 axes, and you imshow() on that axes. But hold is still on for that axes, so now you will have two images in the axes -- the one for frame #1, and the one for frame #2. Now imline gets confused about which image you want to draw the line for.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!