how to adjust box size of blob analysis
3 views (last 30 days)
Show older comments
i have a picture of digits. i have croped that digits using blobanalysis then i resize that croped image to 20x20 and after place that 20x20 image into 28x28 image so that i can pedict that digit. but there is a problem when there is 1 in image and the box form around 1 is very small so when i resize that croped 1 it becom disorted. how can i fix this ![problem.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195288/problem.png)
![problem.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195288/problem.png)
clc;
clear;
BackgroundImage = imread('back.png');
object = imread('11.png');
subplot(3,3,1);
imshow(object);
title('Orignal Image');
ga = rgb2gray(object);
BW = im2bw(ga);
subplot(3,3,2);
imshow(BW)
title('convert im2bw');
gb = rgb2gray(BackgroundImage);
foregroundDetector = vision.ForegroundDetector('InitialVariance',(30/255)^2);
foreground = step(foregroundDetector, gb);
foreground1 = step(foregroundDetector, ga);
BlobAnalysis = vision.BlobAnalysis('MinimumBlobArea',100,'MaximumBlobArea',50000);
[area,centroid,bbox] = step(BlobAnalysis,foreground1);
Ishape = insertShape(object,'rectangle',bbox,'Color', 'green','Linewidth',6);
subplot(3,3,3);
imshow(Ishape);
no_of_digits = size(bbox,1);
pred = zeros(1,no_of_digits);
for k = 1:no_of_digits
im_k = imcrop(ga,bbox(k,:));
im_k = imresize(im_k,[20,20]);
white = 255 * ones(28, 28, 'uint8');
white(5:24,5:24) = im_k;
final_image = white;
subplot(3,3,k+3)
imshow(final_image)
pred(k) = pred_number1(final_image);
imshow(final_image)
end
Thanks :)
0 Comments
Accepted Answer
Nick
on 17 Nov 2018
The shape of the bounding box will always give the smallest rectangular area in which the blob is contained, but you could change your code a bit to make it so that the blob is not distorted. What you do instead of resizing it to a specific size is adapting the scale in imresize. Followed by inserting that image in the white padded image. A rough implementation in your code by replacing a few lines would look like this:
for k = 1:no_of_digits
im_k = imcrop(ga,bbox(k,:));
% resize based on scale:
maxSize = max(size(im_k));
im_k = imresize(im_k,20/maxSize);
white = 255 * ones(28, 28, 'uint8');
% assign the image inside the white one:
% get the size of the resized image
sizeK = size(im_k);
% determine the indices to place it in the middle of the padded image
iStart = floor((28-sizeK(1))/2)+1;
iEnd = iStart+sizeK(1)-1;
jStart = floor((28-sizeK(2))/2)+1;
jEnd = jStart+sizeK(2)-1;
white(iStart:iEnd,jStart:jEnd) = im_k;
final_image = white;
subplot(3,3,k+3)
imshow(final_image)
pred(k) = pred_number1(final_image);
imshow(final_image)
end
0 Comments
More Answers (1)
Image Analyst
on 17 Nov 2018
After you crop it, use the padarray() function to pad all the way around with some margin.
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!