Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Grab second largest contour from max()

I'm completely new to OpenCV and Python (Java Developer) so It'd be much appreciated if detailed instructions were included. Thanks.

Okay, My goal is to grab two contours (same color) that are the largest contour and the second largest contour AND OR the same size contour. How would I go about this?

    # import the necessary packages
import numpy as np
import imutils
import cv2

#define HSV ranges

# green
greenLower = (35,21,62)
greenUpper = (90,255,255)

camera = cv2.VideoCapture(0)

while True:
    (grabbed, frame) = camera.read()

    #convert to HSV color space
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    #construct a mask for the color, then perform
    #a series of dilations and erosions to remove any small
    #blobs left in the mask

    green_mask = cv2.inRange(hsv, greenLower, greenUpper)
    green_mask = cv2.erode(green_mask, None, iterations=2)
    green_mask = cv2.dilate(green_mask, None, iterations=2)

    grncnts = cv2.findContours(green_mask.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)[-2]

    if len(grncnts)>0:
        green=max(grncnts, key=cv2.contourArea)
        (xg,yg,wg,hg) = cv2.boundingRect(green)
        cv2.rectangle(frame, (xg,yg), (xg+wg, yg+hg), (0,255,0), 2)
    if len(grncnts)>0:
        green=min(grncnts, key=cv2.contourArea)
        (xg,yg,wg,hg) = cv2.boundingRect(green)
        cv2.rectangle(frame, (xg,yg), (xg+wg, yg+hg), (0,255,0), 2)

    cv2.imshow("Frame", frame)
    #cv2.imshow("Mask", mask)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break

# cleanup the camera and close open windows
camera.release()
cv2.destroyAllWindows()

Grab second largest contour from max()

I'm completely new to OpenCV and Python (Java Developer) so It'd be much appreciated if detailed instructions were included. Thanks.

Okay, My goal is to grab two contours (same color) that are the largest contour and the second largest contour AND OR the same size contour. How would I go about this?

EDIT: Also as a note, my current code grabs the biggest contour and the smallest contour.

    # import the necessary packages
import numpy as np
import imutils
import cv2

#define HSV ranges

# green
greenLower = (35,21,62)
greenUpper = (90,255,255)

camera = cv2.VideoCapture(0)

while True:
    (grabbed, frame) = camera.read()

    #convert to HSV color space
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    #construct a mask for the color, then perform
    #a series of dilations and erosions to remove any small
    #blobs left in the mask

    green_mask = cv2.inRange(hsv, greenLower, greenUpper)
    green_mask = cv2.erode(green_mask, None, iterations=2)
    green_mask = cv2.dilate(green_mask, None, iterations=2)

    grncnts = cv2.findContours(green_mask.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)[-2]

    if len(grncnts)>0:
        green=max(grncnts, key=cv2.contourArea)
        (xg,yg,wg,hg) = cv2.boundingRect(green)
        cv2.rectangle(frame, (xg,yg), (xg+wg, yg+hg), (0,255,0), 2)
    if len(grncnts)>0:
        green=min(grncnts, key=cv2.contourArea)
        (xg,yg,wg,hg) = cv2.boundingRect(green)
        cv2.rectangle(frame, (xg,yg), (xg+wg, yg+hg), (0,255,0), 2)

    cv2.imshow("Frame", frame)
    #cv2.imshow("Mask", mask)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break

# cleanup the camera and close open windows
camera.release()
cv2.destroyAllWindows()