You are on page 1of 29

ĐẠI HỌC BÁCH KHOA ĐIỆN TỬ VIỄN THÔNG

ĐẠI HỌC ĐÀ NẴNG KĨ THUẬT MÁY TÍNH

BÀI TẬP XỬ LÝ ẢNH

Giáo viên hướng dẫn: Hồ Phước Tiến

Sinh viên thực hiện: Nguyễn Huỳnh Tiến

Lớp sinh hoạt: 20KTMT1

Lớp học phần: 20.44

Đà Nẵng, ngày 4 tháng 5 năm 2023


Buổi 1:
Bài 1: Xoay ảnh
Code
img=imread('anh1.JPG');

[rows,cols,z]= size(img);

alpha=45;

rads=2*pi*alpha/360;
rowsf=ceil(rows*abs(cos(rads))+cols*abs(sin(rads)));
colsf=ceil(rows*abs(sin(rads))+cols*abs(cos(rads)));

C=uint8(zeros([rowsf colsf 3 ]));

xo=ceil(rows/2);
yo=ceil(cols/2);

midx=ceil((size(C,1))/2);
midy=ceil((size(C,2))/2);

for i=1:size(C,1)
for j=1:size(C,2)

x= (i-midx)*cos(rads)+(j-midy)*sin(rads);
y= -(i-midx)*sin(rads)+(j-midy)*cos(rads);
x=round(x)+xo;
y=round(y)+yo;

if (x>=1 && y>=1 && x<=size(img,1) && y<=size(img,2) )


C(i,j,:)=img(x,y,:);
end

end
end
subplot(1,2,1);
imshow('anh1.JPG');
title('anh goc');
subplot(1,2,2);
imshow(C);
title('anh xoay');
Kết quả

Bài 1b: Tạo ảnh Gif


imgList = {'kodim1.png', 'kodim2.png', 'kodim3.png',
'kodim4.png', 'kodim5.png',
'kodim6.png', 'kodim7.png'};
n=numel(imgList);

dem=0;
while(dem ~= 7)
for i=1:n
imshow(imgList{i}));
pause(1);
end
cnt=cnt+1;
end
Buổi 2: Chia 1 ảnh thành 16 phần rồi xáo trộn
Code
image = imread('kodim19.png');
[height, width, ~] = size(image);
w = width / 4;
h = height / 4;
cnt = 1;
img = zeros([16, h, w, 3], 'uint8');
arr = randperm(16) - 1;
for k = 1:4
for r = 1:4
for x = 1:h
for y = 1:w
img(cnt, x, y, :) = image((h*(k-1)+x), (w*(r-
1)+y), :);
end
end
cnt = cnt + 1;
end
end
I = zeros([height, width, 3], 'uint8');
i = 1;
for k = 1:4
for r = 1:4
for x = 1:h
for y = 1:w
I((h*(k-1)+x), (w*(r-1)+y), :) = img(arr(i)+1,
x, y, :);
end
end
i = i + 1;
end
end
subplot(1,2,1);
imshow(image);title('original Image')
subplot(1,2,2);
imshow(I);title('Shuffled Image')

Kết quả
Buổi 3:
Bài 1: Contrast stretched Image
Code:
I=imread('cameraman.tif');
I=im2double(I);
I = (I * 2) / max(I(:));
I_str = 0;
[row,col] = size(I);
LT = 100;
UT = 150;
for i=1:row
for j=1:col
if I(i,j)<= LT
I_str(i,j)=0.5*I(i,j);
else if I(i,j)<= UT
I_str(i,j)=2*(I(i,j)-LT)+(0.5*LT);
else
I_str(i,j)=0.5*(I(i,j)-UT)+0.5*LT+2*(UT-LT);
end
end
end
end
figure
montage({I, I_str}, 'Size', [1 2]);

title('Original Image Contrast stretched Image');

Kết quả
Bài 2: Tìm và vẽ Histogram của một ảnh xám bất kì

