2

I am trying to get the pixel coordinate values for the point detected using the open pose. Can someone tell me is this the correct way to identify the pixel coordinates or is there any other particular way to get the pixel coordinates represented as 2 and 5 in the below image?

enter image description here

code:

for pair in POSE_PAIRS:
    partA = pair[0]
    partB = pair[1]
    print("{}".format(partA),"{}".format(partB))

    if partA == 2 and partB == 5:
        print("heere")
        cv2.line(frame, points[partA], points[partB], (0, 0, 0), 2)
        cv2.circle(frame, points[partA], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
    else :
        cv2.line(frame, points[partA], points[partB], (0, 255, 255), 2)
        cv2.circle(frame, points[partA], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)

rc = cv2.minAreaRect(partA)
box = cv2.boxPoints(rc)
for p in box:
    pt = (p[0],p[1])
    print (pt)

error :

Traceback (most recent call last): File "OpenPoseImage.py", line 92, in rc = cv2.minAreaRect(partA) cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\convhull.cpp:137: error: (-215:Assertion failed) total >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::convexHull'

Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43
BatmanForLife
  • 99
  • 2
  • 3
  • 12

1 Answers1

1

If you just want to get the pixel coordinate values for the point detected using the open pose, i.e., white spot in the image, then you can use the below code:

import cv2
import numpy as np 

# read and scale down image
img = cv2.pyrDown(cv2.imread('hull.jpg', cv2.IMREAD_UNCHANGED))

# threshold image
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 230, 255, cv2.THRESH_BINARY)

# find contours
contours = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

for c in contours:
    # get the bounding rect
    x, y, w, h = cv2.boundingRect(c)

    # get the min enclosing circle
    (x, y), radius = cv2.minEnclosingCircle(c)

    # convert all values to int
    center = (int(x), int(y))
    radius = int(radius)

    if radius>2 and radius<4:
        print(center)
        img = cv2.circle(img, center, radius, (255, 0, 0), 2)
        cv2.putText(img,'({},{})'.format(int(x), int(y)), (int(x)+5, int(y)+5), cv2.FONT_HERSHEY_SIMPLEX, 0.3,(0,255,0), 1)

cv2.imshow('contours', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

output:

(208, 418)
(180, 410)
(160, 408)
(208, 326)
(152, 316)
(159, 234)
(200, 234)
(136, 224)
(224, 224)
(232, 163)
(184, 163)
(128, 163)
(200, 112)
(232, 91)
(136, 91)
(176, 61)
(176, 0)

enter image description here

In the above code, only those pixels are detected whose enclosing circle radius is greater than 2 and less than 4.

Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43
  • Thank you for this code! I tried using this, But it's throwing up an error:error: OpenCV(3.4.2) C:\Miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\imgproc\src\shapedescr.cpp:158: error: (-215:Assertion failed) count >= 0 && (depth == 5 || depth == 4) in function 'cv::minEnclosingCircle' Like this. How am i suppose to resolve this? – BatmanForLife Jul 14 '19 at 15:57
  • what's the error? As you can see, I already tried and it's giving result. – Anubhav Singh Jul 14 '19 at 15:57
  • I think you are getting this error because of no contours detected. Could you please check output of `print(len(contours))` before `for c in contours:` ? – Anubhav Singh Jul 14 '19 at 16:00
  • I'm getting this error only when I use this code using the jupyter notebook, and then if I run this code using cmd prompt, I'm getting this expected output. May I know the reason behind this? – BatmanForLife Jul 14 '19 at 16:38
  • I tried running above code my `jupyter notebook`, it's working perfectly. – Anubhav Singh Jul 14 '19 at 16:44
  • I have to see how you are running above code in notebook. Wanna share link to colab ? Just wanna see the way you are trying to run. Don't worry about libraries. – Anubhav Singh Jul 14 '19 at 16:46
  • Link : https://colab.research.google.com/drive/1ddqkDsRKRw3nqr9qZbHeCnpdKouhHE_L – BatmanForLife Jul 14 '19 at 17:39
  • @BatmanForLife, please check python version in jupyter notebook. – Anubhav Singh Jul 14 '19 at 18:11
  • And, also check opencv version by running `import cv2 ; cv2.__version__` – Anubhav Singh Jul 14 '19 at 18:17
  • If opencv version is 3.4.x then change `contours = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]` to `contours = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]` – Anubhav Singh Jul 14 '19 at 18:19
  • @BatmanForLife, it worked. Reason you are getting above error error is because of `contours = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]` line which works for opencv 4.0+ version. Change it to `contours = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]` – Anubhav Singh Jul 14 '19 at 18:39
  • Check this https://colab.research.google.com/drive/1ddqkDsRKRw3nqr9qZbHeCnpdKouhHE_L – Anubhav Singh Jul 14 '19 at 18:43
  • Yes! Thank you so much it worked perfectly! Is there any other way for me to contact you if any other doubts come in my research? Please let me know! Thank you so much – BatmanForLife Jul 15 '19 at 13:36