Segment image by lines

I have images in .jpg format which I are segmented in three different areas by simple lines. I want to convert now these 3 areas: One area should only contain 0, and the other 1 and the third 2.
What would be the easiest way to do so?

And I also have the only the annotation


really ? what would be the next step, once you have the areas ?
this kind of “annotation” is quite destructive, no ?

sure, you can try findContours() with RETR_TREE, and have some fun with MNIST to decipher the numbers, but it all looks like a “broken plan” to me …

This image is just an example. The numbers are an example as well. The area with the 0 should contain all 0 in the end and the other areas similiarly.

This works but I receive 5 contours and it only should be two of them

# load images
image1 = cv2.imread("image10prime.jpg")
image2 = cv2.imread("image10-1.jpg")

#image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)


#image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)


# compute difference
difference = cv2.subtract(image1, image2)

new_image = difference.copy()

# Convert the image to grayscale
gray = cv2.cvtColor(difference, cv2.COLOR_BGR2GRAY)
# Display the grayscale image

# Convert the grayscale image to binary
ret, binary = cv2.threshold(gray, 100, 255, 
# Display the binary image

# To detect object contours, we want a black background and a white 
# foreground, so we invert the image (i.e. 255 - pixel value)
inverted_binary = ~binary

# Find the contours on the inverted binary image, and store them in a list
# Contours are drawn around white blobs.
# hierarchy variable contains info on the relationship between the contours
contours, hierarchy = cv2.findContours(inverted_binary,
# Show the total number of contours that were detected
print('Total number of contours detected: ' + str(len(contours)))

basically you need to draw contours, but filled contours.

  1. create new matrix, make sure it’s all 0.
  2. draw the larger contour, give it the color “1”
  3. draw the smaller contour, give it the color “2”

and there’s your label map.

and make sure there are no little circles (“0”) in the outer area. they are in the way. in fact, have no “numbers” there at all. just two properly closed circles, designating the outer and the inner area.

This post can be closed