Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
regionprops

Subject: regionprops

From: peter veres

Date: 1 Nov, 2010 14:13:03

Message: 1 of 22

Hello for everyone
I try to use the regionprops to sort objects (Matlab R2007b) - circle, square, triangle - using area and perimeter from a binary image with the famous formula:

s=regionprops(L,'perimeter','area');
a=s.Perimeter^2/s.Area;

    % if the object is square
    if ((a>17) && (a<20))
    
% if the object is triangle
    %elseif((a>20) && (a<30))
   
        % if the object is circle
    elseif((a>13) && (a<17))
My biggest problem is that regionprops returns the same value for perimeter for any type of object.
The image is a clean binnary picture, and only one object/picture...
Is there any other way to determine what kind of object is?
Thank you for everyone...

Subject: regionprops

From: Sean

Date: 1 Nov, 2010 14:27:04

Message: 2 of 22

"peter veres" <petike779@gmail.com> wrote in message <iamhtf$e0h$1@fred.mathworks.com>...
> Hello for everyone
> I try to use the regionprops to sort objects (Matlab R2007b) - circle, square, triangle - using area and perimeter from a binary image with the famous formula:
>
> s=regionprops(L,'perimeter','area');
> a=s.Perimeter^2/s.Area;
>
> % if the object is square
> if ((a>17) && (a<20))
>
> % if the object is triangle
> %elseif((a>20) && (a<30))
>
> % if the object is circle
> elseif((a>13) && (a<17))
> My biggest problem is that regionprops returns the same value for perimeter for any type of object.
> The image is a clean binnary picture, and only one object/picture...
> Is there any other way to determine what kind of object is?
> Thank you for everyone...

Try comparing perimeter and area to ideal equivalent diameter. The equivalent diameter is the key.

You may also be able to measure distance from centroid to perimeter for all points on the perimeter and create a histogram of this. That would be more complicated though.

Subject: regionprops

From: ImageAnalyst

Date: 1 Nov, 2010 14:32:38

Message: 3 of 22

peter:
Didn't we talk about this before? (http://www.mathworks.com/
matlabcentral/newsreader/view_thread/293689) - you should have just
appended onto that thread.

regionprops does not do that. I use it all the time and unless your
perimeters are actually all the same, it will return different values,
i.e. the proper perimeters.

For one thing, if you have more than one object you can't do
a=s.Perimeter^2/s.Area;
because that / means the matrix inversion. You'd need to use dot
slash and dot caret, ./ & .^.
a = [s.Perimeter.^2] ./ [s.Area];

You can also use the Solidity measurement from regionprops to do shape
discrimination. Give that a try.

I know I've asked before, but post your picture and your code to a
file/image sharing web site.
-ImageAnalyst

Subject: regionprops

From: peter veres

Date: 1 Nov, 2010 15:08:04

Message: 4 of 22

Ok, you're right, I used that thread too. But, I'm still stuck.
Couple a minute ago I uploaded via file exchange the program and the sample pictures.
And yes I used too the 'Solidity', but there are problem with it, because it depends a lot from the size of the object. For small objects returns different values, it can be used single, but remember, in my final program I will use it on a web camera with different light condition... And this can be a problem...
Finnaly, I cannot understand why a simple task doesn't work on my analysis, everything is the same, but regionprops returns something else....



ImageAnalyst <imageanalyst@mailinator.com> wrote in message <3f1c6773-3489-41fb-adf0-f3e442b102df@v16g2000yqn.googlegroups.com>...
> peter:
> Didn't we talk about this before? (http://www.mathworks.com/
> matlabcentral/newsreader/view_thread/293689) - you should have just
> appended onto that thread.
>
> regionprops does not do that. I use it all the time and unless your
> perimeters are actually all the same, it will return different values,
> i.e. the proper perimeters.
>
> For one thing, if you have more than one object you can't do
> a=s.Perimeter^2/s.Area;
> because that / means the matrix inversion. You'd need to use dot
> slash and dot caret, ./ & .^.
> a = [s.Perimeter.^2] ./ [s.Area];
>
> You can also use the Solidity measurement from regionprops to do shape
> discrimination. Give that a try.
>
> I know I've asked before, but post your picture and your code to a
> file/image sharing web site.
> -ImageAnalyst

Subject: regionprops

From: peter veres

Date: 1 Nov, 2010 15:38:04

Message: 5 of 22

Hello again
Here is the .zip file with my program and pictures is alive for 7 days:
http://www.fileconvoy.com/filelist.php?id=93bf337542649




