How to find and store each spot center position in the loop.

3 views (last 30 days)
Hello, I would like to seek your help. I have several image signal, and would like to find and stored each spot center position in the loop. I tried to write a code as below, but I got the error message "
s =
3×1 struct array with fields:
Centroid
Expected one output from a curly brace or dot indexing expression, but there
were 3 results.
My code as below,
close all; clear all; clc
%%---------Open image-------
D = 'C:\Users\Admin\Desktop';
S = dir(fullfile(D,'*.bmp')); % pattern to match filenames.
CCDx = 1:numel(S)-1;
CCDy = 1:numel(S)-1;
for k = 1:numel(S)-1
F = fullfile(D,S(k).name);
I = imread(F);
% figure(),imshow(I);
%%-------open image BG file----
S_BG = dir(fullfile(D,'BG1.bmp')); % pattern to match filenames.
for k_BG = 1:numel(S_BG)
F_BG = fullfile(D,S_BG(k_BG).name);
I_BG = imread(F_BG);
% figure(100),imshow(I_BG)
%%%---convert data type from RGB to gray-----
Z = im2double(I);
Z = rgb2gray(Z);
Z_BG = im2double(I_BG);
Z_BG = rgb2gray(Z_BG);
% figure(200),imshow(Z_BG)
%%%------cut background-------
Z_net = Z-Z_BG;
%%%-----croppedImage--------
leftColumn=115; % x axis
width = 100;
topLine = 180; % y axis
height = 100;
croppedImage = imcrop(Z_net, [leftColumn, topLine, width, height]);
bw = imbinarize(croppedImage);
s = regionprops(bw,'Centroid')
%%%-------stored and save position for each image----------
px = s.Centroid(1);
py = s.Centroid(2);
figure()
imshow(croppedImage,'InitialMagnification','fit')
hold on
plot(s.Centroid(1),s.Centroid(2),'b*')
CCDx(k) = px;
CCDy(k) = py;
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 9 Apr 2021
Use
bw = bwareafilt(imbinarize(croppedImage), [10 inf]);
Some of your images end up with very small extra regions, just a few pixels each.
  3 Comments
Walter Roberson
Walter Roberson on 10 Apr 2021
One of your images (not one you attached above) has an extra blob that is 10 or more pixels.
If you change to
bw = bwareafilt(imbinarize(croppedImage), 1);
then it will extract only the largest blob, but I am concerned about the possibility that in time one of your images might have two non-trivial blobs that you need to take into account. I think you should put in a conditional breakpoint on the assignment to px, to stop if numel(s) > 1, at which point you should examine the image stored in bw and determine whether the extra blobs are significant or not. You could consider raising the [10 inf] to something larger to filter out more small blobs, but you should be having a closer look at the data before you determine whether it is wise.
You could consider using imclose() with a structuring element in order to join nearby blobs together. But I still worry that at some point there might be multiple significant blobs, and that you should plan your code for that possibility.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!