Hello Mat

 找回密码
 立即注册
查看: 6190|回复: 2

二值化特征(面积、椭圆长短轴、周长)、连通域检测

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-7-15 20:01:12 | 显示全部楼层 |阅读模式
  1. clc,clear,close all
  2. warning off;
  3. % [filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.tif'},'选择图片');   %选择不同格式下的图片
  4. % str=[pathname,filename];   %图像名称
  5. % Image =imread(str);    %  加载该图像
  6. Image =imread('jinx.jpg');    %  加载该图像
  7. % Image =imread('1.jpg');    %  加载该图像

  8. % 获取灰度图像
  9. if size(Image,3)>1
  10.     ImageGray = rgb2gray(Image);   % 转化为灰度图像
  11. else
  12.     ImageGray = Image;
  13. end
  14. % 中值滤波
  15. ImageGray = medfilt2(ImageGray,[3,2]);
  16. % 二值化分割
  17. threshold = graythresh(ImageGray);
  18. bw = im2bw(ImageGray,threshold);
  19. imshow(bw)
  20. %% 形态学操作
  21. se = strel('rectangle',[2,2]);
  22. for i=1:2
  23.     bw = imerode(bw,se);
  24. end
  25. imshow(bw)
  26. for i=1:2
  27.     bw = imdilate(bw,se);
  28. end
  29. % 移除对象
  30. bw = bwareaopen(bw,100);  % 去掉面积小于100的小块
  31. se = strel('rectangle',[3,3]);
  32. bw = imclose(bw,se);     % 闭运算
  33. bw = imopen(bw,se);      % 开运算
  34. imshow(bw)

  35. % 连通域检测,计算特征
  36. cc = bwconncomp(~bw);                % 连通域操作
  37. stats = regionprops(~bw,'all');      % 提取二值化块的所有特征
  38. N = cc.NumObjects;          % 晶粒个数
  39. Area = sum( [stats.Area] ); % 面积
  40. Perimeter = sum( [stats.Perimeter] ); % 周长
  41. MajorAxisLength = mean( [stats.MajorAxisLength] ); % 长轴长
  42. MinorAxisLength = mean( [stats.MinorAxisLength] ); % 短轴长
  43. Area_ratio = Area/size(bw,1)/size(bw,2);   % 面积分数
  44. Area_mean = Area/N;             % 平均面积
  45. Perimeter_mean = Perimeter/N;   % 平均周长

  46. fid=fopen('result.txt','wt');   % 打开文件
  47. fprintf(fid,'%s','面积             周长            长轴长        短轴长      面积分数       平均面积        平均周长');
  48. fprintf(fid,'\n');
  49. fprintf(fid,'%3.4f       ',Area);  % 输入数据
  50. fprintf(fid,'%3.4f       ',Perimeter);  % 输入数据
  51. fprintf(fid,'%3.4f       ',MajorAxisLength);  % 输入数据
  52. fprintf(fid,'%3.4f       ',MinorAxisLength);  % 输入数据
  53. fprintf(fid,'%3.4f       ',Area_ratio);  % 输入数据
  54. fprintf(fid,'%3.4f       ',Area_mean);  % 输入数据
  55. fprintf(fid,'%3.4f       ',Perimeter_mean);  % 输入数据
  56. fprintf(fid,'\n');
  57. fclose(fid); %关闭文件

  58. % % 二值化区域边界分割
  59. % [B_regions, Label] = bwboundaries(bw,'noholes');
  60. % Label_rgb = label2rgb(Label,'jet',[.5 .5 .5]);
  61. % imshow(Label_rgb)
  62. %%
  63. % [L, rgb] = watershed_segmentation(bw);
  64. % Label_rgb = label2rgb(L,'jet',[.5 .5 .5]);
  65. % imshow(Label_rgb)

  66. %%
  67. % id = find( [stats.Solidity]<thre );  % 矩形度小于thre的,全部输出
  68. % for i=1:length(id)
  69. %     bw(labelmatrix(cc)==id(i))=0;   % 剔除矩形度小于thre的所有二值化块
  70. % end
复制代码
分水岭分割参考:分水岭分割算法


算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
 楼主| 发表于 2017-10-11 21:42:41 | 显示全部楼层
  1. %% CT切片
  2. clc,clear,close all
  3. file_path =  './试件/';% 图像文件夹路径
  4. img_path_list = dir(strcat(file_path,'*.jpg')); % 获取该文件夹中所有bmp格式的图像
  5. img_num = length(img_path_list);  % 获取图像总数量
  6. if img_num > 0                    % 有满足条件的图像
  7.     k=1;
  8.     for j = 1 : 2 : img_num            % 逐一读取图像
  9.         image_name = img_path_list(j).name;   % 图像名
  10.         fprintf('正在处理的图像: %d %s\n',j,strcat(file_path,image_name));  % 显示正在处理的图像名
  11.         img =  imread(strcat(file_path,image_name));
  12.         if size(img,3)>1
  13.             img = rgb2gray(img);
  14.         end
  15.         img = imresize(img,[512,512]);  % 缩小处理
  16.         bw=img>10;
  17.         % 连通域检测,计算特征
  18.         cc = bwconncomp(bw);         % 连通域操作
  19.         s  = regionprops(cc, {'centroid','area'});  % 面积特征
  20.         [A, id] = max([s.Area]);     % 面积最大
  21.         bw(labelmatrix(cc)~=id)=0;   % 只保留最大二值化块
  22.         bw = bwfill(bw,'holes');     % 填充孔洞
  23.         img1 = immultiply(img,bw);   % 交运算
  24.         D(:,:,k) = img1;
  25.         k=k+1;
  26.     end
  27. end
  28. %% CT重建
  29. [x,y,z,D] = reducevolume(D,[4,4,1]);
  30. D = smooth3(D);
  31. p1 = patch(isosurface(x,y,z,D, 5,'verbose'),...
  32.     'FaceColor','g','EdgeColor','none');
  33. isonormals(x,y,z,D,p1);
  34. p2 = patch(isocaps(x,y,z,D, 5),...
  35.     'FaceColor','interp','EdgeColor','none');
  36. view(3);
  37. axis tight;
  38. daspect([1,1,.4]);
  39. colormap(gray(100));
  40. camlight;
  41. lighting gouraud;
复制代码
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
 楼主| 发表于 2018-3-24 23:12:37 | 显示全部楼层
求解最大的二值化区域图,:
  1. %% 图像二值化处理
  2.     bw = imG<190;               % 图像二值化处理
  3.     figure(2),imshow(bw)
  4.     bw1 = bwareaopen(bw,20);    % 去除面积小于20的块
  5.     figure(2),imshow(bw1)
  6.     bw2 = imfill(bw1,'holes');  % 填充空洞
  7.     figure(2),imshow(bw2)
  8.     cc = bwconncomp(bw2);       % 连通域检查
  9.     stats = regionprops(bw2,'area');                 % 提取二值化块的所有特征
  10.     id = find([stats.Area] == max([stats.Area]));    % 求最大连通域的索引
  11.     bw2(labelmatrix(cc)~=id)=0;    % 提取二值图像中最大连通区域
  12.     figure(2),imshow(bw2)
  13.     bw3 = bw2.*bw1;                % 交运算
  14.     figure(2),imshow(bw3)
复制代码

算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-24 18:00 , Processed in 0.213304 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表