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?
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)
plt.imshow(image1)
plt.show()
#image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
plt.imshow(image2)
plt.show()
# 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
plt.imshow(gray)
plt.show()
# Convert the grayscale image to binary
ret, binary = cv2.threshold(gray, 100, 255,
cv2.THRESH_OTSU)
# Display the binary image
plt.imshow(binary)
# 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
plt.imshow(inverted_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,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
# 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.
create new matrix, make sure it’s all 0.
draw the larger contour, give it the color “1”
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.