How to make all coins in 'coins.png' the same size?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ivan Shorokhov
le 5 Nov 2015
Commenté : Image Analyst
le 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 commentaires
Réponse acceptée
Image Analyst
le 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 commentaires
Image Analyst
le 8 Nov 2015
You're welcome. Good job - it works well. Thanks for Accepting the answer.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with Image Processing Toolbox dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!