Code
img =
imread('https://media.istockphoto.com/id/859528824/vi/anh/t%C3%B
2a-nh%C3%A0-melbourne-
docklands.jpg?s=612x612&w=0&k=20&c=cZHrZF4HghoJ7uKZ7WGtRav8Xg0yq
aGeWcP0m9lmSVw=');
img=rgb2gray(img);
[height, width] = size(img);
num_bins = 256;
histogram = zeros(1, num_bins);

for i = 1:height
for j = 1:width
pixel_value = img(i, j);
histogram(pixel_value + 1) = histogram(pixel_value + 1)
+ 1;
end
end
figure;
subplot(1,2,1);
imshow(img);
title('Ảnh Xám');

subplot(1,2,2);
bar(0:num_bins-1, histogram);
xlim([0 num_bins-1]);
title('Histogram');
xlabel('Giá tri pixel');
ylabel('Tần số');
Kết quả

Bài 3: Cân bằng histogram


Code
img =
imread('https://media.istockphoto.com/id/859528824/vi/anh/t%C3%B
2a-nh%C3%A0-melbourne-
docklands.jpg?s=612x612&w=0&k=20&c=cZHrZF4HghoJ7uKZ7WGtRav8Xg0yq
aGeWcP0m9lmSVw=');
img = rgb2gray(img);
h = imhist(img);
cdf = cumsum(h) / numel(img);
img_eq = uint8(255 * cdf(img + 1));
subplot(221); imshow(img); title('Anh goc');
subplot(223); imshow(img_eq); title('Anh da can bang
histogram');
subplot(222); imhist(img);
subplot(224); imhist(img_eq);

Kết quả
Buổi 4:
Bài 1a: Sử dụng bộ lọc
Code
I=imread('https://media.istockphoto.com/id/859528824/vi/anh/t%C3
%B2a-nh%C3%A0-melbourne-
docklands.jpg?s=612x612&w=0&k=20&c=cZHrZF4HghoJ7uKZ7WGtRav8Xg0yq
aGeWcP0m9lmSVw=')
I_gray=rgb2gray(I);
H1 = ones(3,3)/9;
H2 = ones(5,5)/25;
H3 = ones(9,9)/81;

Y1 = uint8(conv2(I_gray,H1,'same'));
Y2 = uint8(conv2(I_gray,H2,'same'));
Y3 = uint8(conv2(I_gray,H3,'same'));

figure
subplot(221);
imshow(I_gray); xlabel('Anh goc');
subplot(222);
imshow(Y1); xlabel('Anh qua bo loc H1');
subplot(223);
imshow(Y2); xlabel('Anh qua bo loc H2');
subplot(224);
imshow(Y3); xlabel('Anh qua bo loc H3');

Kết quả

Bài 1b: Sử dụng bộ lọc nhiễu


Code
img=imread('cameraman.tif');
img = double(img);
sigma = 50;
noise = sigma*randn(size(img));
noisy_image = img + noise;
H1 = [-1 0 1;-1 0 1;-1 0 1];
H2 = [-1 -1 -1;0 0 0;1 -1 -1];
YH1 = conv2(noisy_image,H1,'same');
YH2 = conv2(noisy_image,H2,'same');
YH3 = abs(YH1) + abs(YH2);
subplot(2,3,1);
imshow(image);
title('anh goc');
subplot(2,3,2);
imshow(noisy_image);
title('anh nhieu');
subplot(2,3,3);
imshow(YH3,[]);
title('anh su dung bo loc H');
G1 = [-1 0 1;-2 0 2; -1 0 1];
G2 = [-1 -2 -1; 0 0 0; 1 2 1];
YG1 = conv2(noisy_image,G1,'same');
YG2 = conv2(noisy_image,G2,'same');
YG3 = abs(YG1) + abs(YG2);
subplot(2,3,4);
imshow(YG3,[]);
title('anh su dung bo loc G');

Kết quả

Bài 2: Cho ảnh xám I vàH=1/4*[1 2 1] (1 cột), H2=H1’


a) Tính Y1=H1*Y, Y2= Y1*H2.
b) Cho H=1/16[ 1 2 1; 2 4 2; 1 2 1]. Tính Y3=I*H
c) So sánh Y2 vàY3
Code
I = imread('cameraman.tif');
H1=[1 2 1]/4;
H2= H1';
H3 = [1 2 1 ; 2 4 2 ;1 2 1]/16;
Y1 = conv2(I,H1,'same');
Y2 = conv2(Y1,H2, 'same');
Y3 = conv2(I,H3, 'same');
subplot(221), imshow(Y1,[]), title('Y1');
subplot(222), imshow(Y2,[]), title('Y2');
subplot(223), imshow(Y3,[]), title('Y3');
subplot(224), imshowpair(Y2,Y3,'montage'), title('so sanh Y2 va
Y3');

Kết quả

Nhận xét: Với ảnh là Y2= I* H1*H1’ mà H1 và H1’ là 2 ma trận chuyển vị
của nhau cho nên ảnh Y2 gần như bằng ảnh xám I.
Còn Y3 áp dụng bộ lọc H=1/16[ 1 2 1; 2 4 2; 1 2 1] là một bộ lọc thông
thấp có tác dụng giảm nhiễu và làm mịn ảnh.
Bài 3: Cho ảnh xám I
H1=ones( 3,3)/9; H2=ones( 5,5)/25; H3=ones( 9,9)/81
Lọc ảnh bởi bộ lọc 1, 2, 3 Hiển thị kết quả và nhận xét. Làm lại câu trên
thay bằng ảnh I có nhiễu gauss
Code
I = imread('cameraman.tif');
H1=ones(3,3)/9;
H2=ones(5,5)/25;
H3=ones(9,9)/81;
Y1 = conv2(I,H1,'same');
Y2 = conv2(I,H2, 'same');
Y3 = conv2(I,H3, 'same');
subplot(221), imshow(Y1,[]), title('Y1');
subplot(222), imshow(Y2,[]), title('Y2');
subplot(223), imshow(Y3,[]), title('Y3');

Kết quả

Buổi 5:
Bài 1: Sử dụng bộ lọc Median
Code
I = imread('cameraman.tif');

I1=imnoise(I,'salt & pepper');


%a)

H = [1 2 1; 2 4 2 ; 1 2 1]/16;
Y1 = conv2(I1,H);

%b)
Y2 = medfilt2(I1,[3 3]);
%c)
I2 = imnoise(I1,'salt & pepper', 0.05);
Y3 = medfilt2(I2,[5 5]);
subplot(2,2,1);
imshow(I1);
title('anh nhieu');
subplot(2,2,2);
imshow(Y1,[]);
title('anh loc H');
subplot(2,2,3);
imshow(Y2,[]);
title('anh loc dung median');
subplot(2,2,4);
imshow(Y3,[]);
title('anh loc dung median');

