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

1 view (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:
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;
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);
hold on
CCDx(k) = px;
CCDy(k) = py;

Accepted Answer

Walter Roberson
Walter Roberson on 9 Apr 2021
bw = bwareafilt(imbinarize(croppedImage), [10 inf]);
Some of your images end up with very small extra regions, just a few pixels each.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!