%C均值聚类算法
clear
clc
[filename,pathname] = uigetfile('*.jpg','读取图片文件');%选择图片文件
if isequal(filename,0) %判断是否选择
msgbox('没有选择任何图片');
else
pathfile = fullfile(pathname,filename); %获得图片路径
RGB = imread(pathfile); %将图片读入矩阵
YUV = rgb2ycbcr(RGB);%转成YUV
figure;
imshow(YUV); %绘制图片
end
size_image = size(YUV(:,:,1));
YUV = double(YUV);
i = 1:1:size_image(1);
j = 1:1:size_image(2);
data = zeros(size_image(1)*size_image(2),2);
for i = 1:size_image(1); %data为n*m矩阵,n为样本点个数,m为么个特征的维数
for j = 1:size_image(2);
data(j+(i-1)*size_image(2),1) = YUV(i,j,1);
data(j+(i-1)*size_image(2),2) = YUV(i,j,3);
end
end
[center,U,obj_fcn] = fcm(data,4); %模糊C聚类算法
figure
plot(data(:,1),data(:,2),'o');
hold on;
maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2,:) == maxU);
index3 = find(U(3,:) == maxU);
index4 = find(U(4,:) == maxU);
plot(data(index1,1),data(index1,2),'og');
plot(data(index2,1),data(index2,2),'or');
plot(data(index3,1),data(index3,2),'ob');
plot(data(index4,1),data(index4,2),'oy');
%画聚类中心
plot(center(1,1),center(1,2),'xr','MarkerSize',15,'LineWidth',3);
plot(center(2,1),center(2,2),'xb','MarkerSize',15,'LineWidth',3);
plot(center(3,1),center(3,2),'xy','MarkerSize',15,'LineWidth',3);
plot(center(4,1),center(4,2),'xk','MarkerSize',15,'LineWidth',3);
title('分类结果');
hold off;
figure
result = zeros(size_image(1),size_image(2),3)+255;
a1 = fix(index1/size_image(2));
b1 = rem(index1,size_image(2));
sizea1 = size(a1);
sizeb1 = size(b1);
%画第一类点
for i = 1:size_image(1);
temp_a1 = any(a1==i);
if(temp_a1==1)
pos_a1 = find(a1 == i);
size_pos_a1 = size(pos_a1);
b_block = b1(pos_a1(1):(pos_a1(1)+size_pos_a1(2)-1));
for j = 1:size_image(2);
temp_b1 = any(b_block == j);
if(temp_b1 == 1)
result(i,j,:) = [1,0,0];
end
end
end
end
a2 = fix(index2/size_image(2));
b2 = rem(index2,size_image(2));
sizea2 = size(a2);
sizeb2 = size(b2);
%画第二类点
for i = 1:size_image(1);
temp_a2 = any(a2==i);
if(temp_a2==1)
pos_a2 = find(a2==i);
size_pos_a2 = size(pos_a2);
b_block = b2(pos_a2(1):(pos_a2(1)+size_pos_a2(2)-1));
for j = 1:size_image(2);
temp_b2 = any(b_block == j);
if(temp_b2 == 1)
result(i,j,:) = [0,1,0];
end
end
end
end
a3 = fix(index3/size_image(2));
b3 = rem(index3,size_image(2));
sizea3 = size(a3);
sizeb3 = size(b3);
%画第三类点
for i = 1:size_image(1);
temp_a3 = any(a3==i);
if(temp_a3==1)
pos_a3 = find(a3==i);
size_pos_a3 = size(pos_a3);
b_block = b3(pos_a3(1):(pos_a3(1)+size_pos_a3(2)-1));
for j = 1:size_image(2);
temp_b3 = any(b_block == j);
if(temp_b3 == 1)
result(i,j,:) = [0,0,1];
end
end
end
end
imshow(result);
figure
BW1 = im2bw(result);
BW = bwperim(BW1);
imshow(BW)
% I = medfilt2(L); %采用二维中值滤波函数medfilt2对受椒盐噪声干扰的图像滤波
% figure
% imshow(I)
% title('去噪之后的图像')
switch filename
case '1.jpg'
%------------------------------------------------------------------------
% AL-AZHAR UNIVERSITY
% FACULTY OF ENGINEERING
% SYSTEMS & COMPUTERS ENGINEERING Department
%------------------------------------------------------------------------
% Author : Ahmed Samieh Abd El-Wahab
% Date : 14 December 2006
%------------------------------------------------------------------------
% Shapes Classifier
% Step 1: Read image Read in
% Step 2: Convert image from rgb to gray
% Step 3: Threshold the image
% Step 4: Invert the Binary Image
% Step 5: Find the boundaries Concentrate
% Step 6: Determine Shapes properties
% Step 7: Classify Shapes according to properties
% Square = 3
% Rectangular = 2
% Circle = 1
% UNKNOWN = 0
%------------------------------------------------------------------------
% Step 2: Convert image from rgb to gray
% Step 4: Invert the Binary Image
BW = ~ BW;
figure,
imshow(BW),
title('Inverted Binary Image');
% Step 5: Find the boundaries Concentrate only on the exterior boundaries.
% Option 'noholes' will accelerate the processing by preventing
% bwboundaries from searching for inner contours.
[B,L] = bwboundaries(BW, 'noholes');
% Step 6: Determine objects properties
STATS = regionprops(L, 'all'); % we need 'BoundingBox' and 'Extent'
% Step 7: Classify Shapes according to properties
% Square = 3 = (1 + 2) = (X=Y + Extent = 1)
% Rectangular = 2 = (0 + 2) = (only Extent = 1)
% Circle = 1 = (1 + 0) = (X=Y , Extent < 1)
% UNKNOWN = 0
figure
imshow(RGB),
title('Results');
hold on
for i = 1 : length(STATS)
W(i) = uint8(10<abs(STATS(i).BoundingBox(3)-STATS(i).BoundingBox(4)) < 50);
W(i) = W(i) + 2 * uint8((STATS(i).Extent - 1) == 0 );
centroid = STATS(i).Centroid;
switch W(i)
case 1
plot(centroid(1),centroid(2),'wO');
case 2
plot(centroid(1),centroid(2),'wX');
case 3
plot(centroid(1),centroid(2),'wS');
end
end
case {'2.jpg','3.jpg'}
step_r = 1;
step_angle = 0.05;
r_min = 50;
r_max = 60;
thresh = 0.95;
% %%%%%%%%%%%%%%%%%%%%%%%%%%
% input
% BW:二值图像;
% step_r:检测的圆半径步长
% step_angle:角度步长,单位为弧度
% r_min:最小圆半径
% r_max:最大圆半径
% p:阈值,0,1之间的数
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% output
% hough_space:参数空间,h(a,b,r)表示圆心在(a,b)半径为r的圆上的点数
% hough_circl:二值图像,检测到的圆
% para:检测到的圆的圆心、半径
[m,n] = size(BW);
size_r = round((r_max-r_min)/step_r)+1;
size_angle = round(2*pi/step_angle);
hough_space = zeros(m,n,size_r);
[rows,cols] = find(BW);
ecount = size(rows);
% Hough变换
% 将图像空间(x,y)对应到参数空间(a,b,r)
% a = x-r*cos(angle)
% b = y-r*sin(angle)
for i=1:ecount
for r=1:size_r
for k=1:size_angle
a = round(rows(i)-(r_min+(r-1)*step_r)*cos(k*step_angle));
b = round(cols(i)-(r_min+(r-1)*step_r)*sin(k*step_angle));
if(a>0&a<=m&b>0&b<=n)
hough_space(a,b,r) = hough_space(a,b,r)+1;
end
end
end
end
% 搜索超过阈值的聚集点
max_para = max(max(max(hough_space)));
index = find(hough_space>=max_para*thresh);
length = size(index);
hough_circle = false(m,n);
for i=1:ecount
for k=1:length
par3 = floor(index(k)/(m*n))+1;
par2 = floor((index(k)-(par3-1)*(m*n))/m)+1;
par1 = index(k)-(par3-1)*(m*n)-(par2-1)*m;
if((rows(i)-par1)^2+(cols(i)-par2)^2<(r_min+(par3-1)*step_r)^2+5&...
(rows(i)-par1)^2+(cols(i)-par2)^2>(r_min+(par3-1)*step_r)^2-5)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
CSDN海神之光上传的代码均可运行,亲测可用,直接替换数据即可,适合小白; 1、代码压缩包内容 主函数:C_mean.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开C_mean.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描博客文章底部QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作 能量泛函分割:DBSCAN、GAC水平集、snake、分水岭、 阙值分割: 特征增强分割:超像素SFFCM图像分割、关键像素点FLICM图像分割 智能算法图像分割:遗传算法、灰狼算法、人工鱼群、贝叶斯、北方苍鹰、萤火虫、和声搜索、粒子群、蚁群算法
资源推荐
资源详情
资源评论
收起资源包目录
【图像聚类】基于matlab K-means聚类算法路标识别与提取【含Matlab源码 2975期】.zip (11个子文件)
【图像聚类】基于matlab K-means聚类算法路标识别与提取【含Matlab源码 2975期】
运行结果1.jpg 30KB
运行结果6.jpg 60KB
2.jpg 138KB
运行结果5.jpg 129KB
1.jpg 184KB
C_mean.m 9KB
C_mean.asv 6KB
运行结果2.jpg 40KB
运行结果4.jpg 128KB
3.jpg 128KB
运行结果3.jpg 118KB
共 11 条
- 1
资源评论
海神之光
- 粉丝: 3w+
- 资源: 2091
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功