Kalsan
Kalsan

Reputation: 1039

OpenCV: Object tracking with denoising

I'm attempting to extract a blue object, very much like the one described in https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html#object-tracking

An example of a raw image with three blue shapes to extract is here:

Three shapes that should be segmented

The captured image is noisy and the unfiltered shape detection returns hundreds to thousands of "blue" shapes. In order to mitigate this, I applied the following steps:

The complete code is:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
while(True):
    ret, frame = cap.read()
    blur = cv2.GaussianBlur(frame, (15, 15), 0)

    hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
    lower_red = np.array([115, 50, 50])
    upper_red = np.array([125, 255, 255])
    mask = cv2.inRange(hsv, lower_red, upper_red)
    blue = cv2.bitwise_and(blur, blur, mask=mask)
    gray = cv2.cvtColor(blue, cv2.COLOR_BGR2GRAY)

    (T, ted) = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)

    im2, contours, hierarchy = cv2.findContours(
        ted, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        cv2.drawContours(frame, [cnt], 0, (0, 255, 0), 3)

    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(frame, str(len(contours)), (10, 500), font, 2, (0, 0, 255), 2, cv2.LINE_AA)

    cv2.imshow('mask', mask)
    cv2.imshow('blue', blue)
    cv2.imshow('grey', gray)
    cv2.imshow('thresholded', ted)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Unfortunately, there are still 6-7 contours left whereas there should be three.

How can I further refine image processing to get just the three shapes?

Upvotes: 1

Views: 588

Answers (1)

Paolo Irrera
Paolo Irrera

Reputation: 253

You could use morphological operations coupled with connected components analysis:

If the shapes that you're looking for specific shapes (e.g. shapes), you could use some shape descriptors.

Finally, I suggest you trying replacing the Gaussian Filter with a bilateral filter (https://docs.opencv.org/3.0-beta/modules/imgproc/doc/filtering.html#bilateralfilter) to better preserve the shapes. If you want an even better filter, have a look at this tutorial on NL-means filter (https://docs.opencv.org/3.3.1/d5/d69/tutorial_py_non_local_means.html)

Upvotes: 1

Related Questions