Find the boundaries of Signature

前端 未结 2 1852
無奈伤痛
無奈伤痛 2020-12-07 00:16

How do I find the boundaries of this inverted binary image?

相关标签:
2条回答
  • 2020-12-07 00:39

    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:

    enter image description here

    Have fun!

    0 讨论(0)
  • 2020-12-07 00:54

    Just in case Matlab isn't a firm requirement, or if you wish to check your Matlab results easily, or anyone else is looking who can't afford Matlab, there is the incredibly powerful ImageMagick that runs on all platforms, with command line and bindings for Perl, Python, .NET, Ruby and others.

    I increased the borders around your image to make a proper test, and ran the following command:

    convert out.png -format "%@" info:
    362x135+49+26
    

    which tells me that the bounding box you seek is 362 pixels wide and 135 pixels tall and its top-left corner is offset 49 pixels right and 26 pixels down from the top-left of the background - all in one simple command.

    Just for fun, I can then draw that box in in red with the following command:

    convert out.png -stroke red -strokewidth 1 -fill none -draw "rectangle 49,26 410,160" box.png
    

    so it looks like this:

    enter image description here

    0 讨论(0)
提交回复
热议问题