Kết quả
Ta thấy khi áp dụng bộ lọc median thì các thành phần nhiễu đã giảm
đáng kể sau 2 kiểu lọc nhưng đổi lại thì ảnh lại mờ không giống như
hình ảnh ban đầu

Bài 2: Ảnh Mosaic


Code
img = imread('thap.png');
[h, w, t] = size(img)
R = img(:,:,1);
G = img(:,:,2);
B = img(:,:,3);
for i=2:2:h
R(i,:) = 0;
end
for j=2:2:w
R(:,j) = 0;
end
for i=1:2:h
for j=1:2:w
G(i,j) = 0;
end
end
for j=2:2:w
for i=2:2:h
G(i,j) = 0;
end
end
for i=1:2:h
B(i,:) = 0;
end
for j=1:2:w
B(:,j) = 0;
end
I = img;
I(:,:,1) = R;
I(:,:,2) = G;
I(:,:,3) = B;figure
imshow(I,[])
WRB = [1 2 1;2 4 2;1 2 1] / 4;
WG = [0 1 0; 1 4 1; 0 1 0] / 4;
R = conv2(R,WRB,'same');
G = conv2(G, WG,'same');
B = conv2(B, WRB,'same');
I(:,:,1) = R;
I(:,:,2) = G;
I(:,:,3) = B;
figure
imshow(I,[])
Kết quả

Buổi 6
Bài 1: Demosaicing bằng phương pháp Alleyson
Code
img = imread('kodim19.png');
[r,c,k] = size(img);
R = img(:,:,1);
G = img(:,:,2);
B = img(:,:,3);
for i = 2:2:r
R(i,:) = 0;
end
for j = 2:2:c
R(:,j) = 0;
end
for i = 1:2:r
for j = 1:2:c
G(i,j) = 0;
end
end
for j = 2:2:c
for i = 2:2:r
G(i,j) = 0;
end
end
for i = 1:2:r
B(i,:) = 0;
end
for j = 1:2:c
B(:,j) = 0;
end
I = uint8(zeros(r,c,k));
I(:,:,1) = R;
I(:,:,2) = G;
I(:,:,3) = B;
FRB = [1 2 1; 2 4 2; 1 2 1]/4;
FG = [0 1 0; 1 4 1;0 1 0]/4;
FL = [-2 3 -6 3 -2; 3 4 2 4 3; -6 2 48 2 -6; 3 4 2 4 3; -2 3 -6
3 -2]/64;
R1=conv2(double(R),double(FRB),'same');
G1=conv2(double(G),double(FG),'same');
B1=conv2(double(B),double(FRB),'same');
I1 = I;
I1(:,:,1) = R1;
I1(:,:,2) = G1;
I1(:,:,3) = B1;
Itemp = R + B + G;
luminnace = conv2(Itemp,FL,'same');
multi_cr = double(zeros(r,c));
multi_cr = double(Itemp) - luminnace;
C = img;
mR = zeros(r,c);mG = zeros(r,c);mB = zeros(r,c);
mR(1:2:r,1:2:c) = 1;
mG(1:2:r,2:2:c) = 1;mG(2:2:r,1:2:c) = 1;
mB(2:2:r,2:2:c) = 1;
C1 = multi_cr .* mR;
C2 = multi_cr .* mG;
C3 = multi_cr .* mB;
img2 = zeros(r,c,3);
img2(:,:,1) = C1;
img2(:,:,2) = C2;
img2(:,:,3) = C3;
WRB = [1 2 1;2 4 2;1 2 1] / 4;
WG = [0 1 0; 1 4 1; 0 1 0] / 4;
res = img;
res(:,:,1) = conv2(C1,WRB,'same') + luminnace;
res(:,:,2) = conv2(C2, WG,'same') + luminnace;
res(:,:,3) = conv2(C3, WRB,'same') + luminnace;
imshowpair(res,img,"montage")
title('anh demosaic va anh goc')

