Counting the number of medium-sized buttons on the image using Matlab

59 views Asked by At

I tried to select borders and then calculate the area. Вut I'm having some difficulty with this. Here's my original image.

enter image description here

After after some processing of the original image I obtained this image:

enter image description here

Here's my code:

    clear;
    I=imread('C:\Users\Sashka\Desktop\calculator.jpg');
    i=rgb2gray(I);
    background = imopen(i,strel('disk',20));
    im=histeq(background, 64);
    uns=fspecial('unsharp') ;  
    uns1=imfilter(im, uns);
    bw = im2bw(uns1,0.3); 
    bw2=bwmorph(bw, 'close', Inf);
    bw3=bwmorph(bw2, 'majority', Inf);
    bw4=bwmorph(bw3, 'erode', 18);
    c=[30 30 440 440];
    r=[480 680 680 480];
    bw5=bwselect(bw4,c,r,4);
    I2=imcrop(bw5, [30 25 1300 1090]);

Can You please help me to count the number of medium-sized buttons?

1

There are 1 answers

0
SamuelNLP On

Just use regionprops and define a threshold of the maximum or minimum area of the desired buttons, example:

your code plus...

original = imcrop(i, [30 25 1300 1090]);

L_Measurements = regionprops(~I2, original, 'all');

threshold = 40000;
Areaofbuttons = cell2mat({L_Measurements.Area}');
numberOfButtons = sum( Areaofbuttons > threshold)

which for example gives,

numberOfButtons =

     9