how to store(imwrite) all the extracted coin images in Labeling labeled objects .

1 view (last 30 days)
aa=imread('https://upload.wikimedia.org/wikipedia/commons/0/0e/Acute_leukemia-ALL.jpg');
II=rgb2gray(aa);
T=50;
I = im2bw(II,T/255);
figure, imshow(I)
%B=im2bw(I);
%figure,imshow(B);
%title('Binary Image');
C=imfill(I,'holes');
figure,imshow(C);
label=bwlabel(C);
%TT=(graythresh(label) ~=100);
g=max(max(label))
%im1= (label==1);
%figure,imshow(im1);
%figure,imshow(label==6);
for j=1:max(max(label));
[row, col] = find(label==j);
len=max(row)-min(row)+2
breadth=max(col)-min(col)+2
target=uint8(zeros([len breadth]));
sy=min(col)-1;
sx=min(row)-1;
for i=1:size(row,1)
x=row(i,1)-sx;
y=col(i,1)-sy;
target(x,y)=rgbImage(row(i,1),col(i,1));
end
mytitle=strcat('Object Number:',num2str(j));
figure,imshow(target);
title(mytitle);
%subplot((g/3),3,j),imshow(target);
s=regionprops(label==j,'area','perimeter','Solidity')
title([num2str(j)]);
end

Answers (1)

DGM
DGM on 12 Oct 2024
At first I thought the code just needed a little cleanup and debugging, but the more I looked at it, I'm thinking it's just a collage of code from somewhere else. The image is obviously not "coins". The objects in the image are easily separable by color, but color is discarded. The result is quite specifically thresholded at a completely inappropriate level and at the wrong polarity, so there is no binary image. The variable rgbImage doesn't exist, and if it were an RGB image, it's not being addressed appropriately. Everything else is a bunch of repeated and unused operations.
I'm going to ignore the given code and just interpret the title itself.
% a single-channel image
inpict = imread('coins.png');
% reduce it to a logical mask
% imbinarize() was available in early 2016,
% but im2bw() will also accept this syntax
mask = imbinarize(inpict);
% clean the mask
mask = bwareafilt(mask,25);
mask = imfill(mask,'holes');
%imshow(mask)
% save a pile of tiny images that were
% probably not actually useful for anything
S = regionprops(mask,'boundingbox');
for k = 1:numel(S)
thisobject = imcrop(inpict,S(k).BoundingBox);
thisname = sprintf('object_%03d.png',k);
imwrite(thisobject,thisname)
end
... or something like that.

Community Treasure Hunt

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

Start Hunting!