Author Topic: Image processing  (Read 755 times)

Offline amt_015

  • Newbie
  • *
  • Posts: 1
    • View Profile
Image processing
« on: May 18, 2009, 12:16:58 AM »
Hi every1.

The problem I have with the following function is that I don't know why it opens a figure window everytime I run it. It works perfectly, its just that I don't want this window to open, since I will use it in a bigger function more than a 100 times, and a 100 windows would be a bit annoying.

Thanks a lot.

__________________________


function centroidsProperties = colorToCentroidsParametersNoFigures(filename)
%%
%colorToCentroidsParametersNoFigures
%
%Prototype: centroidsProperties = colorToCentroidsParametersNoFigures(filename)
%
%Arguments: filename, image that will be processed.
%
%Description: colorToCentroidsParametersNoFigures receives a color image with
%markers in it and returns a matrix with the angle between the markers, the
%markers' centroids' area, and their x & y Cartesian coordinates:
%
%   angle         angle         angle
%   areaCent1     areaCent2     areaCent3
%   xCoordCent1   xCoordCent2   xCoordCent3
%   yCoordCent1   yCoordCent2   yCoordCent3
%
%No images will be displayed during the execution of this function.
%

%%
%Definition of important parameters
length = 640;
height = 480;
           

%%
%Convert the original image to B&W
originalImage = imread(filename);                         %Read image
bwImage = ind2gray(originalImage, colormap);  %Transform color image to B&W


%%
%Filtering the image

%Creates a more uniform background
background = imopen(bwImage, strel('disk', 13));
bwImage = bwImage - background;

%Increase the image contrast
bwImage = imadjust(bwImage);


%%
%Convert the B&W image to a binary image
level = graythresh(bwImage);                    %Automatically define threshold
binImage = im2bw(bwImage, level);
binImage = bwareaopen(binImage, 180);            %Removes background noise


%%
%Component labeling with 8 contiguous elements.
centroidsLabeled = bwlabel(binImage, 8);


%%
%Obtain an array with an structure for each marker. Each structure contains
%the area, centroid and bounding box of each one.
rawProperties = regionprops(centroidsLabeled);

%%
%Obtain the cetroids' cartesian coordinates.
x1 = rawProperties(1,1).Centroid(1,1);
y1 = rawProperties(1,1).Centroid(1,2);
y1 = height - y1;

x2 = rawProperties(2,1).Centroid(1,1);
y2 = rawProperties(2,1).Centroid(1,2);
y2 = height - y2;

x3 = rawProperties(3,1).Centroid(1,1);
y3 = rawProperties(3,1).Centroid(1,2);
y3 = height - y3;


%%
%Obtain the dimensions of the triangle formed by the centroids.
a = sqrt( ((x1 - x2)^2) + ((y1 - y2)^2) );
b = sqrt( ((x3 - x2)^2) + ((y3 - y2)^2) );
c = sqrt( ((x3 - x1)^2) + ((y3 - y1)^2) );


%%
%Obtain the desired angle in degrees.
angleRad = acos(((c^2) - (a^2) - (b^2))/(-2*a*b));
angleDeg = (angleRad * 360)/(2*pi);

%%
%Pack the results in a matrix structured like this:
%
%   angle         angle         angle
%   areaCent1     areaCent2     areaCent3
%   xCoordCent1   xCoordCent2   xCoordCent3
%   yCoordCent1   yCoordCent2   yCoordCent3

for i = 1:3
    centroidsProperties(1,i) = angleDeg;   
end

for i = 1:3
    centroidsProperties(2,i) = rawProperties(i,1).Area;
end

for i = 1:3
    centroidsProperties(3,i) = rawProperties(i,1).Centroid(1,1);
end

for i = 1:3
    centroidsProperties(4,i) = height - (rawProperties(i,1).Centroid(1,2));
end


Matlab and SimuLink Development Forum

Image processing
« on: May 18, 2009, 12:16:58 AM »