Kết quả

Bài 2: Cho ảnh xám I, tìm và hiển thị biên độ và phổ của ảnh xám đó.
Thực hiện lọc ảnh I bằng bộ lọc thông thấp lí tưởng (trong miền tần
số)
Code
I = imread('https://r0k.us/graphics/kodak/kodak/kodim19.png');
I = rgb2gray(I);
s = fft2(double(I));
% Quang phổ
figure
imagesc(log(abs(fftshift(s))));
title('Spectre');
% Thiết kế bộ lọc thông thấp lí tưởng
figure
Fil = zeros(size(s));
[M N] = size(s);
r = (M+1)/2;
c = (N+1)/2;
[X Y] = meshgrid([1:N],[1:M]);
radius = sqrt((X-c).^2 + (Y-r).^2);
D0 = min(N/8,M/8);
Fil(radius < D0) = 1;
imshow(Fil,[]);
title('BỘ LỌC');
S1 = fftshift(s).*Fil;
S1 = ifftshift(S1);
I1 = ifft2(S1);
% show ảnh gốc
figure
subplot(121)
imshow(I);
title(' ẢNH GỐC ');
subplot(122)
imshow(real(I1),[]);
title(' ẢNH QUA BỘ LỌC ');

Kết quả

b) Thực hiện lọc ảnh I bằng bộ lọc thông thấp Butterworth (trong
miền tần số).
Code
I = imread('https://r0k.us/graphics/kodak/kodak/kodim19.png');
img = rgb2gray(I);
s = fft2(double(img));
figure
imagesc(log(abs(fftshift(s))));
title('Spectre');
figure
subplot(131)
imshow(I);
title('ẢNH GỐC');
Fil = zeros(size(s));
[M N] = size(s);
r = (M+1)/2;
c = (N+1)/2;
D0 = min(N/8,M/8);
for i = 1:N
for j = 1:M
Duv = sqrt((i-c)*(i-c) + (j-r)*(j-r));
Fil(j,i) = 1/(1+((Duv/D0).^2));
end
end
subplot(133)
imshow(Fil,[]);
title('BỘ LỌC');
S1 = fftshift(s).*Fil;
S1 = ifftshift(S1);
I1 = ifft2(S1);
subplot(132)
imshow(real(I1),[]);
title('ẢNH BỘ LỌC');

Kết quả

c) Thực hiện lọc ảnh I bằng bộ lọc Gaussian (trong miền tần số).
Hiển thị và nhận xét kết quả thu được.
Code
I = imread('https://r0k.us/graphics/kodak/kodak/kodim19.png');
I = rgb2gray(I);
s = fft2(double(I));
figure
subplot(131)
imshow(I);
title('ẢNH GỐC');
Fil = zeros(size(s));
[M N] = size(s);
r = (M+1)/2;
c = (N+1)/2;
D0 = min(N/8,M/8);
for i = 1:N
for j = 1:M
Duv = sqrt((i-c)*(i-c) + (j-r)*(j-r));
Fil(j,i) = exp(-Duv*Duv/(2*D0*D0));
end
end
subplot(133)
imshow(Fil,[]);
title('BỘ LỌC');
S1 = fftshift(s).*Fil;
S1 = ifftshift(S1);
I1 = ifft2(S1);
subplot(132)
imshow(real(I1),[]);
title('ẢNH BỘ LỌC');

