2

I have an image (sorry cannot link it for copyright purposes) that has a character outlined in a black line. The black line that outlines the character is the darkest thing on the picture (planned on using this fact to help find it). What I need to do is obtain four coordinates that draw a virtual box around the character. The box should be as small as possible while still keeping the outlined character inside its contents. I intend on using the box to help pinpoint what would be the central point of the character's figure by using the center point of the box.

I started with trying to identify parts of the outline. Since it's the darkest line on the image, I used getextrema() to obtain at least one point on the outline, but I can't figure out how to get more points and then combine those points to make a box.

Any insight into this problem is greatly appreciated. Cheers!

  • EDIT *

This is what I have now:

im = Image.open("pic.jpg")
im = im.convert("L")
lo, hi = im.getextrema()
im = im.point(lambda p: p == lo)
rect = im.getbbox()
x = 0.5 * (rect[0] + rect[2])
y = 0.5 * (rect[1] + rect[3])

It seems to be pretty consistent to getting inside the figure, but it's really not that close to the center. Any idea why?

Joshua Gilman
  • 1,174
  • 4
  • 16
  • 32

1 Answers1

1
  1. Find an appropriate threshold that separates the outline from the rest of the image, perhaps using the extrema you already have. If the contrast is big enough this shouldn't be too hard, just add some value to the minimum.
  2. Threshold the image with the value you found, see this question. You want the dark part to become white in the binary thresholded image, so use a smaller-than threshold (lambda p: p < T).
  3. Use thresholdedImage.getbbox() to get the bounding box of the outline
Community
  • 1
  • 1
Junuxx
  • 14,011
  • 5
  • 41
  • 71