Thursday, July 17, 2008

..on binary operations

Below is an image of scattered punched paper. Our goal is approximate the size (pixel count) of a single punched paper.




















Since the image is relatively large, we cut it down into twelve 256X256 subimages. We then convert them into binary images. Below are examples of the binary images.

























These images are not ideal. We enhanced them further by applying opening and closing morphology operator. These clean the image of isolated pixel and small holes. Below are the results of the enhancement.

























We now measure the pixel count for each blob. Since the enhancement was not able to perfectly separate individual punched paper, there are big blobs on the image. We need to filter out this outliers. We do this by getting the histogram of the pixel count.


















For this particular plot, we consider only those values between 300-600 since it is in this range that we have an approximate Gaussian distribution. Finally, we take the mean of the pixel count within this range. Our result is 428.89 with a standard deviation of 71.53. To check, we processed images of individual punched paper and measured the pixel count. The average value is 456.1 with a standard deviation of 70.69. This is a 6% error.

Below is the Scilab code. I was able to implement the program such that it automatically outputs the mean pixel count and standard deviation for all images.

numt=[];
v=0;
for j=1:1:12
//upload image
dir('E:\acads\1st sem 08-09\186\activity9');
str=strcat(['C2_',string(j),'.jpg']);
image1=imread(str);

//convert to gray scale
image1=im2gray(image1);

//convert to binary
image1=im2bw(image1,229/255);
//f1=scf(1);imshow(image1);

//make structuring element
sq=ones(2,2);
se=mkfftfilter(sq,'binary',1);

//opening
C1=erode(image1,se,[1,1]);
//f2=scf(2);imshow(C1);
C2=dilate(C1,se,[1,1]);
//f3=scf(3);imshow(C2);

//closing
C3=dilate(C2,se,[1,1]);
//f4=scf(4);imshow(C3);
C4=erode(C3,se,[1,1]);
//f4=scf(2);imshow(C4);


//labeling
[mat,n]=bwlabel(C4);

//counting pixel size
val=[];
num=[];
counter=1;
for i=1:1:n
[x,y]=find(mat==i);
val(counter)=i;
num(counter)=length(x);
counter=counter+1;
end

for i=1:1:n
//mm=v+i
numt(v+i)=num(i);
end
v=v+n;

end

//histplot([10:30:800],numt);

//neglect outlier
numf=[];
c=1;
for i=1:1:length(numt)
if numt(i)>=300 & numt(i)<=600
numf(c)=numt(i);
c=c+1;
end
end

mean(numf)
stdev(numf)

I was able complete the activity. I was also able to implement the program automatically. I think I would give myself a 10 plus the bonus (5), thus a 15.=)

No comments: