Ask Your Question

Daltz3's profile - activity

2018-07-17 05:17:53 -0600 received badge  Student (source)
2017-02-15 16:35:07 -0600 commented answer Grab second largest contour from max()

Could I see a working example of this? Like how do I change my code to match this?

2017-02-14 19:36:08 -0600 received badge  Editor (source)
2017-02-14 19:34:57 -0600 asked a question 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()
2017-02-10 06:07:30 -0600 received badge  Scholar (source)
2017-02-09 20:08:23 -0600 commented answer IndexError at cv2.minAreaRect(contours[0])

After implementing it, nothing happens. No display, no print. It just blanks. Not even an error message. http://pastebin.com/raw/beKBj5bD

2017-02-09 14:11:40 -0600 asked a question IndexError at cv2.minAreaRect(contours[0])

I keep getting an IndexError at cv2.minAreaRect and my screen only displays a white screen. I'm new to python (java developer) I have absolutely no idea what's going on. Some help would be greatly appreciated!

Code is running on a Raspberry Pi3 connecting to an IP Camera. Can successfully connect to camera via browser. Also I'm trying to detect the green color range so no idea how HSV ranges work.

PS: Sorry about the ugly formatting, pasting from eclipse to here breaks everything. I can't post a pastebin link either.

#imports
import cv2
import numpy as np
import time
import argparse
import array
from collections import deque
from networktables import NetworkTables

#NetworkTables information
NetworkTables.initialize(server='OPENCV.ORG WONT LET ME DO IP')
Table = NetworkTables.getTable("table")
array.array('i')

cap = cv2.VideoCapture('OPENCV.ORG WONT LET ME DO IP')

while(True):
    try:
    # Capture frame-by-frame
        ret, frame = cap.read()

    #set color to HSV
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV);

    #set color range
        greenLower = (120, 100, 100) #adjust as needed
        greenUpper = (109, 48, 89)

    #set color range
        bin = cv2.inRange(gray, greenLower, greenUpper)

    #Our operations on the frame come here
        bin = cv2.dilate(bin, None)  # fill some holes
        bin = cv2.dilate(bin, None)
        bin = cv2.erode(bin, None)   # dilate made our shape larger, revert that
        bin = cv2.erode(bin, None)
        bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

        #find box coordinates, if break add while
        rc = cv2.minAreaRect(contours[0])
        box = cv2.boxPoints(rc)
        for p in box: 
            pt = (p[0],p[1])
            cv2.circle(ret,pt,5,(200,0,0),2)

        #Display the resulting frame
        cv2.imshow('frame',bin) #shows the frame to screen
        if cv2.waitKey(1) & 0xFF == ord('q'): #if 'q' key is pressed, end feed
            break

    except IndexError:
        print("No Contours Found")

#safely end code sequence
cap.release()
cv2.destroyAllWindows()