How to get the value from Structure array ?

Dear Matlab experts,
I need some help from someone.
I want to get the location value from the structure array (stat). Then use them to crop image with the size is 64x64.
This is my step:
  1. Detect the centroid of image
2. Use the value of centroid location and crop the original to many 64x64 image.( this step i stuck because i don't know how to get the
value of centroid location).
My code:
% Step 1 centroid detection
I=imread("2_mask.png")
Ibw = im2bw(I);
stat = regionprops(Ibw,'centroid');
imshow(I); hold on;
for x = 1: numel(stat)
plot(stat(x).Centroid(1),stat(x).Centroid(2),'+',LineWidth=2);
end
hold off
%Step 2 crop image
I2 = imcrop(I,[??-32 ??-32 63 63])
This is the structure array value of centroid location.
This is what i want to do
Thanks
Han

 채택된 답변

Matt J
Matt J 2022년 5월 13일
편집: Matt J 2022년 5월 13일
I=imread("https://www.mathworks.com/matlabcentral/answers/uploaded_files/996660/2_mask.png");
Ibw = im2bw(I);
stat = regionprops(Ibw,'Image');
for i=1:numel(stat)
I=stat(i).Image;
I(end+1:64,end+1:64)=0;
s=regionprops(I,'Centroid');
I=imtranslate(I,32-s.Centroid);
if ~isequal(size(I),[64,64])
win = centerCropWindow2d(size(I),[64,64]);
I=imcrop(I,win);
end
stat(i).Image=I;
end
montage({stat.Image},'Back','w','Bor',5)

댓글 수: 1

Jenifer NG
Jenifer NG 2022년 5월 13일
편집: Jenifer NG 2022년 5월 13일
Thanks you so much.
I want to use the centroid location in this image to crop another image then save the cropped area to separate file.
Could you help me to modify this code?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2022년 5월 13일
I want to use the centroid location in this image to crop another image then save the cropped area to separate file.
I=imread("https://www.mathworks.com/matlabcentral/answers/uploaded_files/996660/2_mask.png");
Ibw = im2bw(I);
stat = regionprops(Ibw,'Centroid');
N=numel(stat);
for i=1:N
stat(i).BoundingBox=[stat(i).Centroid-32.5, 64,64];
end
And now you can extract the cropped subimages from an arbitrary image with the following, although I use the original image Ibw just for illustration.
subimages=arrayfun(@(s)imcrop(Ibw,s.BoundingBox), stat,'uni',0); %You can use a new image here
montage(subimages,'Back','w','Bor',5)

댓글 수: 5

Jenifer NG
Jenifer NG 2022년 5월 13일
편집: Jenifer NG 2022년 5월 13일
Wow it work fine.
Thanks you
how to save 9 cropped areas to 9 image files ?
Matt J
Matt J 2022년 5월 13일
편집: Matt J 2022년 5월 13일
Why not save them all to the same file, as follows?
save myFile subimages
Jenifer NG
Jenifer NG 2022년 5월 13일
편집: Jenifer NG 2022년 5월 13일
I mean save to 'JPG' file. by using imwrite but i dont know how to use
Jenifer NG
Jenifer NG 2022년 5월 13일
편집: Jenifer NG 2022년 5월 13일
Dear Matt J,
I am using the function to convert subimage from array to mat (array2mat) then i can save the cropped image by using imwrite and the code as bellow . Could you advise me to modify the code ?
clear all
I=imread("https://www.mathworks.com/matlabcentral/answers/uploaded_files/996660/2_mask.png");
Ibw = im2bw(I);
stat = regionprops(Ibw,'Centroid');
N=numel(stat);
for i=1:N
stat(i).BoundingBox=[stat(i).Centroid-32.5, 63,63];
end
I2=imread("2.png")
subimages=arrayfun(@(s)imcrop(I2,s.BoundingBox), stat,'uni',0); %You can use a new image here
montage(subimages,'Back','w','Bor',5)
for k = 1:N
b{k}=cell2mat(subimages(k,1))
filename=sprintf('file%d.jpg',k)
imwrite(b{k},filename)
end
Thanks
Matt J
Matt J 2022년 5월 13일
Why is modification necessary?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 5월 13일

댓글:

2022년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by