Ask Your Question
0

IndexError at cv2.minAreaRect(contours[0])

asked 2017-02-09 14:11:22 -0600

Daltz3 gravatar image

updated 2017-02-09 18:33:26 -0600

berak gravatar image

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()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-02-09 18:36:05 -0600

berak gravatar image

updated 2017-02-09 23:59:19 -0600

your contours list seems to be empty, so contours[0] is invalid.

please check like:

    if len(contours) > 0:
        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

but the main reason, you're not finding any contours is: your "lower" values for inRange() are larger than the "upper" ones (it should be the opposite), so already your binary img is all black only. it's probably a good idea, to visualize it, by putting an cv2.imshow("bin",bin) after the inRange() operation.

edit flag offensive delete link more

Comments

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

Daltz3 gravatar imageDaltz3 ( 2017-02-09 19:44:12 -0600 )edit

ok, total noob. no problem, please see edit above !

berak gravatar imageberak ( 2017-02-10 00:00:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-09 14:11:22 -0600

Seen: 1,085 times

Last updated: Feb 09 '17