Wednesday, May 29, 2013

Final Code

Below is the final code, this code is the property of Group 01- Section 064

clc
clear all
close all
%strategy for showing kmeans values on image, by subtracting an arbitrary
%mutliplication from the cluster of the original iamge, we can locate all
%of the pole locations.
%six clusters 3 values, 9 clusters 3 or 4 values, 12 clusters 4 values
i = imread('Image1.tif'); %original image
j = i(:,:,1:3);%scaled down images
k = imresize(j, 0.25);%reduces image further, used to prevent memory failures.
red = k(:,:,2); %isolates the color red
j = rgb2gray(k); %converts image to greyscale
y = fspecial('laplacian', 0.9); %various filters
d = fspecial('gaussian', [8 8], 0.5);
kd = fspecial('prewitt');%used for edge detection
kd = kd';%inverse for vertical lines
f = imfilter(j,y);
for i = 1:2 %filters twice for enahcnement purposes.
    f = imfilter(f,y);
end

r = imfilter(f,kd);

%rs1 = edge(r, 'prewitt', 0.25, 'vertical');
rs1 = imfilter(j,fspecial('sobel')'/8,'replicate'); %finds verticale gradients
rs1 = uint8(rs1);%below are the features being used for kmeans
rs2 = red;
rs3 = r;
rs4 =  imfilter(j,fspecial('sobel') /8,'replicate');
rs4 = uint8(rs4);

k1 = reshape(rs1,size(rs1,1)*size(rs1,2),1); %shapes each feature into a usable 1 dimensional vector
k2 = reshape(rs2,size(rs2,1)*size(rs2,2),1);
k3 = reshape(rs3,size(rs3,1)*size(rs3,2),1);
k4 = reshape(rs4,size(rs4,1)*size(rs4,2),1);
list = [k1, k2, k3, k4]; %with k2 and k3, and the clusters set to four, we can see the poles at km1==2.
list = double(list);

%performs k-means.
opts = statset('Display','final'); %other effects on k means
[km1,km2] = kmeans(list,12,'emptyaction', 'singleton','Replicate',5,'Options', opts); %kmeans
plot(list(km1 ==1,1), list(km1 ==1,2), 'r.', list(km1==2,1),list(km1==2,2),'b.',km2(:,1),km2(:,2),'kx'); %graph k-means

xm = reshape(km1,2176,1501); %reshapes the kmeans storage vector
%produces images, pick the one most accurate.
for i =1:12
xm2 = (xm==i); %selects the kmeans cluster closest to the poles (possibly make a loop?)
xm2 = uint8(xm2); %convets xm2 from a logical to a uint8, allows for the next operation.
xm3 = j - xm2*100; %subtracts the previous cluster from the original image.
figure, imshow(xm3);  %produces the image that is very close to where the poles are located
end

No comments:

Post a Comment