How to make all coins in 'coins.png' the same size?
11 views (last 30 days)
Show older comments
Ivan Shorokhov
on 5 Nov 2015
Commented: Image Analyst
on 8 Nov 2015
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:
- I found the smallest coin area and index;
- Calculated the image background.
- 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]
- Somehow re-size all images to the size of the smallest coin (area or BoundingBox);
- 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
0 Comments
Accepted Answer
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
Image Analyst
on 8 Nov 2015
You're welcome. Good job - it works well. Thanks for Accepting the answer.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!