Kết quả

Nhận xét:
Bộ lọc thông thấp lý tưởng là một bộ lọc tín hiệu được sử dụng để loại bỏ
các tần số cao hơn một tần số cắt (cutoff frequency) nhất định trong tín
hiệu đầu vào. Bộ lọc này được gọi là "lý tưởng" vì nó là bộ lọc có khả
năng loại bỏ tất cả các tần số cao hơn tần số cắt và không làm thay đổi
các tần số thấp hơn. Bản chất bộ lọc thông thấp là làm mịn ảnh nên có
thể dễ dàng nhận thấy ảnh sau khi qua bộ lọc mờ hơn ảnh gốc ban đầu.
Tuy nhiên, bộ lọc thông thấp lý tưởng có một số hạn chế khi được áp
dụng trong thực tế. Trong quá trình lọc, bộ lọc này gây ra hiện tượng
overshoot (điều này xảy ra khi tín hiệu đầu vào có tần số gần bằng tần số
cắt) và ringing (điều này xảy ra khi tín hiệu đầu vào có tần số cao hơn tần
số cắt).
Bài 3: Bộ lọc thông thấp Butterworth, gauss cho ảnh mosaic
Phần code: bộ lọc butterworth
img = imread('kodim19.png');
img=double(img);
[m,n,h]=size(img);
mr=zeros(m,n);
mr(1:2:end,1:2:end)=1;
mb=zeros(m,n);
mb(2:2:end,2:2:end)=1;
mg=1-mr-mb;
Im=img(:,:,1).*mr+img(:,:,2).*mg+img(:,:,3).*mb;
% Compute the 2D Fourier transform of the image
s = fft2(double(Im));
figure
imagesc(log(abs(fftshift(s))));
title('Spectre');
figure
subplot(131)
imshow(Im,[]);
title('ẢNH GỐC');
Fil = zeros(size(s));
[M N] = size(s);
r = (M+1)/2;
c = (N+1)/2;
D0 = min(N/8,M/8);
for i = 1:N
for j = 1:M
Duv = sqrt((i-c)*(i-c) + (j-r)*(j-r));
Fil(j,i) = 1/(1+((Duv/D0).^2));
end
end
subplot(133)
imshow(Fil,[]);
title('BỘ LỌC');
S1 = fftshift(s).*Fil;
S1 = ifftshift(S1);
I1 = ifft2(S1);
subplot(132)
imshow(real(I1),[]);
title('ẢNH BỘ LỌC');

Kết quả
Phần code: Bộ lọc gaussian
img = imread('kodim19.png');
img=double(img);
[m,n,h]=size(img);
mr=zeros(m,n);
mr(1:2:end,1:2:end)=1;
mb=zeros(m,n);
mb(2:2:end,2:2:end)=1;
mg=1-mr-mb;
Im=img(:,:,1).*mr+img(:,:,2).*mg+img(:,:,3).*mb;
% Compute the 2D Fourier transform of the image
s = fft2(double(Im));
figure
subplot(131)
imshow(Im,[]);
title('ẢNH GỐC');
Fil = zeros(size(s));
[M N] = size(s);
r = (M+1)/2;
c = (N+1)/2;
D0 = min(N/8,M/8);
for i = 1:N
for j = 1:M
Duv = sqrt((i-c)*(i-c) + (j-r)*(j-r));
Fil(j,i) = exp(-Duv*Duv/(2*D0*D0));
end
end
subplot(133)
imshow(Fil,[]);
title('BỘ LỌC');
S1 = fftshift(s).*Fil;
S1 = ifftshift(S1);
I1 = ifft2(S1);
subplot(132)
imshow(real(I1),[]);
title('ẢNH BỘ LỌC');

