0

I am doing image processing on Raspberry Pi 3, Model B, using Python and OpenCv.

I need to follow an orange object by drawing a circle around it. Here is the code.

    # import the necessary packages
    from picamera.array import PiRGBArray
    from picamera import PiCamera
    import time
    import cv2
    import numpy as np

    # initialize the camera and grab a reference to the raw camera capture
    camera = PiCamera()
    camera.resolution = (640, 480)
    camera.framerate = 30
    camera.hflip = True

    rawCapture = PiRGBArray(camera, size=(640, 480))

    # allow the camera to warmup
    time.sleep(0.1)

    # capture frames from the camera
    for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
            # grab the raw NumPy array representing the image, then initialize the timestamp
            # and occupied/unoccupied text
            image = frame.array

            blur = cv2.blur(image, (3,3))

            #hsv to complicate things, or stick with BGR
            hsv = cv2.cvtColor(blur,cv2.COLOR_BGR2HSV)
            thresh = cv2.inRange(hsv,np.array((0, 150, 150)), np.array((40, 220, 220)))

            #lower = np.array([0,100,200],dtype="uint8")
            #upper = np.array([90,180,255], dtype="uint8")

            #thresh = cv2.inRange(blur, lower, upper)
            thresh2 = thresh.copy()

            # find contours in the threshold image
            image, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

            # finding contour with maximum area and store it as best_cnt
            max_area = 0
            best_cnt = 1
            for cnt in contours:
                    area = cv2.contourArea(cnt)
                    if area > max_area:
                            max_area = area
                            best_cnt = cnt

            # finding centroids of best_cnt and draw a circle there
            M = cv2.moments(best_cnt)
            cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
            print("cx: " + str(cx) + " cy: " + str(cy))
            #if best_cnt>1:
            cv2.circle(blur,(cx,cy),40,(0,255,0),4)
            # show the frame
            cv2.imshow("Frame", blur)
            cv2.imshow('thresh',thresh2)
            key = cv2.waitKey(1) & 0xFF

            # clear the stream in preparation for the next frame
            rawCapture.truncate(0)

            # if the `q` key was pressed, break from the loop
            if key == ord("q"):
                    break

This code is succesfully compiled when I use python colourdetection.py on the termminal. However, When I try to compile this code by using sudo python colourdetection.py , I get the following error:

Traceback (most recent call last): File "colourdetection.py", line 38, in image, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) ValueError: need more than 2 values to unpack

I need to be able to compile this code using sudo, because I will have to combine it with a motor driving code, which is compiled with sudo pyhton motordrive.py

Any help ?

  • Don't use sudo. There is no reason to be using sudo to control the GPIO any more. – joan Dec 16 '16 at 20:10
  • My motor control module uses sudo, if I dont use sudo it gives module rpi.gpio module not found error. – Deniz Yildirim Dec 16 '16 at 20:34
  • Update RPi.GPIO. It no longer needs to be run with sudo. Or use a different Python module. – joan Dec 16 '16 at 20:51
  • I am really a beginner in python and raspberry. I did sudo apt-get update and sudo apt-get upgrade, but I still get the same error. What else should I do? – Deniz Yildirim Dec 17 '16 at 5:44
  • If you are using Raspbian a device /dev/gpiomem should now be present which an up to date RPi.GPIO will automatically use. Just check that the pi user is in the gpio group, e.g. sudo adduser pi gpio. Then don't launch RPi.GPIO scripts with sudo. – joan Dec 17 '16 at 9:03
1

In opencv2, findContours returns just two values, contours and hierarchy. Therefore line 38 should be corrected to be:

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.