How can I draw a red circle around the detected blobs in the image?


Cristada 673

I have the following images:

enter image description here

I want to achieve 3 results in the output:

  1. Highlights black dots/color patches in the image with a red circular outline around them.
  2. Calculate points/patches
  3. 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:

enter image description here

would be something like this:

enter image description here

childish

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()

enter image description here enter image description here

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()

Related


How can I make the image rotate around itself and the circle?

username I have an image of a curved rectangle that is part of a circle, so what I want is to have the rectangle rotate in a circular fashion as if it were the only visible part of the circle, while rotating around itself to get the desired effect. Hal I think

How can I make the image rotate around itself and the circle?

username I have an image of a curved rectangle that is part of a circle, so what I want is to have the rectangle rotate in a circular fashion as if it were the only visible part of the circle, while rotating around itself to get the desired effect. Hal I think

How can I draw an image of four equal quarters of a circle?

Pandey_Laxman I need four groups of images that draw a circle equally per quarter. Can anyone help me below is the screen Leo You just need to add one view, like 200*200 Then imageviwin this view add 4 then setcornerRadius self.testview.layer.cornerRadius = 10

How can I draw an image of four equal quarters of a circle?

Pandey_Laxman I need four groups of images that draw a circle equally per quarter. Can anyone help me below is the screen Leo You just need to add one view, like 200*200 Then imageviwin this view add 4 then setcornerRadius self.testview.layer.cornerRadius = 10

How can I draw a border around a translucent image? (css)

not clear I'd like to know how to draw a border around a semi-transparent image, that is, I don't need either a box-shaped border nor a border-radius, but a border that really fits the shape of the image itself. According to this article, this is not possible,

How can I draw a border around a translucent image? (css)

not clear I'd like to know how to draw a border around a semi-transparent image, that is, I don't need either a box-shaped border nor a border-radius, but a border that really fits the shape of the image itself. According to this article, this is not possible,

how to draw around a circle in svg

Jack Mason I'm trying to make a stroke around an svg circle, but have a flattened line on top. bottom. left and right. <div class="wrapper"> <svg version="1.1" id="elp-badge" class="headerbadge" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://w

how to draw around a circle in svg

Jack Mason I'm trying to make a stroke around an svg circle, but have a flattened line on top. bottom. left and right. <div class="wrapper"> <svg version="1.1" id="elp-badge" class="headerbadge" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://w

how to draw around a circle in svg

Jack Mason I'm trying to make a stroke around an svg circle, but have a flattened line on top. bottom. left and right. <div class="wrapper"> <svg version="1.1" id="elp-badge" class="headerbadge" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://w

how to draw around a circle in svg

Jack Mason I'm trying to make a stroke around an svg circle, but have a flattened line on top. bottom. left and right. <div class="wrapper"> <svg version="1.1" id="elp-badge" class="headerbadge" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://w

how to draw around a circle in svg

Jack Mason I'm trying to make a stroke around an svg circle, but have a flattened line on top. bottom. left and right. <div class="wrapper"> <svg version="1.1" id="elp-badge" class="headerbadge" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://w

how to draw around a circle in svg

Jack Mason I'm trying to make a stroke around an svg circle, but have a flattened line on top. bottom. left and right. <div class="wrapper"> <svg version="1.1" id="elp-badge" class="headerbadge" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://w

Red circle around custom Android button image

Gabe Pound I created a floating action button and made my own image (per dpi size), but it has a red circle around it (below). This is the xml where I define the button <android.support.design.widget.FloatingActionButton android:id="@+id/addplant" android:layo

Red circle around custom Android button image

Gabe Pound I created a floating action button and made my own image (every dpi size), but it has a red circle around it (below). This is the xml where I define the button <android.support.design.widget.FloatingActionButton android:id="@+id/addplant" android:la

How to draw an image on a circle

Sasasuke Hayashiuchi I've drawn two circles on the canvas (one with speed and the other with some controls) and I've been trying to draw an image on each circle, but I don't know how to do this. I have uploaded pictures. Does anyone know? Also, does anyone kno

How to draw an image on a circle

Sasasuke Hayashiuchi I've drawn two circles on the canvas (one with speed and the other with some controls) and I've been trying to draw an image on each circle, but I don't know how to do this. I have uploaded pictures. Does anyone know? Also, does anyone kno

How to draw an image on a circle

Sasuke Hayashi Uchine I've drawn two circles on the canvas (one with speed and one with some controls) and I've been trying to draw an image on each circle, but I don't know how to do this. I have uploaded pictures. Does anyone know? Also, does anyone know why

How to draw an image on a circle

Sasuke Hayashi Uchine I've drawn two circles on the canvas (one with speed and one with some controls) and I've been trying to draw an image on each circle, but I don't know how to do this. I have uploaded pictures. Does anyone know? Also, does anyone know why

How can I draw a line/draw on an image?

Rajash Mappu So far, I've been able to draw lines on a normal image, just like creating my own normal context of size CGRect and drawing lines on it. All the tutorials I've seen are how to draw on a created image context of size x*y. But I want to draw lines o