From: Davide longo on
Hi everyone,
i'm newbie with matlab. I'm working with image processing tool and i try to realize a script that takes the image of a plate number and returns the segmented image. This is based on a program that i found here.
My problem is this: i have generated the segmentated image but i have a large number of connected region. How could I remove little regions?
I try to obtain the area of all region throught regionprops and then i used this information to calculate the mean area, because i have think that i can use this mean as a threshold. But i don't know how i can eliminate these.

ps. sorry for my english ;)

this is my source code:

%% initialization
clc; % clear command window
clear all; %remove all initialized variables
close all; %close all windows

%% load image
[filename, pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.JPG','All Image Files';'*.*','All Files' }, 'Carica Immagine');

immagine = imread([pathname,filename]);

imInfo = imfinfo([pathname, filename]);%estrae le informazioni da un'immagine

imshow(immagine);

%% converting the image from rgb to grey level
%se l'immagine è in livelli di grigio OK, altrimenti se è RGB la trasformo
%in livelli di grigio

if (imInfo.Width > 436 || imInfo.Height > 302)
imInfo.Width, imInfo.Height
immagine = imresize(immagine, [436 302]);
end
if (strcmp(imInfo.ColorType, 'truecolor' ))
g = rgb2gray(immagine);
end
figure, imshow(g);

%% remove noise
% applico un filtro mediano 2-D(immagine)
g=medfilt2(g,[3 3]);
figure, imshow(g);
title('denoise');
%l'effetto è un'immagine più opaca(blurring) questo favorisce i passaggi
%successivi. previene l'aliasing
figure, imhist(g);


%% find the edges
%BW = edge(I) takes a grayscale or a binary image I as its input, and returns a binary image BW of the same size as I, with 1's where the function finds edges in I and 0's elsewhere.
%generalmente il metodo edge utilizza sobel per l'individuazione degli edge

%The Sobel method finds edges using the Sobel approximation to the
%derivative. It returns edges at those points where the gradient of I is maximum
[BW, thresh] = edge(g,'sobel'); %finding edges
thresh;
%la soglia è 0.1632. Ne prendo una più alta in modo tale da eliminare
%qualche oggetto non necessario
BW = edge(g, 'sobel', 0.24);

figure, imshow(BW);
title('sobel');


%% deconvolution

% size(X) returns the sizes of each dimension of array X
[imx,imy]=size(BW);

%definisco la maschera di deconvolutione
msk=[0 0 0 0 0;
0 1 1 1 0;
0 1 1 1 0;
0 1 1 1 0;
0 0 0 0 0;];

%2-D convolution
%C = conv2(A,B) computes the two-dimensional convolution of matrices A and
%B. If one of these matrices describes a two-dimensional finite impulse response (FIR) filter, the other matrix is filtered in two dimensions.
B=conv2(double(BW),double(msk)); %Smoothing image to reduce the number of connected components

figure, imshow(B);
title('deconvolution');


%% labeling

%L = bwlabel(BW,n) returns a matrix L, of the same size as BW, containing
%labels for the connected objects in BW. n can have a value of either 4 or 8, where 4 specifies 4-connected objects and 8 specifies 8-connected objects;
%L = bwlabel(B,8);% Calculating connected components

%RGB = label2rgb(L);
%figure, imshow(RGB);

[L, mx] = bwlabel(B, 8); %ritorna il numero massimo di componenti connesse

%% area delle componenti connesse

blobMeasurements = regionprops(L, 'all');
numberOfBlobs = size(blobMeasurements, 1);
%get a list of the areas
area_values = [blobMeasurements.Area];

ordinato = sort(area_values);
min = ordinato(1);
max = sort(mx);
threshold = mean(ordinato);

%tutto quello che è più piccolo della soglia lo butto via

se1 = zeros(3);

this is the point

for i=1:mx
if ordinato(i) < threshold
ordinato(i)
%metto tutti i pixel di quella regione a zero in modo da farla
%scomparire
%vedere le operazioni per il label (fill di una regione, negativo,
%etc)
end
end
From: ImageAnalyst on
Davide:
You could use bwareaopen(), or use ismember().
Go to my image processing demo
http://www.mathworks.com/matlabcentral/fileexchange/25157
for an example of how to use ismember.
ImageAnalyst

By the way, if you convolve a binary image, you'll get a gray scale
image. You'll probably get an error when you pass your double B into
bwlabel, which has been superceded by bwconncomp().