飞码网-免费源码博客分享网站

点击这里给我发消息

matlab算法-硬币分割 - 图像分割的应用

飞码网-免费源码博客分享网站 爱上飞码网—https://www.codefrees.com— 飞码网-matlab-python-C++ 爱上飞码网—https://www.codefrees.com— 飞码网-免费源码博客分享网站
我们得到一张包含五枚硬币的图片。我们的目标是将这些硬币分别分割,并保存到不同的独立文件中。


Image-Segmentation-1

1. 要想开始,打开MATLAB软件,在命令窗口中输入'imageSegmenter',如下图所示。

Image-Segmentation-2

2. 将图像加载到图像分割工具boxox中。


Image-Segmentation-3

3. 点击图形切割,打开选择工具。


Image-Segmentation-4

4. 使用图形切割工具箱标记前景和背景图像,完成后是这样的。

Image-Segmentation-5Image-Segmentation-5

5. 完成后导出并生成函数到.m文件。以相同的名称'segmentImage'保存该函数。

 
function [BW,maskedImage] = segmentImage(RGB)
%segmentImage Segment image using auto-generated code from imageSegmenter App
% [BW,MASKEDIMAGE] = segmentImage(RGB) segments image RGB using
% auto-generated code from the imageSegmenter App. The final segmentation
% is returned in BW, and a masked image is returned in MASKEDIMAGE.
 
% Auto-generated by imageSegmenter app on 21-Jul-2018
%----------------------------------------------------
 
% Convert RGB image into L*a*b* color space.
X = rgb2lab(RGB);
 
% Graph Cut
foregroundInd = [2934150 3025838 4323953 4389272 4428452 4441522 4441527 4454632 4478152 4491256 4504326 4504361 4517451 4517476 4517500 4517540 4517555 4517575 4517590 4517595 4543730 5802341 5828471 5838971 5838986 5852060 5852070 5852125 5852130 5852150 5852170 5852180 5852185 5852195 7722421 7722426 7735456 7800269 8616438 8629458 8629468 8655519 8655539 8681604 8707674 8733615 8733749 ];
backgroundInd = [869446 947905 ];
L = superpixels(X,30326,'IsInputLab',true);
 
% Convert L*a*b* range to [0 1]
scaledX = X;
scaledX(:,:,1) = X(:,:,1) / 100;
scaledX(:,:,2:3) = (X(:,:,2:3) + 100) / 200;
BW = lazysnapping(scaledX,L,foregroundInd,backgroundInd);
 
% Create masked image.
maskedImage = RGB;
maskedImage(repmat(~BW,[1 1 3])) = 0;

现在要使用该函数,我们在MATLAB脚本中再创建一个.m文件。编写代码的第一步是。

1. 装入图片


close all
clear all
[file,path]=uigetfile('*.jpg');
name=[path,file];
A=imread(name); % Read Image File to variable A ( Original Image)
figure(1),imshow(A)
title('Original Image')

2. 使用创建的功能。


[BW, maskedImage]= segmentImage(A); % We will use the Function we craeted using image segmentation
figure(2),imshow(maskedImage)
title('Masked Image with Black Background')

3. 给对象贴上标签,并将图像裁剪成不同的对象。


label=bwlabel(BW); % Finds out the independent objects and labels them.
max(max(label)); % This Command gives us the maximum objects detected.
 
for j=1:max(max(label)) % j will iterate from 1 to maximum objects.
[row,col]=find(label==j); % to get number of rows and columns of each and every object(coins)
len=max(row)-min(row)+2; % length of the of the cropped image is set
breadth=max(col)-min(col)+2; % breadth of the cropped image is set
target=uint8(zeros([len breadth 3] ));% intermediate target matrix and is assigned to zeros.
sy=min(col)-1;
sx=min(row)-1;
 
for i=1:size(row,1) % Iteration to retrieve the original image segments into the black target
x=row(i,1)-sx; % x values of the actual image
y=col(i,1)-sy; % y values of the actual image
target(x,y,:)=A(row(i,1),col(i,1),:); % the actual coin image on to the black target
end
mytitle=strcat('object number:',num2str(j)); % title for every figure
figure,imshow(target);
title(mytitle);
imwrite(target,sprintf('%d.jpg',j)) % Save cropped images along with proper numbers in same directory.
end
Image-Segmentation-6
​​​​​​​Image-Segmentation-7
Image-Segmentation-11
飞码网-免费源码博客分享网站 爱上飞码网—https://www.codefrees.com— 飞码网-matlab-python-C++ 爱上飞码网—https://www.codefrees.com— 飞码网-免费源码博客分享网站
赞 ()
内容页底部广告位3
留言与评论(共有 0 条评论)
   
验证码: