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

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

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

@Image Analyst Thank you very much,
Now I have another question, how to make background of cropped coins transparent?
And just to let you the know 'copy_paste_image_freehand.m' returns an error:
Error using imread (line 349)
File "C:\Program Files\MATLAB\R2015b\toolbox\images\imdemos\AT3_1m4_09.tif" does not exist.
Error in copy_paste_image_freehand (line 64)
targetImage = imread(fullFileName);
Thanks for informing me. Recent versions of MATLAB moved the location of the demo images. I fixed it by putting in a method to find the demo images folder regardless of where it lives. See attached.
What do you mean by transparent? What's behind it that you want to see? Or do you want to make a PNG or GIF to be used in some program other than MATLAB?
@Image Analyst
I'll show in the following example:
How to remove the black color around coins, so coins would fit naturally.
Code attached.
You're going to have to make a mask and then use it with the new image and the old image. You already have the binary image, so just use that as a mask.
cityImage(binaryImage) = coinsImage(binaryImage);
This takes pixels from the coins image where the mask is true/1/white, and replaces those same pixel locations in the city image with those from the coins image.
@Image Analyst
Thank you for all your help!
If you've made a new image with resized coins pasted onto it, you're going to have to create a new binary image from that new image rather than use the binary image from the original coin image.
@Image Analyst
Could you also recommend me please, how I can equalize/normalize intensity of all the coins, so all coins would have similar intensity?
Use regionprops() to get the mean intensities. Then get the average of those and divide by it and multiply by the desired intensity. Something like (untested):
measurements = regionprops(labeledImage, grayImage, 'MeanIntensity');
averageMeanIntensity = mean([measurements.MeanIntensity]);
Then for each sub-image
subImage = double(subImage) * desiredIntensity/averageMeanIntensity;
@Image Analyst
Thank you, for all your help.
Please find attached file, may be you will use it for new demo ;-)
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!