qurit / qurit/rt-utils

Error when segmentation consists of only one pixel

Open
#131 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
256
Forks
75
PR merge metrics
No merged PRs in 30d

Description

Hi together,

I've encontered an openCV error in one case where only one voxel was annotated in a slice (full error log below).

I found the root of the error in the file image_helper.py in the function get_slice_mask_from_slice_contour_data. When there is only one annotated pixel, the line 275

polygon = np.array(polygon).squeeze()

reduces the polygon array to an array of size 2, but the cv.fillPoly needs the array to be of size n x 2 where n is the number of countour points in this slice (in this case n=1). I fixed this by adding

if polygon.ndim == 1:
        polygon = polygon[None]

just in the line below and now it works for me. Would you mind adding this to your repo?

Many thanks!

here is the full error log:

File ~/miniconda3/envs/torch_env/lib/python3.12/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
exec(code, globals, locals)

File /import/H-HH-Laufwerk/thomas/lungenprojekte/Python/visual_eval/NILS/try_read_rtstruct.py:21
ccs += (i+1) * rtstruct.get_roi_mask_by_name(roi_name)

File ~/miniconda3/envs/torch_env/lib/python3.12/site-packages/rt_utils/rtstruct.py:113 in get_roi_mask_by_name
return image_helper.create_series_mask_from_contour_sequence(

File ~/miniconda3/envs/torch_env/lib/python3.12/site-packages/rt_utils/image_helper.py:247 in create_series_mask_from_contour_sequence
mask[:, :, i] = get_slice_mask_from_slice_contour_data(

File ~/miniconda3/envs/torch_env/lib/python3.12/site-packages/rt_utils/image_helper.py:280 in get_slice_mask_from_slice_contour_data
cv.fillPoly(img=slice_mask, pts = polygons, color = 1)

error: OpenCV(4.9.0) /io/opencv/modules/imgproc/src/drawing.cpp:2432: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'fillPoly'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in image_helper.py at get_slice_mask_from_slice_contour_data, especially the polygon normalization around line 275 and the subsequent cv.fillPoly call. Reproduce the single-pixel contour case from the issue and verify that the function completes without the OpenCV assertion error.

Written by the indexing model from the issue text.

Assessment

Tech stack
opencv, python
Domain
computer-vision
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.