필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
Ivan Shorokhov
Ivan Shorokhov 2015년 11월 5일
댓글: Image Analyst 2015년 11월 8일
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

채택된 답변

Image Analyst
Image Analyst 2015년 11월 5일
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
Ivan Shorokhov
Ivan Shorokhov 2015년 11월 8일
편집: Ivan Shorokhov 2015년 11월 8일
@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 2015년 11월 8일
You're welcome. Good job - it works well. Thanks for Accepting the answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by