how to get the min a contour of the color with HSV?

209 Views Asked by At

I'm trying to work on an image-processing. So, I need to grab the max and min area of the contour under for pic, contour in enumerate(contours): after selecting the min area if (area > 2000):

I could grab the max and min of the contour outside for loop, the problem that I need which min contour greater than 2000 in this code.

my full code:

import cv2
import numpy as np
from imutils.video import FPS

import time

cap = cv2.VideoCapture(0)

width = cap.get(3)  # float
height = cap.get(4)  # float
print width, height
time.sleep(2.0)
fps = FPS().start()
while (1):
    _, img = cap.read()

    if _ is True:
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    else:
        continue


    blue_lower = np.array([86,0,90], np.uint8)
    blue_upper = np.array([163, 64, 145], np.uint8)
    blue = cv2.inRange(hsv, blue_lower, blue_upper)
    kernal = np.ones((9, 9), "uint8")
    blue = cv2.dilate(blue, kernal)
    res_blue = cv2.bitwise_and(img, img, mask=blue)

    (_, contours, hierarchy) = cv2.findContours(blue, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for pic, contour in enumerate(contours):
        area = cv2.contourArea(contour)
        if (area > 2000):
            print area
            x, y, w, h = cv2.boundingRect(contour)
            img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            cv2.putText(img, "Blue Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0))
    if len(contours) > 0:
        c = max(contours, key=cv2.contourArea)

        x, y, w, h = cv2.boundingRect(c)
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 5)
        cv2.putText(img, "Blue Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0))

    cv2.imshow("Color Tracking", img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break
    fps.update()

Any ideas or suggestions will be appreciated

0

There are 0 best solutions below