Ask Your Question
0

Turn HFS result into Contours

asked 2020-03-29 10:46:11 -0600

mrersatz gravatar image

I suspect I'm missing something obvious but I can't sort out the best way to turn my segmented results into contours. I am using the cv2.hfs module and the preview looks great for my purposes. But I need to get a set of contours with centers, orientation etc. But the output of performSegmentCpu gives me a posterized or false-color image.

What's the best way to turn this into a set of contours?

Do I need to iterate through the whole output checking for unique pixel values and then do some kind of threshold for those pixels so I can run it through findContours?

The docs mention that the default result of hfs.performSegmentCpu is a "matrix of index" but I can't imagine what that is or how I would access it.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-03-29 11:28:25 -0600

berak gravatar image

updated 2020-03-30 03:46:09 -0600

the "posterized" image output is only a visualization for humans, for a program rather use the index version.

im = cv2.imread("some.png")
h  = cv2.hfs.HfsSegment_create(im.shape[0],im.shape[1])
res = h.performSegmentCpu(im, False) # False -> indexed (16U)

each "blob" is numbered, and has its own index, you can make a binary mask from it in python like:

mask = cv2.compare(res, 17, cv2.CMP_EQ)  # 0/1 mask for blob 17, same size as input image

if you really need contours:

cnt,hier = cv2.findContours(mask, .....)
edit flag offensive delete link more

Comments

1

I am not familiar with this kind of array access and I have not found any relevant docs in NumPy or elsewhere. Any reference? Is there a way to know how many blobs were found or valid indexes?

Using this technique gives me a mask which is a 1-dimensional array with all elements with the value '17' (and the resulting contours are all 571 x 0). Is something missing?

mrersatz gravatar imagemrersatz ( 2020-03-29 12:36:08 -0600 )edit

dearest apologies, not tested from python, see updated answer, please (use compare())

berak gravatar imageberak ( 2020-03-30 03:35:57 -0600 )edit

Is there a way to know how many blobs were found

cv2.minMaxLoc(), or simply np.max()

berak gravatar imageberak ( 2020-03-30 03:45:21 -0600 )edit

Thanks for the updates. It's working quite well now using compare.

mrersatz gravatar imagemrersatz ( 2020-03-30 06:19:46 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-03-29 10:46:11 -0600

Seen: 270 times

Last updated: Mar 30 '20