Because there are gaps in the signature, using standard bounding box algorithms will fail to fully encapsulate the entire signature because when you detect bounding boxes, the gaps in the strokes will be interpreted as individual regions, and so individual bounding boxes will be detected there. One work around is to simply find all of those pixels that are non-zero, and find the minimum and maximum row and column locations. You can use find
to help you do that. These minimum and maximum values will correspond to the top left and bottom right corners of the overall bounding box that encapsulates this signature.
Before I show any code, I'm directly reading your image that you have uploaded, but it is a RGB image. As such, I'm going to convert this to grayscale with rgb2gray
, then threshold the image with im2bw
. There's also an unnecessary white border around the signature image so I'm going to clear this up with imclearborder
.
Without further ado, here's the code:
%// Read in image and convert to binary
%// Also clear the borders
im = imread('http://oi59.tinypic.com/5fk9y0.jpg');
im_bw = imclearborder(im2bw(rgb2gray(im)));
%// Find those non-zero pixel locations
[rows, cols] = find(im_bw);
min_row = min(rows);
max_row = max(rows);
min_col = min(cols);
max_col = max(cols);
%// Now extract the bounding box
bb = im_bw(min_row:max_row, min_col:max_col);
%// Show the image
imshow(bb);
When you do this, bb
should contain the image where the signature is bounded so that it fits within the image exactly. This is what I get when you display bb
:
Have fun!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…