OpenCV polyLines() throws error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'polylines'

huangapple go评论64阅读模式
英文:

OpenCV polyLines() throws error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'polylines'

问题

我正在尝试从YOLO分割掩码数据集中绘制分割掩码。我正在阅读的注释行如下:

36 0.6158357764423077 0.814453125 0.6158357764423077 0.8095703125 0.6070381225961539 0.8095703125 0.6041055721153846 0.8115234375 0.5894428149038462 0.8154296875 0.5747800576923077 0.8125 0.5513196490384615 0.8134765625 0.5483870961538462 0.81640625 0.5923753653846154 0.818359375 0.6158357764423077 0.814453125

我正在使用cv2.polylines来绘制形状,但出现错误:

image_height, image_width, c = img.shape
isClosed = True
color = (255, 0, 0)
thickness = 2

with open(annotation_file) as f:
    for line in f:
        split_line = line.split()
        class_id = split_line[0]
        mask_shape = [float(numeric_string) for numeric_string in split_line[1:len(split_line)]]
        mask_points = []
        for i in range(0, len(mask_shape), 2):
            x, y = mask_shape[i:i+2]
            mask_points.append((x * image_width, y * image_height))
        points = np.array([mask_points])
        image = cv2.polylines(img, points,
                              isClosed, color, thickness)
        break

错误:

OpenCV(4.7.0) /Users/xperience/GHA-OCV-Python/_work/opencv-python/opencv-python/opencv/modules/imgproc/src/drawing.cpp:2434: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'polylines'

英文:

I am trying to draw a segmentation mask from a YOLO segmentation mask dataset. The annotation line I am reading looks like this:

36 0.6158357764423077 0.814453125 0.6158357764423077 0.8095703125 0.6070381225961539 0.8095703125 0.6041055721153846 0.8115234375 0.5894428149038462 0.8154296875 0.5747800576923077 0.8125 0.5513196490384615 0.8134765625 0.5483870961538462 0.81640625 0.5923753653846154 0.818359375 0.6158357764423077 0.814453125

I am using cv2.polylines to draw the shape but am getting an error:

image_height, image_width, c = img.shape
isClosed = True
color = (255, 0, 0)
thickness = 2

with open(annotation_file) as f:
    for line in f:
        split_line = line.split()
        class_id = split_line[0]
        mask_shape = [float(numeric_string) for numeric_string in split_line[1:len(split_line)]]
        mask_points = []
        for i in range(0,len(mask_shape),2):
            x,y = mask_shape[i:i+2]
            mask_points.append((x * image_width, y * image_height))
        points = np.array([mask_points])
        image = cv2.polylines(img, points,
                              isClosed, color, thickness)
        break

Error:

OpenCV(4.7.0) /Users/xperience/GHA-OCV-Python/_work/opencv-python/opencv-python/opencv/modules/imgproc/src/drawing.cpp:2434: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'polylines'

答案1

得分: 2

这是关于 .dtype 的问题。points.dtype 必须是 np.int32polyLines 需要整数,而不是浮点数。

你插入列表的值都是浮点数。数组的 dtype 会是 float64

只需调用此代码:

# 用于一个折线的点数组
points = np.array(mask_points, dtype=np.int32)

# polyLines 需要一个折线列表
# 你有一个折线,所以它将是一个包含一个元素的列表
image = cv2.polylines(
    img,
    [points], # 一个折线
    isClosed, color, thickness)

将外层维度放入 numpy 数组本身(你用 [mask_points] 实现了这一点),而不是使用外部列表,只有当所有折线具有相同数量的点时,才是可接受的。

英文:

It's an issue of the .dtype. points.dtype must be np.int32. polyLines wants integers, not floats.

The values you insert into your list are all floats. The array would have had float64 dtype.

Just call this:

# points array for one polyline
points = np.array(mask_points, dtype=np.int32)

# polyLines takes a list of polylines
# you have one polyline, so it'll be a list of one element
image = cv2.polylines(
    img,
    [points], # one polyline
    isClosed, color, thickness)

Putting the outer dimension into the numpy array itself (you did that with [mask_points]), instead of using an outer list, is only acceptable if ALL your polylines have the same number of points.

huangapple
  • 本文由 发表于 2023年5月29日 23:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定