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.


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


2. Load the image to image segmentation tool box


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


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


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
A=imread(name); % Read Image File to variable A ( Original Image)
title('Original Image')

2. Use of the created function.

[BW, maskedImage]= segmentImage(A); % We will use the Function we created using image segmentation
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.
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
mytitle=strcat('object number:',num2str(j)); % title for every figure
imwrite(target,sprintf('%d.jpg',j)) % Save cropped images along with proper numbers in same directory.

Output Plots

Did you find some helpful content from our video or article and now looking for its code, model, or application? You can purchase the specific Title, if available, and instantly get the download link.

Thank you for reading this blog. Do share this blog if you found it helpful. If you have any queries, post them in the comments or contact us by emailing your questions to [email protected]. Follow us on LinkedIn Facebook, and Subscribe to our YouTube Channel. If you find any bug or error on this or any other page on our website, please inform us & we will correct it.

If you are looking for free help, you can post your comment below & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we recommend you book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our training modules. 

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

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