4

I'm trying to detect a blob from the following image. I used the skimage and used the 3 different methods explained in the manual, but it's not able to detect the grey blob. Here is the original image:

enter image description here

So I tried the following code:

from math import sqrt
import cv2
from skimage.feature import blob_dog, blob_log, blob_doh
from skimage.color import rgb2gray
import matplotlib.pyplot as plt

image = cv2.imread("blob800_cropped.png")
image_gray = rgb2gray(image)

blobs_log = blob_log(image_gray, max_sigma=30, num_sigma=10, threshold=.05)

# Compute radii in the 3rd column.
blobs_log[:, 2] = blobs_log[:, 2] * sqrt(2)

blobs_dog = blob_dog(image_gray, max_sigma=30, threshold=.05)
blobs_dog[:, 2] = blobs_dog[:, 2] * sqrt(2)

blobs_doh = blob_doh(image_gray, max_sigma=30, threshold=.01)

blobs_list = [blobs_log, blobs_dog, blobs_doh]
colors = ['yellow', 'lime', 'red']
titles = ['Laplacian of Gaussian', 'Difference of Gaussian',
          'Determinant of Hessian']
sequence = zip(blobs_list, colors, titles)

fig, axes = plt.subplots(1, 3, figsize=(9, 3), sharex=True, sharey=True)
ax = axes.ravel()

for idx, (blobs, color, title) in enumerate(sequence):
    ax[idx].set_title(title)
    ax[idx].imshow(image)
    for blob in blobs:
        y, x, r = blob
        c = plt.Circle((x, y), r, color=color, linewidth=2, fill=False)
        ax[idx].add_patch(c)
    ax[idx].set_axis_off()

plt.tight_layout()
plt.show()

However, the blob which I'm looking for is not detected:

enter image description here

This is the output I'm expecting:

enter image description here

1 Answer 1

8

Here is how I would do that in Python/OpenCV.

  • Read input
  • Convert to gray
  • Apply extreme adaptive thresholding
  • Apply morphology open and close to remove small regions
  • Get contours and save the largest
  • Draw the largest contour on the input
  • Save the results

Input:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread("doco3.jpg")

# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# do adaptive threshold on gray image
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 101, 3)

# apply morphology open then close
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
blob = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
blob = cv2.morphologyEx(blob, cv2.MORPH_CLOSE, kernel)

# invert blob
blob = (255 - blob)

# Get contours
cnts = cv2.findContours(blob, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
big_contour = max(cnts, key=cv2.contourArea)

# test blob size
blob_area_thresh = 1000
blob_area = cv2.contourArea(big_contour)
if blob_area < blob_area_thresh:
    print("Blob Is Too Small")

# draw contour
result = img.copy()
cv2.drawContours(result, [big_contour], -1, (0,0,255), 1)

# write results to disk
cv2.imwrite("doco3_threshold.jpg", thresh)
cv2.imwrite("doco3_blob.jpg", blob)
cv2.imwrite("doco3_contour.jpg", result)

# display it
cv2.imshow("IMAGE", img)
cv2.imshow("THRESHOLD", thresh)
cv2.imshow("BLOB", blob)
cv2.imshow("RESULT", result)
cv2.waitKey(0)


Threshold image:

enter image description here

Morphology cleaned image of blob:

enter image description here

Resulting contour on input:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

this is great, thanks. One quick question, how should I set a limit size for the blob? For example if the max blob size is smaller than some value, I want to be able to return "No Blob". Thanks for your help @fmw42
Add after big_contour=... blob_area = cv2.contourArea(big_contour) and then if blob_area < blob_area_thresh: print("Too Small") where blob_area_thresh is your area limitation. See my change in my answer
best response ever! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.