How can I make the image processing faster for large number of images?

2 views (last 30 days)
Hi,
I am trying to obtain the position of the light stripe at each row. Below is an example for an image. However, identification of the light spots are performed for more than 2500 images, which takes about 10 hours. What would be the most efficient way to increase the speed of this calculation? I would be happy if you could guide me.
I = imread('C:\Users\img1.png');
img = I;
[height, width, dim] = size(I);
% Some image processing in between
% ...
img(img~=0)=1;
intline=nan;
p =1;
tic
for j=1:width
rows=[];
rows=find(img(:,j)==1);
if length(rows)>3 % spots detectable?
int=[];
for k=rows(1):rows(end)
int(k)=sum(double(I(k,j,1:3)));
end
y_px=find(int>=0.90*max(int));
if length(y_px)>2
cuts=[];
cuts=find(y_px(2:length(y_px))-y_px(1:length(y_px)-1)>10);
if cuts
cuts=cuts(end);
y_px=round(mean(y_px(cuts+1:end)));
else
y_px=round(mean(y_px));
end
else
y_px=y_px(end);
end
intline(j,1:2)=[j y_px];
p = p+1;
end
end
toc

Answers (1)

Walter Roberson
Walter Roberson on 7 Feb 2019
preallocate int and intline. You know the exact size of int at the for loop bounds. You do not know the exact size intline will turn out as but you know the maximum size so allocate that and keep track of what you use and truncate down to the used part afterward .
Do not assign [] to variables you then completely overwrite as it is a waste of time.
  8 Comments
Walter Roberson
Walter Roberson on 8 Feb 2019
It is not obvious to me that you want to scan by rows?? For example at about 1/3 of the way down, you have the streak at roughly 1/16 of the way across, but you have not yet reached the rows that have brightness at the far right, so the center of mass for that row would be the streak. But slightly further down where you also get the brightness at the far right, the center of mass of the row would be... ummm, about 60% of the way to the right or so? And slightly below that, the far right has dimmed but a second streak has started so the center of mass would be on the order of 1/10th of the way to the right. But this is all pretty vague and it is not obvious those lights are meaningful compared to the rest of the image, but your tests are relative to the maximum for that row, not relative to the maximum for the image. On the other hand, your logical mask is based on the red channel only, which would have only weak intensity in the green, and it gets tricky to predict visually what would be picked out.
It would seem to make more sense to be scanning column by column, which is what your current code does. img(:,j) is one column and you are looking for non-zero values in there and comparing per-column maxima that are going to include the fully-saturated white light in the middle rows. Visually the expected result would be close to a straight line across.
But you did not answer my question about whether the position information you are looking for is position relative to the first non-zero entries in the red channel in a given column, or if you are looking for absolute positions that can be easily compared column by column ? Your current code gives relative positions, so if your image had a huge shear that displaced the detectable light downwards by hundreds of pixels in part of your image, your code would not notice. The modification I talked about, int3 = sum(I .* repmat(img(:,:,1), 3) would be for working with absolute locations.
Image Analyst
Image Analyst on 8 Feb 2019
I agree with Walter. Please explain in words what "the position of the light stripe at each row" means, because if you want something like the centerline of that bright region, or the mean or center line (row) number of the line, I'd do something different. It might mean scanning across the image column by column or it could mean doing something else. It would help if you say what the next step would be assuming you had whatever it is you want here. Like "Once I have that, I will use it to..............."

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!