1

I'm working on a project that requires me to extract a face:

enter image description here

And I've done so, with dlib. The next step is to extract the keypoints from that region and store them. The problem, is that I can't extract that region.

I'm using OpenCV's sub rectangle function:

for k, d in enumerate(dets):

    a = d.left()
    b = d.top()
    c = d.right()
    d = d.bottom()

    print ("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(k, a, b, c, d))
#   showImg(img1, "Image")

    temp1 = img1
    cv2.rectangle(temp1, (a, b), (c, d), (255, 0, 0), 2)
#   showImg(temp1, "Face")

    aa = a
    print ("Selection Size: " + str(aa))


#   x1 = (a, b)
#   x2 = (d, b)
#   y1 = (a, d)
#   y2 = (c, d)
#   r, z = (x1+x2/2, y1+y2/2)

    x = ((a+d)/2)
    y = ((b+c)/2)

    temp2 = cv2.getRectSubPix(img1, (aa,aa), (x,y))
    showImg(temp2, "Face Image")

And despite reading multiple geometry and calculus tutorials, I can't get the math right. It keeps coming out too small, or off center. I finally dicerned that it needed to take the whole image into account, not just the frame:

enter image description here

But that's not accureate either, the bottom corner is the image, not the frame center.

And to make it even harder, I have to use cv2.getRectSubPix. There's something wrong with my numpy and it won't process pictures correctly, and PIL/Pillow/PythonImageLybrary is giving me installation issures. It has to be all in OpenCV.

Can anyone help me with this issue?

Rich
  • 1,103
  • 1
  • 15
  • 36
  • Here is an answer for your question: http://stackoverflow.com/questions/15589517/how-to-crop-an-image-in-opencv-using-python – Evgeniy Jul 06 '16 at 06:01
  • That answer uses *numpy slicing*, which I can't use. – Rich Jul 06 '16 at 16:56
  • I already tried that, and it gave me an errror: `cv2.error: /home/dave/opencv-2.4.11/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow` – Rich Jul 06 '16 at 17:00
  • It *has* to be with `cv2.getRectSubPix`. – Rich Jul 06 '16 at 17:01

1 Answers1

1

If you can't use numpy slicing, you should fix your code:

here is your main error:

x = ((a+d)/2)
y = ((b+c)/2)

that is equal to:

x = ((left+bottom)/2)
y = ((top+right)/2)

one more error is re-declaring variable 'd' that what initially 'detection rect' and comes to 'bottom'

So the solution can look like:

c = d.center();
center = (c.x, c.y)
patch_size = (d.width(), d.height())
cv2.rectangle(temp1, patch_size, center, (255, 0, 0), 2)
Evgeniy
  • 2,481
  • 14
  • 24
  • Actually, the `d` is from dlib: `Traceback (most recent call last):, File "FaceToFace.py", line 43, in , c = d.center(); AttributeError: 'int' object has no attribute 'center'` – Rich Jul 07 '16 at 11:32
  • but you redefined d: d = d.bottom() – Evgeniy Jul 07 '16 at 11:33
  • It came like that: https://github.com/davisking/dlib/blob/master/python_examples/face_landmark_detection.py – Rich Jul 07 '16 at 11:56
  • Hey, this didn't solve my problem, but it did really help out & clarify a few things for me. I'm accepting it, and thanks. – Rich Aug 03 '16 at 05:09