0% found this document useful (0 votes)
31 views4 pages

K Mean Class

The document contains code for image processing and analysis tasks including: 1. Loading an image, converting it to grayscale, and applying k-means clustering to segment it into 3 classes 2. Applying thresholding to binarize an image based on a threshold value 3. Computing the image histogram and applying histogram equalization for contrast adjustment
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

K Mean Class

The document contains code for image processing and analysis tasks including: 1. Loading an image, converting it to grayscale, and applying k-means clustering to segment it into 3 classes 2. Applying thresholding to binarize an image based on a threshold value 3. Computing the image histogram and applying histogram equalization for contrast adjustment
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

@@KmeanClass.

m
im=imread('image9.jpg');
im_gray = rgb2gray(im);
[r c] = size(im_gray);
inputdata =
reshape(im_gray,r*c,1);
lab = kmeans(double(inputdata),3);
res = reshape(lab,r,c);
imagesc(res);

/////////
im=imread('image9.jpg');
[r c p] = size(im);
inputdata = reshape(im,r*c,3);
lab = kmeans(double(inputdata),3);
res = reshape(lab,r,c);
imagesc(res);

@@
im=imread('image3.jpg');
%figure(1);
%imshow(im);
im_gray = rgb2gray(im);
%figure(2);
%imshow(im_gray);
[r c] = size(im_gray);
T= 130;
result = zeros(r,c);
for i=1:r
for j=1:c
if im_gray(i,j)< T
result(i,j) = 0;
else
result(i,j)=255;
end
end
end
t= zeros(256,1);
right = [];
left = [];
H=zeros(256,1);
for i=1:r
for j=1:c

H(im_gray(i,j)+1)=H(im_gray(i,j)+1)+1;
end
end
P=zeros(256,1);
for i=1:256
%?????????????
P(i) = H(i)/(r*c);
end
PS=zeros(256,1);
for i=1:256
%?????????????
if i==1
PS(i)=P(i)
else
PS(i)=PS(i-1)+P(i);
end
end
for k=1:256
inl = 1;
inr = 1;
for i=1:r
for j=1:c
if im_gray(i,j)< k-1
left(inl)=
im_gray(i,j);
inl = inl + 1;
else
right(inr)=
im_gray(i,j);
inr = inr + 1 ;

right= [];
left = [];

end
end
end

end

%w1
w1=PS(k);
%w2
w2=PS(256)-PS(k);

[th in] = min(t1);


res = im2bw(im_gray, in/255);
figure(3);
imshow(res);

lstd = std(double(left));
rstd = std(double(right));
t2(k)=(w1*lstd)+(w2*rstd);
t1(k)= lstd+rstd;

[thr ind] = min(t2);


res = im2bw(im_gray, ind/255);
figure(4);
imshow(res);

%???????
clc
clear
clf
im = imread('image7.jpg');
figure(1);
imshow(im);
im_gray = rgb2gray(im);
figure(2);
imshow(im_gray);
m=[1/9,1/9,1/9;1/9,1/9,1/9;1/9,1/9,1/9];
res = conv2(im_gray, m);
figure(3);
imshow(res);
imagesc(res);

@@HA
clear
clc
%622558week3
%1 read immage
im = imread('image1.jpg');
im_gray = rgb2gray(im);
h = zeros(256,1);

@@meanFitter
im = imread('image5.jpg');
figure(1);
imshow(im);
im_gray = rgb2gray(im);
figure(2);
imshow(im_gray);
%e = [1,4,1;5,7,2;8,9,8];
[m n]=size(im_gray);

[r c] = size(im_gray);
for i=1:r
for j=1:c
h(im_gray(i,j)+1)
=h(im_gray(i,j)+1)+1;
end
end

%hx histro
for i=2: m-1
for j=2 :n-1
t(1) =im_gray(i-1,j-1);
t(2) =im_gray(i-1,j);
t(3) =im_gray(i-1,j+1);
t(4) =im_gray(i,j-1);
t(5) =im_gray(i,j);
t(6) =im_gray(i,j+1);
t(7) =im_gray(i-1,j-1);
t(8) =im_gray(i+1,j);
t(19) =im_gray(i+1,j+1);
t2 = sort(t);
im_gray(i,j) = t2(5);
end
end
m=[1/9,1/9,1/9;1/9,1/9,1/9;1/9,1/9,1/
9];
res = conv2(im_gray, m);
figure(3);
imagesc(res);

hx = zeros(256,1);
for i=1:256
if(i==1)
hx(i) = h(i);
else
hx(i) = hx(i-1)+h(i);
end
end
%3 HA
%new_hx = zeros(256,1);
result = zeros(r,c);
t=0;
for i=1 :r
for j=1 :c
t =
hx(im_gray(i,j)+1)*255/hx(256);
if (t > 255) t=255; end;
if (t < 0) t=0; end;
result(i,j)=uint8(t);
end
end
figure(1);
imshow(im_gray);
figure(2);
imshow(uint8(result));

%3
clc
clear
clf
im = imread('image5.jpg');
%figure(1);
%imshow(im);

%3
clc
clear
clf
im = imread('image5.jpg');
%figure(1);
%imshow(im);

im_gray = rgb2gray(im);
im_gray = rgb2gray(im);
%figure(2);
%imshow(im_gray);
m=[-1 0 1;-1 0 1;-1 0 1];
res = conv2(im_gray, m);
figure(3);
imagesc(res);
m2=[-1 -1 -1;0 0 0;1 1 1];
res2 = conv2(im_gray, m2);
figure(4);
imagesc(res2);
sumXY = (res+res2)/2;
%res3 = conv2(im_gray, sumXY);
figure(5);
imagesc(sumXY);

/////////////
%4
clc
clear
clf
im = imread('image5.jpg');
%figure(1);
%imshow(im);
im_gray = rgb2gray(im);
%figure(2);
%imshow(im_gray);
gx=[-1 0 1;-1 0 1;-1 0 1];
gy=[-1 -1 -1;0 0 0;1 1 1];

%figure(2);
%imshow(im_gray);
m=[-1 0 1;-1 0 1;-1 0 1];
res = conv2(im_gray, m);
figure(3);
imagesc(res);
m2=[-1 -1 -1;0 0 0;1 1 1];

res2 = conv2(im_gray, m2);


figure(4);
imagesc(res2);
sumXY = (res+res2)/2

//////
res1 = conv2(im_gray, gx);
res2 = conv2(im_gray, gy);
[m n]=size(res1);
for i=1:m
for j=1:n
end
end

figure(4);
imagesc(res2);

You might also like