2

There are two images, one is original image with value, another let's call it shape image, with irregular shapes. I want to detect the shape, and add different value to the value img, corresponding to the shape img. shapes won't intersect with each other. I've been looking through multiple libs, including open cv2, yet find hard to accomplish this goal. Would anyone be able to help? Thanks.

possible shape image

possible shape image

I wish to add +1/-1 alternatively from outside to inside

I wish to add +1/-1 alternatively from outside to inside

Eather
  • 29
  • 4

1 Answers1

2

To detect shapes, you can use cv2.findContours() and the cv2.RETR_TREE flag. To determine the inner contours, we can use hierarchy to filter for each inner layer. Here's a good tutorial on contour hierarchy. Essentially, we iterate through each layer and alternate marking each contour (-1 or 1). To add the label, you can use cv2.putText(). You may have to change the offset of the label depending on the image that you're using.

Here's the results


import cv2

image = cv2.imread('1.png')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,120, 255,cv2.THRESH_BINARY_INV)[1]
cnts, h = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

label = '1'
count = 0

# Get inner list of hierarchy
for layer in zip(cnts, h[0]):
    contour = layer[0]
    hierarchy = layer[1]

    # If we find new contour (not inner) reset label
    if hierarchy[1] >= 0:
        label = '1'
    # Ensure that we only have outer contour
    if count % 2 == 0:
        cv2.drawContours(image, [contour], -1, (36, 255, 12), 2)
        x,y,w,h = cv2.boundingRect(contour)
        cv2.putText(image, label, (x +50,y+ 70), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36,255,12), 3)
        label = str(int(label) * -1)

    count += 1

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()
nathancy
  • 42,661
  • 14
  • 115
  • 137
  • Thanks, hierarchy is indeed what I need. However the pic i gave was misleading, my intention was to have all the pixels' value(img as a 2-d array) inside the shape increased by +1/-1. should I use floodfill? – Eather Jul 18 '19 at 16:19
  • Yes that would be one potential way of doing it – nathancy Jul 18 '19 at 19:48