• #@
    了解作者
  • matlab
    开发工具
  • 4.5MB
    文件大小
  • zip
    文件格式
  • 1
    收藏次数
  • 1 积分
    下载积分
  • 3
    下载次数
  • 2019-07-31 20:48
    上传日期
采用聚类的方法对路标进行提取,效果不错。
03-聚类.zip
  • 03-聚类
  • 3.jpg
    128.3KB
  • fizzyC_RGB_3.fig
    146.6KB
  • 2.jpg
    137.6KB
  • fuzzyC1.fig
    2MB
  • C_mean.asv
    6KB
  • 1.jpg
    183.9KB
  • fuzzyC2.fig
    1.9MB
  • C_mean.m
    9.2KB
内容介绍
%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 rel='nofollow' onclick='return false;'>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)
评论
    相关推荐