Kết quả
Buổi 7: Tính toán biến đổi Fourier
Code
img1 = imread('https://hinhnen4k.com/wp-
content/uploads/2023/02/anh-gai-xinh-vn-2.jpg');
img2 = imread('https://static-
6.happynest.vn/storage/uploads/2020/04/9866e406a551bf3268a0fa93c
31c62a9.png');
img3 = imread('https://vnn-imgs-
f.vgcloud.vn/2022/02/10/10/273240071-2239446092859843-
7097566017885036718-n.jpg');
img4 = imread('https://hacom.vn/media/news/1304_hanoicomputer-
ghe-gaming-ghe-choi-game-tot-1.jpg');
img5 = imread('https://cdn.sforum.vn/sforum/wp-
content/uploads/2022/12/tft-kaisa.jpg');
Arr = [2 4 6 8 10];
Arr = Arr(randperm(length(Arr)))
figure
subplot(3,4,1);imshow(img1,[]);
title('Con Nguoi');
subplot(3,4,3);imshow(img2,[]);
title('Nha Cua');
subplot(3,4,5);imshow(img3,[]);
title('Cay Mai');
subplot(3,4,7);imshow(img4,[]);
title('Goc Lam Viec');
subplot(3,4,9);imshow(img5,[]);
title('Hoat Hinh');
if size(img1, 3) == 3
img1 = rgb2gray(img1);
end
if size(img2, 3) == 3
img2 = rgb2gray(img2);
end
if size(img3, 3) == 3
img3 = rgb2gray(img3);
end
if size(img4, 3) == 3
img4 = rgb2gray(img4);
end
if size(img5, 3) == 3
img5 = rgb2gray(img5);
end

F1 = fft2(double(img1));
F2 = fft2(double(img2));
F3 = fft2(double(img3));
F4 = fft2(double(img4));
F5 = fft2(double(img5));

F1 = fftshift(F1);
F2 = fftshift(F2);
F3 = fftshift(F3);
F4 = fftshift(F4);
F5 = fftshift(F5);

F_mag1 = abs(F1);
F_mag2 = abs(F2);
F_mag3 = abs(F3);
F_mag4 = abs(F4);
F_mag5 = abs(F5);

subplot(3,4,Arr(1));imshow(log(1+F_mag1), []);
subplot(3,4,Arr(2));imshow(log(1+F_mag2), []);
subplot(3,4,Arr(3));imshow(log(1+F_mag3), []);
subplot(3,4,Arr(4));imshow(log(1+F_mag4), []);
subplot(3,4,Arr(5));imshow(log(1+F_mag5), []);

Kết quả

Nhận xét: Những hình ảnh có nhiều góc cạnh thì khi chuyển sang
biểu diễn ở miền tần số sẽ có nhiều đường sáng chạy qua tâm,
những đường đó thể hiện sự chuyển đổi mức năng lượng( các góc
cạnh) từ vùng này sang vùng khác trong ảnh.

Buổi 8:
Bài 1: Kmeans cluster
Code
N = 100;
K = 4;
center = [2 2];
x = generate_data(N,0.5,center);
center = [-2 2];
x = [x;generate_data(N,1,center)];
center = [2 -2];
x = [x;generate_data(N,0.7,center)];
center = [-2 -2];
x = [x;generate_data(N,0.6,center)];

figure
text(x(:,1),x(:,2),'*','color','b');

axis([-4 4 -4 4]);
opts=statset('Display','final');

[idx,C]=kmeans(x,4,'Distance','cityblock','Replicates',5,'Option
s',opts);

plot(x(idx==1,1),x(idx==1,2),'r.','MarkerSize',12);

hold on;

plot(x(idx==2,1),x(idx==2,2),'b.','MarkerSize',12);

plot(x(idx==3,1),x(idx==3,2),'g.','MarkerSize',12);

plot(x(idx==4,1),x(idx==4,2),'y.','MarkerSize',12);

plot(C(:,1),C(:,2),'Kx','MarkerSize',15,'LineWidth',3);

legend('Cluster 1','Cluster 2','Cluster 3','Cluster


4','Centroids', 'Location','NW');

title('Cluster Assignments and centroids');

hold off;

for i=1:size(C, 1)

display(['Centroid ', num2str(i), ': X1 = ', num2str(C(i, 1)),


'; X2 = ', num2str(C(i, 2))]);

end
Kết quả
Ta thấy Kmeans cluster hoạt động dựa trên việc tìm kiếm trung tâm
của các cụm để phân tách dữ liệu.

Buổi 9: Nhận diện biển số xe bằng opening và closing


Code
img = imread('target7.jpg');
gray = rgb2gray(img);
bw = imbinarize(gray);
SE2 = strel('rectangle',[10 5]);
SE = strel('rectangle', [4 4]);
dilate = imdilate(bw, SE);
dilate = imdilate(dilate, SE);
erode = imerode(dilate, SE2);
erode = imerode(erode, SE2);
cc = bwconncomp(erode);
stats = regionprops(cc, 'BoundingBox');
figure;
imshow(img);
hold on;
for i = 1:numel(stats)
rectangle('Position', stats(i).BoundingBox, 'EdgeColor',
'r', 'LineWidth', 2);
end
Kết quả

You might also like