Advertisement

图像入门:MATLAB图像识别

阅读量:

该练习旨在识别图像中的图形形状、颜色、位置、面积和周长。处理步骤包括裁剪、增强对比度、二值化、边缘检测、质心标记、Hough变换检测直线、圆的周长和面积计算等。运行结果图显示椭圆检测尚未完成。

初学数字图像处理,做一个练习,识别图中的图形形状,颜色,位置,面积,周长

基本思路:首先先对图像进行裁剪,增强等处理,使图片成为简单的二值图。

接着提取图像边缘

对图像处理后的结果,即可完成质心计算并进行标注。通过HOUGH变换进行直线检测,根据圆的周长与面积的比例判断形状是否为圆形,并计算图形的周长、面积,以及RGB值等相关操作。

下面附全部代码,欢迎大家一起学习讨论~

复制代码
    f=imread('C:\Users\QHJ\Desktop\demo.png');
    f=f(:,:,3);
    f=histeq(f,256);       %增强对比度
    f=im2bw(f,0.386);
    rowhigh=102+276-1;     %提取有用部分
    colhigh=193+277-1;
    f=f(102:rowhigh,193:colhigh);
    se=strel('square',14); %开闭运算
    fo=imopen(f,se);
    f2=imclose(fo,se);
    f2=~f2;
    imshow(f2);
    
    g=edge(f2,'sobel',0.47);
    [B,L] = bwboundaries(f2,'noholes');
    hold on
    for k = 1:length(B)    % 标记边界
      boundary = B{k};
      plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 2)
    end
    
    hold on              
    [L1,n]=bwlabel(g);     %标定质心
    ff=imread('C:\Users\QHJ\Desktop\demo.png');
    for k=1:n
      [r,c]=find(L1==k);
      rbar=mean(r);
      cbar=mean(c);
      plot(cbar,rbar,'Marker','*','MarkerEdgeColor','blue');
      fprintf('行坐标为%9.2f    , ',cbar)
      fprintf('列坐标为%9.2f\n',rbar);
    rgb=ff(floor(cbar)+193,floor(rbar)+102,:)          %读取RGB值
    
    end
    
    axis on,axis normal;
    [H,theta,rho]=hough(g);%判断直线
    peak=houghpeaks(H,11);
    lines=houghlines(g,theta,rho,peak,'FillGap',10,'MinLength',31);%
    hold on
    for k=1:length(lines)
      xy=[lines(k).point1;lines(k).point2];
      plot(xy(:,1),xy(:,2),'LineWidth',2.5,'Color','blue');
    end
    
    l=regionprops(L,'Perimeter','Area');  %求周长,面积
    l.Perimeter   
    l.Area
    
    stats = regionprops(L,'Area','Centroid','Perimeter');
    threshold = 0.95;
    for k = 1:length(B)
      boundary = B{k};
      area = stats(k).Area;                
      %perimeter=stats(k).Perimeter;
      delta_sq = diff(boundary).^2;    
      perimeter = sum(sqrt(sum(delta_sq,2)));                 %利用4pi*面积/周长^2=1判断圆
      metric = 4*pi*area/perimeter^2;
      metric_string = sprintf('%2.2f',metric) ;
      text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','g',...
       'FontSize',14,'FontWeight','bold');
      if metric > threshold
     centroid = stats(k).Centroid;
     text(centroid(1)-10,centroid(2)-10,'圆','Color','b','FontSize',10,'FontWeight','bold');
      end
    
    end

运行结果图,判断椭圆还没做出来。。。。。

全部评论 (0)

还没有任何评论哟~