How to find and store each spot center position in the loop.
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2021-4-9
Use
bw = bwareafilt(imbinarize(croppedImage), [10 inf]);
Some of your images end up with very small extra regions, just a few pixels each.
3 个评论
Walter Roberson
2021-4-10
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!