.st0{fill:#FFFFFF;}

Image Processing with Coin Segmentation 

 January 4, 2019

By  Aditya Chaudhari

We are given an image containing five ₹10 coins. Our Objective is to segment these coins separately and save them into different and individual files.

Image-Segmentation-1

1. To get started open MATLAB Software and in the command window type ‘imageSegmenter’ as shown in the figure below:

Image-Segmentation-2

2. Load the image to image segmentation tool box

Image-Segmentation-3

3. Click on Graph Cut to open the selection tools.

Image-Segmentation-4

4. Mark the foreground and background image using the graph cut tool box, after its done it looks like this.

Image-Segmentation-5

5. After its done Export and generate the function to .m file. Save the function by the same name ‘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.
 %----------------------------------------------------
 
% 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;

Now to use the function we create another .m file in MATLAB Script. First step in writing the code is:
1. Load the images

close all
clear
[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. Use of the created function.

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

3. Labeling the objects and cropping the images as different objects.

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

Output Plots
Image-Segmentation-6
Image-Segmentation-7
Image-Segmentation-9
Image-Segmentation-10
Image-Segmentation-12
Image-Segmentation-8
Image-Segmentation-11

Get instant access to the code, model, or application of the video or article you found helpful! Simply purchase the specific title, if available, and receive the download link right away! #MATLABHelper #CodeMadeEasy

Ready to take your MATLAB skills to the next level? Look no further! At MATLAB Helper, we've got you covered. From free community support to expert help and training, we've got all the resources you need to become a pro in no time. If you have any questions or queries, don't hesitate to reach out to us. Simply post a comment below or send us an email at [email protected].

And don't forget to connect with us on LinkedIn, Facebook, and Subscribe to our YouTube Channel! We're always sharing helpful tips and updates, so you can stay up-to-date on everything related to MATLAB. Plus, if you spot any bugs or errors on our website, just let us know and we'll make sure to fix it ASAP.

Ready to get started? Book your expert help with Research Assistance plan today and get personalized assistance tailored to your needs. Or, if you're looking for more comprehensive training, join one of our training modules and get hands-on experience with the latest techniques and technologies. The choice is yours – start learning and growing with MATLAB Helper today!

Education is our future. MATLAB is our feature. Happy MATLABing!

About the author 

Aditya Chaudhari

  • Socheata Hour says:

    I want to call function [BW_out,properties] = filterRegions(BW_in) a function from apps in Matlab to my own code, how can I write it?

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    MATLAB Helper ®

    Follow: YouTube Channel, LinkedIn Company, Facebook Page, Instagram Page

    Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

    Use Website Chat or WhatsApp at +91-8104622179

    >