ImageAnalyst <imageanalyst@mailinator.com> wrote in message <3f1c6773-3489-41fb-adf0-f3e442b102df@v16g2000yqn.googlegroups.com>...
> peter:
> Didn't we talk about this before? (http://www.mathworks.com/
> matlabcentral/newsreader/view_thread/293689) - you should have just
> appended onto that thread.
>
> regionprops does not do that. I use it all the time and unless your
> perimeters are actually all the same, it will return different values,
> i.e. the proper perimeters.
>
> For one thing, if you have more than one object you can't do
> a=s.Perimeter^2/s.Area;
> because that / means the matrix inversion. You'd need to use dot
> slash and dot caret, ./ & .^.
> a = [s.Perimeter.^2] ./ [s.Area];
>
> You can also use the Solidity measurement from regionprops to do shape
> discrimination. Give that a try.
>
> I know I've asked before, but post your picture and your code to a
> file/image sharing web site.
> -ImageAnalyst

Subject: regionprops

From: Sean

Date: 1 Nov, 2010 16:09:04

Message: 6 of 22

"peter veres" <petike779@gmail.com> wrote in message <iammss$k48$1@fred.mathworks.com>...
> Hello again
> Here is the .zip file with my program and pictures is alive for 7 days:
> http://www.fileconvoy.com/filelist.php?id=93bf337542649

I played with your images a bit and came up with a classifier. The following is the preprocessing steps I did for your images and the engine. To call the engine something like this:
recognition(Itr)

%% Read and Preprocess
%{
Isq = imread('square.jpg');
Ici = imread('circle.jpg');
Itr = imread('triangle.jpg');
Isq = bwareaopen(imclearborder(Isq(:,:,1)~=255),10);
Ici = bwareaopen(imclearborder(Ici(:,:,1)~=255),10);
Itr = bwareaopen(imclearborder(Itr(:,:,1)~=255),10);
%}

%% Engine
function shape = recognition(I)
%SCd 11/01/2010
    S = regionprops(I,'EquivDiameter','Area','Image','MajorAxisLength');
    if S.Area/numel(S.Image) > 0.95
        shape = 'square/rectangle';
    elseif S.EquivDiameter/(S.MajorAxisLength)>.95
        shape = 'circle';
    elseif S.Area/(numel(S.Image(:))) > .45 && S.Area/(numel(S.Image(:))) < .55
        shape = 'triangle';
    else
        shape = 'Other shape';
    end
end

%%

Subject: regionprops

From: Cris Luengo

Date: 1 Nov, 2010 16:13:03

Message: 7 of 22

"peter veres" <petike779@gmail.com> wrote in message <iammss$k48$1@fred.mathworks.com>...
> Hello again
> Here is the .zip file with my program and pictures is alive for 7 days:
> http://www.fileconvoy.com/filelist.php?id=93bf337542649

*sigh*

regionprops takes a binary image, in which the object is set to 1, and the background to 0.

In your program, you are measuring the perimeter of the image. This is why you always get the same result.

Cheers,
Cris.

Subject: regionprops

From: Image Analyst

Date: 1 Nov, 2010 16:37:04

Message: 8 of 22

