How can I draw a red circle around the detected blobs in the image?
I have the following images:
I want to achieve 3 results in the output:
- Highlights black dots/color patches in the image with a red circular outline around them.
- Calculate points/patches
- Prints the number of dots/patches overlaid on the image.
Right now, I can only count the dots/patterns in the image and print it:
import cv2
## convert to grayscale
gray = cv2.imread("blue.jpg", 0)
## threshold
th, threshed = cv2.threshold(gray, 100, 255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
## findcontours
cnts = cv2.findContours(threshed, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]
## filter by area
s1= 3
s2 = 20
xcnts = []
for cnt in cnts:
if s1<cv2.contourArea(cnt) <s2:
xcnts.append(cnt)
print("Number of dots: {}".format(len(xcnts)))
>>> Number of dots: 66
But I don't know how to highlight the color blocks on the image.
EDIT: Expected result for the image below:
would be something like this:
Here are some methods:
1. Color Threshold
The idea is to convert the image to HSV format and then define upper and lower color thresholds to isolate the desired color range. This produces a mask where we can find the outline of the mask and usecv2.findContours()
cv2.drawContours()
import numpy as np
import cv2
# Color threshold
image = cv2.imread('1.jpg')
original = image.copy()
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 127])
upper = np.array([179, 255, 255])
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(original,original,mask=mask)
# Find blob contours on mask
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(original,[c], -1, (36,255,12), 2)
cv2.imshow('result', result)
cv2.imshow('original', original)
cv2.waitKey()
2. Simple Thresholding
The idea is to threshold and get a binary mask. Likewise, to highlight color patches in the image, we use cv2.drawContours()
. To determine the number of colonies, we keep a counter while traversing the contour. Finally, to print the patch count onto the image, we usecv2.putText()
Colonies: 11
import numpy as np
import cv2
image = cv2.imread('2.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray, 5)
thresh = cv2.threshold(blur,100,255,cv2.THRESH_BINARY_INV)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
colonies = 0
for c in cnts:
cv2.drawContours(image, [c], -1, (36,255,12), 2)
colonies += 1
print("Colonies:", colonies)
cv2.putText(image, 'Colonies: {}'.format(colonies), (0, image.shape[0] - 15), \
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (36,255,12), 2)
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()
Color thresholding to detect blue blobs will also work
lower = np.array([0, 0, 0])
upper = np.array([179, 255, 84])
You can use this script to determine the HSV upper and lower color ranges
import cv2
import sys
import numpy as np
def nothing(x):
pass
# Load in image
image = cv2.imread('1.jpg')
# Create a window
cv2.namedWindow('image')
# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)
# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)
# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0
output = image
wait_time = 33
while(1):
# get current positions of all trackbars
hMin = cv2.getTrackbarPos('HMin','image')
sMin = cv2.getTrackbarPos('SMin','image')
vMin = cv2.getTrackbarPos('VMin','image')
hMax = cv2.getTrackbarPos('HMax','image')
sMax = cv2.getTrackbarPos('SMax','image')
vMax = cv2.getTrackbarPos('VMax','image')
# Set minimum and max HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(image,image, mask= mask)
# Print if there is a change in HSV value
if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
phMin = hMin
psMin = sMin
pvMin = vMin
phMax = hMax
psMax = sMax
pvMax = vMax
# Display output image
cv2.imshow('image',output)
# Wait longer to prevent freeze for videos.
if cv2.waitKey(wait_time) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()