How to make all coins in 'coins.png' the same size?

15 views (last 30 days)
Hello everybody,
[Wanted]: I want to make all coins the same size in standard Matlab image 'coins.png', for example the same size as the smallest coin.
[What has been done so far]: Here is what I have done so far:
  1. I found the smallest coin area and index;
  2. Calculated the image background.
  3. Cropped all the coins;
close all; clc;clear all;
I = imread('coins.png');
bw = im2bw(I, graythresh(I));
bw = imfill(bw, 'holes');
L = bwlabel(bw);
s = regionprops(L, 'Centroid','Area','BoundingBox');
%%Smallest coin index
[~,min_coin_idx] = min([s.Area]);
% Background
[~,maxGrayLevel] = max(imhist(I));
[row,col]=ind2sub(size(I), min_coin_idx);
background_im = I.*0+maxGrayLevel;
%%Crop all coins
figure(1);
for k = 1 : size(s, 1)
thisBlobsBoundingBox = s(k).BoundingBox;
subImage = imcrop(I, thisBlobsBoundingBox);
subplot(3, 4, k);
imshow(subImage);
end
%%Current results
figure(2);
subplot(1,3,1);
imshow(I);title('Original');
subplot(1,3,2);
thisBlobsBoundingBox = s(min_coin_idx).BoundingBox;
subImage = imcrop(I, thisBlobsBoundingBox);
imshow(subImage); title('Smallest coin');
subplot(1,3,3);
imshow(background_im,[]); title('Background');
[Need help with]
  1. Somehow re-size all images to the size of the smallest coin (area or BoundingBox);
  2. Place all re-sized coins on top of created background, by using information of the Centroid value.
Thanks in advance for any help (@Image Analyst),
Ivan

Accepted Answer

Image Analyst
Image Analyst on 5 Nov 2015
Let's say you want all the coins to be 480 by 640, or whatever. Just call imresize in your loop:
subImage = imcrop(I, thisBlobsBoundingBox);
subImage = imresize(subImage, [480,640]);
If you want them pasted back on to your original image, that's a little trickier because you'll have to determine the starting and ending rows and columns for the new resized subimage so that you can paste it back in. I'm attaching two image copying and pasting demos that you can adapt.
  10 Comments
Ivan Shorokhov
Ivan Shorokhov on 8 Nov 2015
Edited: Ivan Shorokhov on 8 Nov 2015
@Image Analyst
Thank you, for all your help.
Please find attached file, may be you will use it for new demo ;-)
Image Analyst
Image Analyst on 8 Nov 2015
You're welcome. Good job - it works well. Thanks for Accepting the answer.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!