How do I find the bounding box of a fingerprint image
1 次查看(过去 30 天)
显示 更早的评论
The patterns on the image are black while the background is white. I need to make the background which is white as minimal as possible. From research I think using regionprops will work but I don't understand regionprops. Thank you.
0 个评论
回答(2 个)
Image Analyst
2013-4-1
Alternatively you could just sum the binary image horizontally and vertically, or use any(), and look for where it's first land last non-zero. Here's the demo for that method:
% Demo to find the bounding box of coins with any() and crop out the subimage.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% Get the full filename, with path prepended.
baseFileName = 'eight.tif';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', 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);
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
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')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 3, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold the image to get a binary image.
binaryImage = grayImage < 175;
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Get vertical profile.
verticalProfile = any(binaryImage, 2);
subplot(2, 3, 4);
plot(verticalProfile, 'b-', 'LineWidth', 4);
grid on;
title('Vertical Profile', 'FontSize', fontSize);
xlabel('Row', 'FontSize', fontSize);
% Get Horizontal profile.
horizontalProfile = any(binaryImage, 1);
subplot(2, 3, 5);
plot(horizontalProfile, 'b-', 'LineWidth', 4);
grid on;
title('Horizontal Profile', 'FontSize', fontSize);
xlabel('Column', 'FontSize', fontSize);
% Find what index the bounding box is at
topRow = find(verticalProfile, 1, 'first')
bottomRow = find(verticalProfile, 1, 'last')
leftCol = find(horizontalProfile, 1, 'first')
rightCol = find(horizontalProfile, 1, 'last')
% Plot box around original
xBox = [leftCol, rightCol, rightCol, leftCol, leftCol]
yBox = [topRow, topRow, bottomRow, bottomRow, topRow]
subplot(2, 3, 1);
hold on;
plot(xBox, yBox, 'r', 'LineWidth', 2);
% Crop it out of the original:
croppedImage = grayImage(topRow:bottomRow, leftCol:rightCol);
% Display the image.
subplot(2, 3, 6);
imshow(croppedImage, []);
title('Cropped Image', 'FontSize', fontSize);
2 个评论
Image Analyst
2013-4-1
I just copied and pasted and verified that it DOES indeed crop. You'll notice the bottom right image has edges just touching the edges of the coins while the original one has a lot of gray background surrounding the coins.
Image Analyst
2013-4-1
Here's the way to do it with bwconvhull() and regionprops():
% Demo to find the bounding box of coins with bwconvhull()
% and regionprops(), and crop out the subimage.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
clearvars;
format longg;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% Get the full filename, with path prepended.
baseFileName = 'eight.tif';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', 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);
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
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')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 3, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold the image to get a binary image.
binaryImage = grayImage < 125;
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Get the convex hull
binaryImage = bwconvhull(binaryImage, 'union');
subplot(2, 3, 4);
imshow(binaryImage, []);
title('Convex Hull Image', 'FontSize', fontSize);
% Call regionprops, asking for BoundingBox.
measurements = regionprops(binaryImage, 'BoundingBox');
% Find what index the bounding box is at
leftCol = measurements(1).BoundingBox(1)+0.5
topRow = measurements(1).BoundingBox(2)+0.5;
width = measurements(1).BoundingBox(3)
height = measurements(1).BoundingBox(4)
rightCol = leftCol + width;
bottomRow = topRow + height;
% Plot box around original
xBox = [leftCol, rightCol, rightCol, leftCol, leftCol]
yBox = [topRow, topRow, bottomRow, bottomRow, topRow]
subplot(2, 3, 1);
hold on;
plot(xBox, yBox, 'r', 'LineWidth', 2);
% Crop it out of the original:
croppedImage = grayImage(topRow:bottomRow, leftCol:rightCol);
% Display the image.
subplot(2, 3, 6);
imshow(croppedImage, []);
title('Cropped Image', 'FontSize', fontSize);
4 个评论
Image Analyst
2013-4-7
Sorry, but my answer stands. I just copied and pasted my code from above and ran it and it ran fine. I hope someone else can do that to verify what I'm saying. You didn't say what version of MATLAB you have. Please say
which -all bwconvhull
and tell me what it says.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!