Determine if a point is inside or outside of a shape with opencv

后端 未结 3 732
梦如初夏
梦如初夏 2020-12-11 02:07

I have images with white background and simple shapes in them (each image has one shape). I want to determine if a certain point (x,y) is inside the shape or not. How can I

3条回答
  •  萌比男神i
    2020-12-11 02:23

    if you want to access all the points inside the convex hull, you can do masking

    I solve this by first painting my convex hull white colour with cv2.fillPoly() on a black frame

    1. First create black frame that follows your frame's shape
      black_frame = np.zeros_like(your_frame).astype(np.uint8)
    2. Paint the convex hull with white
      cv2.fillPoly(black_frame , [hull], (255, 255, 255))
    3. Create a mask by using numpy boolean indexing, it will produce a mask with True/False values inside, it will be True is the pixel value is white
      mask = black_frame == 255
    4. You can access your pixel values by getting the product between your frame and mask, if False, value will
      targetROI = your_frame * mask
    5. Access your pixels by using the mask.
    black_frame = np.zeros_like(your_frame).astype(np.uint8)
    cv2.fillPoly(black_frame , [hull], (255, 255, 255))
    mask = black_frame == 255
    targetROI = your_frame * mask
    

提交回复
热议问题