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

Tuesday, May 28, 2013

Finishing up

Below is a photograph where telephone pole locations have been removed from the image for easier viewing. All of the poles that are undamaged should be completely vertical while poles that are tilted or non-existant have either been destroyed or knocked down due to the storm. The completed code will published within the next day or so.

Figure 1: Utility poles marked by black lines (NOTE!: There is a lot of clutter that will be fixed in later versions of the program) The lines that are not perfectly or almost vertical are poles that have been damaged in the storm.




Wednesday, May 22, 2013

Lab Notes 5/22/2013 (WEEK 8 LAB)

Quick conference with James ( Our TA), one things has been noted that "not using the plot function for more than 3 times, otherwise it will take more than half an hour to do it instead of 5 seconds of plotting three elements in one plot".

With Louis' new finding, as in resizing everything and reshape after words, he did get something interesting and we got the confidence of finishing the research.

Right now, we have three clusters,  and we might do more to make our plot more accurate.


Testing Notes:

1) failed to plot different layers


Conference (as well as testing/solving) with Dr. Anu :

Basically, we convert linear scales into one dimension, and we should get something like one vector with one dimension.

Procedure:

1) pick one value, or make one value to determine the category as in the classification.
2) determine the columns we have, by code " min(km1) " and  " max(km1) "
3) get the value by " sum(km1== 1)" and " sum(km1== 2)" and " sum(km1== 3)", so that the returning number would be the number we need, or the value corresponding to the consecutive 1, 2, and 3.
4) reshape the thing we get, with code "reshape( xxxx,,xxxx)"
5) resize the value after reshape and multiplication  " imresize(xxx)", then use the plot function
" plot (i2=1, 'r')  "


***check the matlab built- in function, "getpixelposition" , which gets an HG object in pixel units.


location1 = find(k3 == 1);
location2 = find(k3 == 2);
location3 = find(k3 == 3);

should find indices where the values are found within k3, saves to location into a column vector

Code for Wednesday 5/22/2013

clc
close all
clear all

i = imread('Image1.tif');

j = i(:, :, 1:3);
red = i(:,:,1);
%clearvars i;
x = rgb2gray(j);
grad = edge(x, 'canny');
gradient = grad;
y = fspecial('laplacian', 0.9);
d = fspecial('gaussian', [3 3], 500);
logs = fspecial('log', [3,3],0.5);
f = imfilter(x,y);

%fun = @(block_struct)block_struct.data;


r = imfilter(f,d);
logf = imfilter(r*4,logs);
%figure, imshow(r*4);
clearvars f;
clearvars y;
clearvars d;
clearvars j;
clearvars grad;
b = imresize(4*r,0.5); %numbers for the filtered image
c = imresize(gradient,0.5); %numbers for gradient.
e = imresize(red,0.5); %color red resized
f1 = imresize(logf,0.5);

rs1 = reshape(b,size(b,1)*size(b,2),1); %creates an m by n matrix of size
rs2 = reshape(c, size(c,1)*size(c,2),1); %creates an m by n matrix of the edge
rs2 = uint8(rs2);
rs3 = reshape(e,size(e,1)*size(e,2),1);
res3 = uint8(rs3);
rs4 = reshape(f1,size(f1,1)*size(f1,2),1);

%plot(x);
%hold on;

list = [rs1,rs4,rs3];
list = double(list);
clearvars rs1;
clearvars rs2;
clear rs3;
clearvars b;
clearvars c;
opts = statset('Display','final'); %other effects on k means
[km1,km2] = kmeans(list,3,'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

%have to resize km1 and km2, convert them to comparable size elements, and
%then imshow them.

k3= reshape(km1, 4351,3001);
k3 = imresize(k3,2);
imshow(k3);

Friday, May 17, 2013

Group meeting, Week 7 Friday (5/17/2013)

Focused on the Kmeans crusting.

This extra meeting is set to help us finish the researching and testing procedure of the kmeans clusting.

1) test of setting kmeans as in :

[km1, km2]= kmeans(list,2,'emptyaction', 'singleton', 'replicates', 3,'Option', opts); 

*worked 

2) problem :
Having the same value after various iteractions.
Looking for solution.
It actually gives a simple clusting model, but we failed to apply it to the actual image.




Do list:

1) More features, like, color, sharpness, other operator. ( try more filters, and figure them out )
2) "blockproc" function

5/17/2013 K-Means continued

clc
close all
clear all

i = imread('Image1.tif');

j = i(:, :, 1:3);
red = i(:,:,1);
clearvars i;
x = rgb2gray(j);
grad = edge(x, 'canny');
gradient = grad;
clearvars grad;
y = fspecial('laplacian', 0.9);
d = fspecial('gaussian', [3 3], 500);
f = imfilter(x,y);

%fun = @(block_struct)block_struct.data;


r = imfilter(f,d);
%figure, imshow(r*4);
clearvars f;
clearvars y;
clearvars d;
clearvars j;
clearvars grad;
b = imresize(4*r,0.5); %numbers for the filtered image
c = imresize(gradient,0.5); %numbers for gradient.
e = imresize(red,0.5); %color red resized

rs1 = reshape(b,size(b,1)*size(b,2),1); %creates an m by n matrix of size
rs2 = reshape(c, size(c,1)*size(c,2),1); %creates an m by n matrix of the edge
rs2 = uint8(rs2);
rs3 = reshape(e,size(e,1)*size(e,2),1);
res3 = uint8(rs3);

plot(x);
hold on;

list = [rs1,rs3];
list = double(list);
clearvars rs1;
clearvars rs2;
clear rs3;
clearvars b;
clearvars c;
opts = statset('Display','final');
[km1,km2] = kmeans(list,2,'emptyaction', 'singleton','Replicate',5,'Options', opts);
plot(list(km1 ==1,1), list(km1 ==1,2), 'r.', list(km1==2,1),list(km1==2,2),'b.',km2(:,1),km2(:,2),'kx');