How can I make the image processing faster for large number of images?
Show older comments
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
1 Comment
Matt Gaidica
on 7 Feb 2019
Can you attach the actual image? More comments in your code would be nice too.
Answers (1)
Walter Roberson
on 7 Feb 2019
0 votes
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
on 7 Feb 2019
some of your operations can be vectorized . For example sum() along the third dimension and store in a matrix and access the result as needed .
Sometimes it is much more efficient to calculate over an entire matrix and ignore parts that you do not need rather than calculating only on the sections you need.
Get rid of unneeded operations . You can use
img = logical(I)
instead of explicit test for nonzero. then do not find(img(:,j)==1) just find(img(:,j)) Or skip img and just find(I(:,j)) since the test is automatically for nonzero .
tethys
on 7 Feb 2019
Walter Roberson
on 7 Feb 2019
accumarray is not relevant here . summing over the third dimension of all of I and accessing appropriate parts would improve performance .
You can do better still .
int3 = sum(I .* repmat(img(:,:,1), 3)
and now any column jj that at has at least 3 dots in, int = int3(:,jj) will act nearly the same as your current int but possibly better . Your current intline records locations calculated relative to the first nonzero pixel in the column where my modification would most naturally calculate relative to the beginning of the column . The meaning of your calculation is not documented so it is difficult to say which is better .
question for you: if one column had a particular pattern of pixels and the next column had the same pattern but 100 pixels further down then are you expecting the same y_px result for both (only pattern is important) or a difference (location of pattern affects output )? Your current code would produce the same output .
Image Analyst
on 7 Feb 2019
Why not sum the image before any of the for loops, then just access it
sumImage = sum(double(I), 3);
for.....
int(k)=s sumImage(k,j);
And please attach an image so we can see if there are built-in functions to do all of this.
And add comments to your code because it looks like you're "position of the light stripe at each column" rather than "position of the light stripe at each row." so it would help if you added comments to each line or block of code. Professional programmers (like me) do that - not everyone is as clairvoyant as Walter.
tethys
on 7 Feb 2019
Image Analyst
on 8 Feb 2019
This is easily solved if you don't saturate your image and use regionprops() and ask for 'WeightedCentroid'.
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
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..............."
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!