0

I'm getting this error when I tried to use OpenCV for a project.

  File "/Users/alex/opt/anaconda3/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/Users/alex/opt/anaconda3/lib/python3.8/multiprocessing/pool.py", line 51, in starmapstar
    return list(itertools.starmap(args[0], args[1]))
  File "/Users/alex/Desktop/CLAM/CLAM/wsi_core/WholeSlideImage.py", line 481, in process_coord_candidate
    if WholeSlideImage.isInContours(cont_check_fn, coord, contour_holes, ref_patch_size):
  File "/Users/alex/Desktop/CLAM/CLAM/wsi_core/WholeSlideImage.py", line 345, in isInContours
    if cont_check_fn(pt):
  File "/Users/alex/Desktop/CLAM/CLAM/wsi_core/util_classes.py", line 86, in __call__
    if cv2.pointPolygonTest(self.cont, points, False) >= 0:
cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'pointPolygonTest'
> Overload resolution failed:
>  - Can't parse 'pt'. Sequence item with index 0 has a wrong type
>  - Can't parse 'pt'. Sequence item with index 0 has a wrong type

What does this error mean? I'm quite new to python so please educate me. Thank you!

Here's the project link

https://github.com/mahmoodlab/CLAM#wsi-segmentation-and-patching

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Vortca 2
  • 31
  • 1
  • 2
  • 6
  • You should add more details (and directly on the question). Your problem is in `pointPolygonTest`, so you should write the error stack with also such line. or it is just your data which has not just numbers but also the string 'pt'. – Giacomo Catenazzi Jun 04 '21 at 12:57
  • @Giacomo Catenazzi Thank you for the reminder, just edited the error stack. – Vortca 2 Jun 04 '21 at 13:03
  • 2
    Possibly pointPolygonTest is looking for *integers*, and perhaps you are passing in float values? (similar to https://stackoverflow.com/questions/67851320/cant-parse-center-sequence-item-with-index-0-has-a-wrong-type ). – David Cary Oct 07 '21 at 00:04

3 Answers3

2

I face the same problem and solve it by downgrading opencv version to 4.5.1.48

Reference from this website: https://pythonrepo.com/repo/opencv-opencv-python-computer-vision

kiwiiiii
  • 41
  • 1
  • 4
2

pt parameter may be a tuple, I solve this problem via the following snippet, hope this can help you.

# if pt is list type point (x, y)
pt = tuple([int(round(pt[0]) ), int(round( pt[1] )) ])
if cv2.pointPolygonTest(contour, pt, False) == 0:
    print(pt)

my opencv-python version is : opencv-python==4.5.3.56

J.Zhao
  • 2,250
  • 1
  • 14
  • 12
0

I face the same problem too and I needed to use opencv-version to 4.5.3.

Here the problem comes from.

iterable = [(coord, contour_holes, ref_patch_size[0], cont_check_fn) for coord in coord_candidates]

The first index of "iterable" has a numpy array to hold the contour coordinates. However, these coordinates are keep as numpy dtype with numpy.uint32. If we are using new versions of opencv-python we have to convert it to int as python data type.

I fixed this issue simply like here,

iterable_new = []

for i in iterable:

    iterable_line = [np.uint32(i[0][0]).item(), np.uint32(i[0][1]).item()], i[1], i[2], i[3]
    iterable_new.append(iterable_line)

Update the "iterable" below with the new one.

results = pool.starmap(WholeSlideImage.process_coord_candidate, iterable)
oreitor
  • 171
  • 1
  • 4