peter veres
Here, try this:
(You may have to connect any lines broken into two by the newsreader)
It asks you which image to read in and measures it and tells you what it is.
(It's long because it displays lots of your intermediate images and has user prompts, and is robust enough to detect multiple shapes within each image.)

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Ask user if they want to use a demo image or a different image.
message = sprintf('Do you want use a standard demo image,\nOr pick one of your own?');
reply2 = questdlg(message, 'Which Image?', 'Demo','I will pick a different one...', 'Demo');
if isempty(reply2)
% They clicked the red x to close without clicking a button.
return;
end
% Open an image.
if strcmpi(reply2, 'Demo')
% Read standard MATLAB demo image.
message = sprintf('Which demo image do you want to use?');
selectedImage = questdlg(message, 'Which Demo Image?', 'Square', 'Triangle', 'Circle', 'Circle');
if strcmp(selectedImage, 'Circle')
fullImageFileName = 'Circle.jpg';
elseif strcmp(selectedImage, 'Square')
fullImageFileName = 'Square.jpg';
else
fullImageFileName = 'Triangle.jpg';
end
else
% They want to pick their own.
% Browse for the image file.
[baseFileName, folder] = uigetfile('*.*', 'Specify an image file');
fullImageFileName = fullfile(folder, baseFileName);
if folder == 0
return;
end
end

% Check to see that the image exists. (Mainly to check on the demo images.)
if ~exist(fullImageFileName, 'file')
message = sprintf('This file does not exist:\n%s', fullImageFileName);
uiwait(msgbox(message));
return;
end

% Read in image into an array.
[rgbImage storedColorMap] = imread(fullImageFileName);
[rows columns numberOfColorBands] = size(rgbImage);
% If it's monochrome (indexed), convert it to color.
% Check to see if it's an 8-bit image needed later for scaling).
if strcmpi(class(rgbImage), 'uint8')
% Flag for 256 gray levels.
eightBit = true;
else
eightBit = false;
end
if numberOfColorBands == 1
if isempty(storedColorMap)
% Just a simple gray level image, not indexed with a stored color map.
% Create a 3D true color image where we copy the monochrome image into all 3 (R, G, & B) color planes.
rgbImage = cat(3, rgbImage, rgbImage, rgbImage);
else
% It's an indexed image.
rgbImage = ind2rgb(rgbImage, storedColorMap);
% ind2rgb() will convert it to double and normalize it to the range 0-1.
% Convert back to uint8 in the range 0-255, if needed.
if eightBit
rgbImage = uint8(255 * rgbImage);
end
end
end

% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
drawnow; % Make it display immediately.
if numberOfColorBands > 1
title('Original Color Image', 'FontSize', fontSize);
grayImage = rgbImage(:,:,1);
else
caption = sprintf('Original Indexed Image\n(converted to true color with its stored colormap)');
title(caption, 'FontSize', fontSize);
grayImage = rgbImage;
end
% Display it.
subplot(2, 2, 2);
imshow(grayImage, []);
title('Grayscale Image', 'FontSize', fontSize);

% Binarize the image.
binaryImage = grayImage < 100;
% Display it.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);

% Remove small objects.
binaryImage = bwareaopen(binaryImage, 300);
% Display it.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Cleaned Binary Image', 'FontSize', fontSize);

[labeledImage numberOfObjcts] = bwlabel(binaryImage);
blobMeasurements = regionprops(labeledImage,'Perimeter','Area');

% for square ((a>17) && (a<20))
% for circle ((a>13) && (a<17))
% for triangle ((a>20) && (a<30))
circularities = [blobMeasurements.Perimeter.^2] ./ (4 * pi * [blobMeasurements.Area])

% Say what they are
for blobNumber = 1 : numberOfObjcts
if circularities(blobNumber) < 1.19
message = sprintf('The circularity of object #%d is %.3f, so the object is a circle',...
blobNumber, circularities(blobNumber));
elseif circularities(blobNumber) < 1.53
message = sprintf('The circularity of object #%d is %.3f, so the object is a square',...
blobNumber, circularities(blobNumber));
else
message = sprintf('The circularity of object #%d is %.3f, so the object is a triangle',...
blobNumber, circularities(blobNumber));
end
uiwait(msgbox(message));
end

Subject: regionprops

From: peter veres

Date: 1 Nov, 2010 18:13:03

Message: 9 of 22

Sorry for delay, but YOU ARE REALLY A MASTER!!!!!!!!!!
It works great!
I uploaded some real pictures from the web camera capture
http://www.fileconvoy.com/filelist.php?id=94e733778264f

Tomorow I will try on the transportation belt with real time capturing, of course some adaptation it's needed...
PS I really need a bank account from you where to send the big bucks.... :-)



