INPUT IMAGE:
MATLAB CODE (also attached):
clear all; close all; clc image=imread('a_ASL3.jpg'); BW=binary_image(image); BW = ~BW; st = regionprops(BW, 'BoundingBox' ); for k = 1 : length(st) thisBB = st(k).BoundingBox; rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],... 'EdgeColor','r','LineWidth',2 ) end
OUTPUT IMAGE:
PROBLEM I want bounding box just around hand so that I can crop it and do further operations but I am getting image as output in which there are bounding box around every object.

1 件のコメント

Poornima Gokhale
Poornima Gokhale 2015 年 12 月 14 日
I tried this code for my image.But its not working properly. How to modify?? And i want an enlarged image of individual letter to save that letter. Please help me

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2014 年 1 月 19 日

4 投票

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in a color demo image.
folder = 'C:\Users\Explorer\Documents';
baseFileName = 'a_ASL3.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage=imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
binaryImage = grayImage < 128;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Label the image
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',2 )
end
% Let's extract the second biggest blob - that will be the hand.
allAreas = [measurements.Area];
[sortedAreas, sortingIndexes] = sort(allAreas, 'descend');
handIndex = sortingIndexes(2); % The hand is the second biggest, face is biggest.
% Use ismember() to extact the hand from the labeled image.
handImage = ismember(labeledImage, handIndex)
% Now binarize
handImage = handImage > 0;
% Display the image.
subplot(2, 2, 3);
imshow(handImage, []);
title('Hand Image', 'FontSize', fontSize);

11 件のコメント

Explorer
Explorer 2014 年 1 月 20 日
Thank you. Can you please write few more lines for cropping hand?
Image Analyst
Image Analyst 2014 年 1 月 20 日
You shouldn't need to crop it, but if you want to, say to magnify it, you can simply call imcrop(). Here are the lines you can add:
% Crop out hand
croppedImage = imcrop(handImage, measurements(handIndex).BoundingBox);
% Display the image.
subplot(2, 2, 4);
imshow(croppedImage, []);
title('Cropped Hand Image', 'FontSize', fontSize);
So, if I've answered your original question, don't forget to mark it "Accepted." Thanks, and good luck with your project.
Explorer
Explorer 2014 年 1 月 20 日
編集済み: Explorer 2014 年 1 月 20 日
Thank you very much :)
Explorer
Explorer 2014 年 2 月 4 日
編集済み: Explorer 2014 年 2 月 4 日
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],... 'EdgeColor','r','LineWidth',2 )
How to know positions in numbers above lines?
These lines are from code mentioned above.
Image Analyst
Image Analyst 2014 年 2 月 4 日
You measured the bounding boxes with regionprops. The positions/numbers were measured by regionprops().
thisBB is the bounding box of just one of the boxes. If they are in a matrix (like in between brackets like I did) immediately after the 'Position' keyword, then it will consider those numbers as the position numbers.
Explorer
Explorer 2014 年 2 月 4 日
Okay thanks
Maaz Muslim
Maaz Muslim 2014 年 5 月 22 日
How to save bounding box image?
Akib Rahman
Akib Rahman 2018 年 6 月 21 日
@Image Analyst, I tried this code but, it doesn't work for my image! My image is given below
My original image is given below:
But I need a code, which will detect in that way!
Thanks.
Image Analyst
Image Analyst 2018 年 6 月 21 日
Exactly what part doesn't work? I see bounding boxes over the regions, so at least that part works. Does something after that not work, like the sort() function?
imrankhan ajees
imrankhan ajees 2018 年 9 月 29 日
not working for all inputs it will work for one input only
Image Analyst
Image Analyst 2018 年 9 月 29 日
From my code
width = thisBB(3)
height = thisBB(4)

サインインしてコメントする。

その他の回答 (1 件)

Ranjitha
Ranjitha 2015 年 9 月 7 日

0 投票

How to find the length and width for the given code.?? please do help with this.. Can you please let me know what all changes has to be made for the existing code to find length and width of the hand image.

カテゴリ

ヘルプ センター および File ExchangeDeep Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by