MATLAB: How to write the name of Objects in the image after recognition of them.

digital image processingimage processingMATLABobject detection

Hello Every one! I hope you are doing well.
I have written a code to detect coins in an image, and at last I want to write a text on each of these objects (coins). But I confront error. can anyone help me please. the code I have written is here:
pic = imread('coins.png');
I = rgb2gray(pic);
imshow(I)
bw = imbinarize(I);
figure; imshow(bw)
bw = bwareaopen(bw, 500);
figure; imshow(bw)
se = strel('disk', 2);
bw = imclose(bw, se);
bw = imfill (bw, 'holes');
figure; imshow(bw);
[r, n] = bwlabel(bw);
prop = regionprops(bw,{'Area','centroid'});
%props =struct2table(props);
Dollor1=0;
Dollor2=0;
for i=1:n
if propsarea.Area(i)<2000
rgb = insertText(I,[prop.Centrod(i,1) prop.Centroid(i,2)],'1 Dollor',0.2, 'FontSize',12,'TextColor','black');
Dollor1=Dollor1+1;
elseif propsarea.Area(i)>2000
rgb = insertText(I,[prop.Centroid(i,1) prop.Centrod(i,2)],'2 Dollors',0.2 ,'FontSize',12,'TextColor','black');
Dollor2=Dollor2+1;
end
end
figure; imshow(rgb)

Best Answer

  • #Modified
    prop = regionprops('table',bw,{'Area','centroid'});
    % Modified here......^
    Dollor1=0;
    Dollor2=0;
    for i=1:n
    if prop.Area(i)<2000
    %position=[prop.Centroid(i,1),prop.Centroid(i,2)];

    position=[1 5;7 6;9 10];%sample example
    rgb = insertText(I,position,'Dollor', 'FontSize',12,'TextColor','black');
    Dollor1=Dollor1+1;
    elseif prop.Area(i)>2000
    %position=[prop.Centroid(i,1),prop.Centroid(i,2)];
    position=[1 5;7 6;9 10]; %sample Example
    rgb = insertText(I,position,'Dollors','FontSize',12,'TextColor','black');
    Dollor2=Dollor2+1;
    else
    end
    end
    figure; imshow(rgb)
    Please note that I have test the code with position sample example, here you have to define the 3 coordinates for the text box. Following result reflects only two numbers, ??
    >> [prop.Centroid(i,1),prop.Centroid(i,2)]
    ans =
    265.7967 102.6094
    Here
    prop =
    10×2 table
    Area Centroid
    ____ ________________
    2651 37.152 106.72
    1854 56.145 49.862
    2680 96.201 146.09
    1810 110.06 84.928
    2753 120.36 208.67
    2510 148.56 34.44
    2594 174.82 119.97
    2544 216.93 70.792
    1893 236.07 173.37
    1864 265.8 102.61
    Hope the remaining work (Postions of the text box) you can do that.