"Image Analyst" <imageanalyst@mailinator.com> wrote in message <iamqbg$a40$1@fred.mathworks.com>...
> peter veres
> Here, try this:
> (You may have to connect any lines broken into two by the newsreader)
> It asks you which image to read in and measures it and tells you what it is.
> (It's long because it displays lots of your intermediate images and has user prompts, and is robust enough to detect multiple shapes within each image.)
>
> clc; % Clear the command window.
> close all; % Close all figures (except those of imtool.)
> imtool close all; % Close all imtool figures.
> clear; % Erase all existing variables.
> workspace; % Make sure the workspace panel is showing.
> fontSize = 20;
>
> % Change the current folder to the folder of this m-file.
> if(~isdeployed)
> cd(fileparts(which(mfilename)));
> end
>
> % Ask user if they want to use a demo image or a different image.
> message = sprintf('Do you want use a standard demo image,\nOr pick one of your own?');
> reply2 = questdlg(message, 'Which Image?', 'Demo','I will pick a different one...', 'Demo');
> if isempty(reply2)
> % They clicked the red x to close without clicking a button.
> return;
> end
> % Open an image.
> if strcmpi(reply2, 'Demo')
> % Read standard MATLAB demo image.
> message = sprintf('Which demo image do you want to use?');
> selectedImage = questdlg(message, 'Which Demo Image?', 'Square', 'Triangle', 'Circle', 'Circle');
> if strcmp(selectedImage, 'Circle')
> fullImageFileName = 'Circle.jpg';
> elseif strcmp(selectedImage, 'Square')
> fullImageFileName = 'Square.jpg';
> else
> fullImageFileName = 'Triangle.jpg';
> end
> else
> % They want to pick their own.
> % Browse for the image file.
> [baseFileName, folder] = uigetfile('*.*', 'Specify an image file');
> fullImageFileName = fullfile(folder, baseFileName);
> if folder == 0
> return;
> end
> end
>
> % Check to see that the image exists. (Mainly to check on the demo images.)
> if ~exist(fullImageFileName, 'file')
> message = sprintf('This file does not exist:\n%s', fullImageFileName);
> uiwait(msgbox(message));
> return;
> end
>
> % Read in image into an array.
> [rgbImage storedColorMap] = imread(fullImageFileName);
> [rows columns numberOfColorBands] = size(rgbImage);
> % If it's monochrome (indexed), convert it to color.
> % Check to see if it's an 8-bit image needed later for scaling).
> if strcmpi(class(rgbImage), 'uint8')
> % Flag for 256 gray levels.
> eightBit = true;
> else
> eightBit = false;
> end
> if numberOfColorBands == 1
> if isempty(storedColorMap)
> % Just a simple gray level image, not indexed with a stored color map.
> % Create a 3D true color image where we copy the monochrome image into all 3 (R, G, & B) color planes.
> rgbImage = cat(3, rgbImage, rgbImage, rgbImage);
> else
> % It's an indexed image.
> rgbImage = ind2rgb(rgbImage, storedColorMap);
> % ind2rgb() will convert it to double and normalize it to the range 0-1.
> % Convert back to uint8 in the range 0-255, if needed.
> if eightBit
> rgbImage = uint8(255 * rgbImage);
> end
> end
> end
>
> % Display the original image.
> subplot(2, 2, 1);
> imshow(rgbImage);
> set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.
> set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
> drawnow; % Make it display immediately.
> if numberOfColorBands > 1
> title('Original Color Image', 'FontSize', fontSize);
> grayImage = rgbImage(:,:,1);
> else
> caption = sprintf('Original Indexed Image\n(converted to true color with its stored colormap)');
> title(caption, 'FontSize', fontSize);
> grayImage = rgbImage;
> end
> % Display it.
> subplot(2, 2, 2);
> imshow(grayImage, []);
> title('Grayscale Image', 'FontSize', fontSize);
>
> % Binarize the image.
> binaryImage = grayImage < 100;
> % Display it.
> subplot(2, 2, 3);
> imshow(binaryImage, []);
> title('Binary Image', 'FontSize', fontSize);
>
> % Remove small objects.
> binaryImage = bwareaopen(binaryImage, 300);
> % Display it.
> subplot(2, 2, 4);
> imshow(binaryImage, []);
> title('Cleaned Binary Image', 'FontSize', fontSize);
>
> [labeledImage numberOfObjcts] = bwlabel(binaryImage);
> blobMeasurements = regionprops(labeledImage,'Perimeter','Area');
>
> % for square ((a>17) && (a<20))
> % for circle ((a>13) && (a<17))
> % for triangle ((a>20) && (a<30))
> circularities = [blobMeasurements.Perimeter.^2] ./ (4 * pi * [blobMeasurements.Area])
>
> % Say what they are
> for blobNumber = 1 : numberOfObjcts
> if circularities(blobNumber) < 1.19
> message = sprintf('The circularity of object #%d is %.3f, so the object is a circle',...
> blobNumber, circularities(blobNumber));
> elseif circularities(blobNumber) < 1.53
> message = sprintf('The circularity of object #%d is %.3f, so the object is a square',...
> blobNumber, circularities(blobNumber));
> else
> message = sprintf('The circularity of object #%d is %.3f, so the object is a triangle',...
> blobNumber, circularities(blobNumber));
> end
> uiwait(msgbox(message));
> end

Subject: regionprops

From: peter veres

Date: 6 Nov, 2010 07:58:03

Message: 10 of 22

Hello again
What can I say, the problem is that my program has a problem! Your program works fine but in my program returns the same value for perimeter.

I uploaded the whole program here:
http://www.fileconvoy.com/filelist.php?id=g1a1ff0a21efce6e634406dd3190d410280a21
(it's single line, use continuosly live for 7 days...)

The final.m is the main program, creates the final5 GUI. After starting sends 'S' character via serial port to device, waits the '#' character from the device and start capturing on ujrecunoastere.m file. This returns a value for 'd' depends on what type of shape is.
Finnaly sends a character to device, depending the type of shape 'R' right, 'L' left, 'N' for neutral and waits again the '#' character to start capture again.

So, the value for perimeter is the same for any type of shape I use...
I'm sure I have a mistake , but really don't know what it is...
Thank you in advice for your help.








ImageAnalyst <imageanalyst@mailinator.com> wrote in message <3f1c6773-3489-41fb-adf0-f3e442b102df@v16g2000yqn.googlegroups.com>...
> peter:
> Didn't we talk about this before? (http://www.mathworks.com/
> matlabcentral/newsreader/view_thread/293689) - you should have just
> appended onto that thread.
>
> regionprops does not do that. I use it all the time and unless your
> perimeters are actually all the same, it will return different values,
> i.e. the proper perimeters.
>
> For one thing, if you have more than one object you can't do
> a=s.Perimeter^2/s.Area;
> because that / means the matrix inversion. You'd need to use dot
> slash and dot caret, ./ & .^.
> a = [s.Perimeter.^2] ./ [s.Area];
>
> You can also use the Solidity measurement from regionprops to do shape
> discrimination. Give that a try.
>
> I know I've asked before, but post your picture and your code to a
> file/image sharing web site.
> -ImageAnalyst

Subject: regionprops

From: ImageAnalyst

Date: 6 Nov, 2010 12:35:20

Message: 11 of 22

peter veres
That's because for some reason you chose to invert your binary image
of your blobs. If you do this then you are selecting the BACKGROUND
instead of the blobs. And there is only one background, not multiple
backgrounds and so the size is all the same and there is only one size
and perimeter to the background. Look again at the code I gave you
above. Do you see anywhere in there where I invert the image? No.
So I'm not sure why you chose to change the algorithm and thus break
it.

 Here is your code where I've fixed it and made it into a demo to
illustrate your problem:
(BE SURE to join any lines split into two by the newsreader - they
will show up as syntax errors).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

close all;
clear all;
clc;
fontSize = 18;
%Step 1: read image in:
RGB = imread('kep1.jpg');

subplot(2,3,1);
imshow(RGB),
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.
title('Captured image', 'FontSize', fontSize);
% set(handles.text1,'String','RESULT:');
% Step 2: Convert image from rgb to gray
GRAY = rgb2gray(RGB);
subplot(2,3,2);
imshow(GRAY),
title('Gray image', 'FontSize', fontSize);

R=GRAY(:,:,1);
R_BW=(R<100);
subplot(2,3,3);
imshow(R_BW);
title('Cleaning the shadow', 'FontSize', fontSize);

% Step 3: Threshold the image Convert the image to black and white in
order
% to prepare for boundary tracing using bwboundaries.
title('Binary image', 'FontSize', fontSize);

% Step 4: Invert the Binary Image
promptMessage = sprintf('Do you want to invert the binary image?\nOr
Cancel to abort processing?');
button = questdlg(promptMessage, 'Invert?', 'Yes', 'No', 'Cancel',
'No');
subplot(2,3,4);
if strcmp(button, 'Yes')
BW = ~ R_BW; % WON'T WORK IF YOU DO THIS!!!!!!!!!!!!!!!!!!!!!!!!!
imshow(BW),
title('Inverted binary image', 'FontSize', fontSize);
elseif strcmp(button, 'No')
BW = R_BW;
imshow(BW),
title('Same (not inverted) binary image', 'FontSize', fontSize);
else
% Cancel
return;
end


BW2=bwareaopen(BW,300); %%%%%%Objects smaller then 300 pixels are
cleaned
subplot(2,3,5);
imshow(BW2),
title('With small objects removed', 'FontSize', fontSize);

[labeledImage, numberOfObjects]=bwlabel(BW2);
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); %
pseudo random color labels
hold on;

subplot(2,3,6);
imshow(coloredLabels)
message = sprintf('Found %d white objects', numberOfObjects);
title(message, 'FontSize', fontSize);
uiwait(msgbox(message));

blobMeasurements = regionprops(labeledImage,'Perimeter','Area',
'Centroid')
circularities = [blobMeasurements.Perimeter.^2] ./ (4 * pi *
[blobMeasurements.Area])

labelShiftX = -7; % Used to align the labels in the centers of the
coins.
for blob = 1 : numberOfObjects
    blobNumber=1;
    if circularities(blobNumber) < 1.19 % if the object is square
        d=2
    elseif circularities(blobNumber) < 1.4 %circle
        d=1
    else
        d=0 %undefined
end
blobCentroid = blobMeasurements(blob).Centroid; % Get centroid.
% Put the "blob number" labels on the "boundaries" grayscale image.
text(blobCentroid(1) + labelShiftX, blobCentroid(2), ...
num2str(blob), 'FontSize', fontSize, 'FontWeight', 'Bold');

message = sprintf('Blob %d\nArea=%.2f\nPerimeter=%.2f\nCircularity=%.
2f',...
blob, blobMeasurements(blob).Area,...
blobMeasurements(blob).Perimeter, circularities(blob));
uiwait(msgbox(message));
end
return

Subject: regionprops

From: peter veres

Date: 6 Nov, 2010 19:32:04

Message: 12 of 22

AGAIN, YOU'RE ABSOLUTELLY RIGHT !!!!!
That was the problem..... I don't know why I used that inversion but was stupidity...
Of course the perimeter was the whole image captured.... And this is identically for the captured pictures.
I made the corrections for my program (ujrecunoastere.m):
% Step 4: Invert the Binary Image
                %%%%%%%%BW = ~ R_BW;
%figure,
                %%%%%%%%%imshow(BW),
                %%%%%%%%%title(Inverted binary image);
pause(1);

%%%%%%%%%%%%%%%%%%%%%%%%%%
BW2=bwareaopen(R_BW,300); %cleaning small objects

Now it's working OK.
What do you think, is somebody interested in the whole project? I can upload to Mathworks, perhaps somebody has the same problem... But my project includes the microcontroler and some electronics too. How can I upload the full project? Of course for free?
I realy appreciate your help and for others who gave to me some ideeas too. It really helped me.


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <4257cc46-0a14-4844-be69-c38bcb148faf@t35g2000yqj.googlegroups.com>...
> peter veres
> That's because for some reason you chose to invert your binary image
> of your blobs. If you do this then you are selecting the BACKGROUND
> instead of the blobs. And there is only one background, not multiple
> backgrounds and so the size is all the same and there is only one size
> and perimeter to the background. Look again at the code I gave you
> above. Do you see anywhere in there where I invert the image? No.
> So I'm not sure why you chose to change the algorithm and thus break
> it.
>
> Here is your code where I've fixed it and made it into a demo to
> illustrate your problem:
> (BE SURE to join any lines split into two by the newsreader - they
> will show up as syntax errors).
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> close all;
> clear all;
> clc;
> fontSize = 18;
> %Step 1: read image in:
> RGB = imread('kep1.jpg');
>
> subplot(2,3,1);
> imshow(RGB),
> set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
> screen.
> title('Captured image', 'FontSize', fontSize);
> % set(handles.text1,'String','RESULT:');
> % Step 2: Convert image from rgb to gray
> GRAY = rgb2gray(RGB);
> subplot(2,3,2);
> imshow(GRAY),
> title('Gray image', 'FontSize', fontSize);
>
> R=GRAY(:,:,1);
> R_BW=(R<100);
> subplot(2,3,3);
> imshow(R_BW);
> title('Cleaning the shadow', 'FontSize', fontSize);
>
> % Step 3: Threshold the image Convert the image to black and white in
> order
> % to prepare for boundary tracing using bwboundaries.
> title('Binary image', 'FontSize', fontSize);
>
> % Step 4: Invert the Binary Image
> promptMessage = sprintf('Do you want to invert the binary image?\nOr
> Cancel to abort processing?');
> button = questdlg(promptMessage, 'Invert?', 'Yes', 'No', 'Cancel',
> 'No');
> subplot(2,3,4);
> if strcmp(button, 'Yes')
> BW = ~ R_BW; % WON'T WORK IF YOU DO THIS!!!!!!!!!!!!!!!!!!!!!!!!!
> imshow(BW),
> title('Inverted binary image', 'FontSize', fontSize);
> elseif strcmp(button, 'No')
> BW = R_BW;
> imshow(BW),
> title('Same (not inverted) binary image', 'FontSize', fontSize);
> else
> % Cancel
> return;
> end
>
>
> BW2=bwareaopen(BW,300); %%%%%%Objects smaller then 300 pixels are
> cleaned
> subplot(2,3,5);
> imshow(BW2),
> title('With small objects removed', 'FontSize', fontSize);
>
> [labeledImage, numberOfObjects]=bwlabel(BW2);
> coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); %
> pseudo random color labels
> hold on;
>
> subplot(2,3,6);
> imshow(coloredLabels)
> message = sprintf('Found %d white objects', numberOfObjects);
> title(message, 'FontSize', fontSize);
> uiwait(msgbox(message));
>
> blobMeasurements = regionprops(labeledImage,'Perimeter','Area',
> 'Centroid')
> circularities = [blobMeasurements.Perimeter.^2] ./ (4 * pi *
> [blobMeasurements.Area])
>
> labelShiftX = -7; % Used to align the labels in the centers of the
> coins.
> for blob = 1 : numberOfObjects
> blobNumber=1;
> if circularities(blobNumber) < 1.19 % if the object is square
> d=2
> elseif circularities(blobNumber) < 1.4 %circle
> d=1
> else
> d=0 %undefined
> end
> blobCentroid = blobMeasurements(blob).Centroid; % Get centroid.
> % Put the "blob number" labels on the "boundaries" grayscale image.
> text(blobCentroid(1) + labelShiftX, blobCentroid(2), ...
> num2str(blob), 'FontSize', fontSize, 'FontWeight', 'Bold');
>
> message = sprintf('Blob %d\nArea=%.2f\nPerimeter=%.2f\nCircularity=%.
> 2f',...
> blob, blobMeasurements(blob).Area,...
> blobMeasurements(blob).Perimeter, circularities(blob));
> uiwait(msgbox(message));
> end
> return

Subject: regionprops

From: robert clive

Date: 27 Nov, 2010 18:23:03

Message: 13 of 22

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iamon0$lt9$1@fred.mathworks.com>...
> "peter veres" <petike779@gmail.com> wrote in message <iammss$k48$1@fred.mathworks.com>...
> > Hello again
> > Here is the .zip file with my program and pictures is alive for 7 days:
> > http://www.fileconvoy.com/filelist.php?id=93bf337542649
>
> I played with your images a bit and came up with a classifier. The following is the preprocessing steps I did for your images and the engine. To call the engine something like this:
> recognition(Itr)
>
> %% Read and Preprocess
> %{
> Isq = imread('square.jpg');
> Ici = imread('circle.jpg');
> Itr = imread('triangle.jpg');
> Isq = bwareaopen(imclearborder(Isq(:,:,1)~=255),10);
> Ici = bwareaopen(imclearborder(Ici(:,:,1)~=255),10);
> Itr = bwareaopen(imclearborder(Itr(:,:,1)~=255),10);
> %}
>
> %% Engine
> function shape = recognition(I)
> %SCd 11/01/2010
> S = regionprops(I,'EquivDiameter','Area','Image','MajorAxisLength');
> if S.Area/numel(S.Image) > 0.95
> shape = 'square/rectangle';
> elseif S.EquivDiameter/(S.MajorAxisLength)>.95
> shape = 'circle';
> elseif S.Area/(numel(S.Image(:))) > .45 && S.Area/(numel(S.Image(:))) < .55
> shape = 'triangle';
> else
> shape = 'Other shape';
> end
> end
>
> %%


hi
i need to recognize shapes as you specified in above code but i am not getting
is that above code need some more attachments or it will work like this only

Subject: regionprops

From: RAZVAN BARBU

Date: 8 Dec, 2010 20:03:05

Message: 14 of 22

Good evening,

I have a project for the university...
I have read all the posts but I still have un unresolved question...
So....I have to recognize circle,square si triangle (this is done by now , using circularities) but I have to separate also the rebuts ( rebuts = other definited shapes and incomplete circles/suares/triangles)...
I need some other properties or characteristics to identify the rebuts...
maybe the number of edges, but I don't know how exactly....
I would appreciate un ideea and of course some indications to realize it...

Thank you!
Razvan Barbu

Subject: regionprops

From: ImageAnalyst

Date: 8 Dec, 2010 21:47:50

Message: 15 of 22

Razvan Barbu:
Look up shape descriptors:
http://en.wikipedia.org/wiki/Shape_analysis

Subject: regionprops

From: RAZVAN BARBU

Date: 9 Dec, 2010 11:04:04

Message: 16 of 22


Re,

I have used the circularities to recognize the 3 shapes ...
if circularities >1 and <1,2 - > circle
if circularities >1,2 and <1,4 - > square
else -> triangle

I'm not interested in the shapes of the rebuts , but i need something to include the rebuts in the above ecuation.... (incomplete circle, or a tringle without an edge)
I don't know what... I would appreciate a sugestion...

Thank you!
Razvan

Subject: regionprops

From: ImageAnalyst

Date: 9 Dec, 2010 11:13:51

Message: 17 of 22

But in your first post you said that the shapes were what defined the
rebuts. You said that they could be defined by NOT( circle OR square
OR triangle). If there is something else that defines them then use
that. Use whatever works.

Subject: regionprops

From: RAZVAN BARBU

Date: 9 Dec, 2010 14:12:05

Message: 18 of 22


Re,

it's me again :D

I need something to separate the rebuts from the other 3 shapes.
I'm not interested in the shape of the rebuts ...(in the first post I just gave some examples)
Maybe the number of edges can help me, but I don't know how to calculate them...


thank you again!
Razvan

Subject: regionprops

From: ImageAnalyst

Date: 9 Dec, 2010 17:52:46

Message: 19 of 22

This is going nowhere. You're just repeating. I could repeat my
answer too but at this point I think you'd better post your photo
somewhere.

By the way, number of edges is not well defined. What if your image
is a bunch of irregularly shaped blobs, like a bunch of splats? How
many edges do those have? Again, post your image then we can work on
solving it.

Subject: regionprops

From: RAZVAN BARBU

Date: 9 Dec, 2010 19:37:06

Message: 20 of 22


ok...maybe i wasn't clear enough...
let's put it on another way...
so I have a transportation belt , which is carring biscuits(cookies)
The biscuits are of 3 shapes and also rebuts and come each at a time .
A camera is placed on the top and takes photos of each biscuit at the end of the transporting belt ...
the algorithm needs to separate the biscuits into the 4 clases .(circle, square,triangle,rebuts)
I have attached the photo with example of biscuits (i repeat the biscuits are coming on the belt each at a time ) and also the algorith I have used to separate the three shapes , but it doesn't help me with the rebuts...
http://www.fileconvoy.com/filelist.php?id=gac04adc37310e404414899d71ef0420789406

Hopping this is more clear,
Razvan

Subject: regionprops

From: ImageAnalyst

Date: 9 Dec, 2010 21:49:53

Message: 21 of 22

Well again, you have to look at what defines your "perfect" shapes.
There are lots of things that regionprops returns and you have to look
at the combination of features that describe the shapes. Maybe you
could measure them all and do a PCA on it, or simply do a scatterplot
and look for similarities. I think you can do this. I know I could
and that's what I would do but I don't have time to do it for you.

Subject: regionprops

From: peter veres

Date: 10 Dec, 2010 16:48:05

Message: 22 of 22

"RAZVAN BARBU" <razvan_brb@yahoo.com> wrote in message <idrb52$coe$1@fred.mathworks.com>...
>
> ok...maybe i wasn't clear enough...
> let's put it on another way...
> so I have a transportation belt , which is carring biscuits(cookies)
> The biscuits are of 3 shapes and also rebuts and come each at a time .
> A camera is placed on the top and takes photos of each biscuit at the end of the transporting belt ...
> the algorithm needs to separate the biscuits into the 4 clases .(circle, square,triangle,rebuts)
> I have attached the photo with example of biscuits (i repeat the biscuits are coming on the belt each at a time ) and also the algorith I have used to separate the three shapes , but it doesn't help me with the rebuts...
> http://www.fileconvoy.com/filelist.php?id=gac04adc37310e404414899d71ef0420789406
>
> Hopping this is more clear,
> Razvan

Hello there
Sorry for the big delay
The program created with the help of imageanalyst works excellent for student demo purposes, I don't want to mention all the minus points of him. But it works ok.
My personnal experience with that is that can sepparate the 3 kind of shapes circle, square, triangle but when you use a star shape for exemple it cannot decide well if this really a star.
Even if you use a square with some minor irregularities on the side like not perfect square shape you will find some error in the results. If you use a circle biscuit with inscriptions on it, or a whole in the middle, you also will go in errors. The object must be cleaned from any irregularities, because if you don't do that, like image analist said, you will obtain a lot of objects instead one object. (a lot of little objects around the big object). And if I remember well the biscuits aren't beautiful perfect shapes.....
I forgot to mention the ilumination of the objects has to be always constant, than colour of the object also...
It makes what do you want, but this is not a "professional" solution.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us