Ask Your Question
0

How to draw contour lines between every colour?

asked Jan 14 '0

OTapio gravatar image

updated Oct 11 '0

I have a Numpy array that contains values between 0 and 255. How can I get contour lines between every color and not just 0 and non-zero?

My situation is the following

image description

It should be more like

image description

In theory I could loop over the array and setting everything to zero except single color and find contours for that, but that would be incredible tedious and I was hoping there was a more elegant solution.

Preview: (hide)

Comments

for my curiosity, what do the original images represent?

LBerger gravatar imageLBerger (Jan 15 '0)edit

These are biological cells. I'm doing cell segmentation.

OTapio gravatar imageOTapio (Jan 15 '0)edit

3 answers

Sort by » oldest newest most voted
0

answered Jan 20 '0

supra56 gravatar image

updated Jan 20 '0

Try this:

#!/usr/bin/python3.8.1
#OpenCV 4.2.0, Raspberry pi 3/3b/4b, Buster v10
#Date: 20th January, 2020

import cv2

img = cv2.imread('bio.png')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 50, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print("Number of contours = {}".format(str(len(contours))))
print('contours {}'.format(contours[0]))

cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
cv2.drawContours(imgray, contours, -1, (0, 255, 0), 3)

cv2.imshow('Image', img)
cv2.imshow('Image GRAY', imgray)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

image description

Preview: (hide)
0

answered Jan 20 '0

BadMachine gravatar image

Also try to use Canny edge detector before findContours

Preview: (hide)
0

answered Jan 15 '0

You could use OpenCV's inrange function that will threshold based on color channel ranges. https://docs.opencv.org/3.4/da/d97/tu...

image description

In this screen capture, the 2nd image from the left is the result of inrange on the background color. The 3rd image is the result of inrange on one of the yellow colors. The 4th image is the result of inrange on one of the blue colors. You would need a loop to find a foreground region, get its color, use inrange to find contours/blobs of this color, then remove those foreground regions. Continue the loop until there are no more foreground regions. You can try this tool for yourself at pixelprocessing.com

Preview: (hide)

Comments

Thanks. I'll have to try this out.

OTapio gravatar imageOTapio (Jan 15 '0)edit

Question Tools

1 follower

Stats

Asked: Jan 14 '0

Seen: 8,845 times

Last updated: Jan 20 '20