0

I'm doing a driverless car project using Respberry Pi3. The following code is for testing, to capture the images from the webcam and store them into my folder.

However, I received error: (-215) scn == 3 || scn == 4 in function cvtColor error, on gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY). But all images are stored successfully. Please give me a hint, where goes wrong in my code ?? Thank you

import threading
import numpy as np
import cv2
#import RPi.GPIO as GPIO

cap=cv2.VideoCapture(0)
i = 0
LEdge = 0
REdge = 0
lineErr = 0

#GPIO.setmode(GPIO.BOARD)
#GPIO.setup(12,GPIO.OUT)
#p = GPIO.PWM(12,100)
#p.start(0)

def fetch_img(image):
        global i, fn, im, gray
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        img_gray_blur = cv2.GaussianBlur(gray, (5,5), 0)
        canny_edges = cv2.Canny(img_gray_blur, 10, 70)
        ret, mask = cv2.threshold(canny_edges, 90, 255, cv2.THRESH_BINARY)
        i = i + 1
        fn = '/home/alex/All IT Courses/Master Computer Vision OpenCV/images/img/'+str(i)+'.jpg'
        cv2.imwrite(fn, mask)
        return mask


def process_img():
        t = threading.Timer(0.1, process_img)
        t.start()
        global LEdge, REdge, lineErr

        while True:
            ret, im = cap.read()
            cv2.imshow("Test", fetch_img(im))
            if cv2.waitKey(1) == 13:  # 13 is the Enter Key - ASCII
                break
        cap.release()
        cv2.destroyAllWindows()

    #gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    #cv2.imshow("Test", gray)

    for j in range(1, 640):
            if gray[240][j] < 80: 
                    gray[240][j] = 0
            else:
                    gray[240][j] = 1
    for k in range(320, 640, 1):
            if gray[240][k] == 0:
                    REdge = k
                    break
    for k in range(320, 0, -1):
            if gray[240][k] == 0:
                    LEdge = k
                    break
    lineErr = (REdge+LEdge)/2-320
    print(lineErr)

def main():
    global thread, line
    process_img()
    thread = threading.Timer(1, process_img)
    thread.start()
    #try:
    #        while 1:
    #                dc = 8 - line/40
    #                if dc > 10:
    #                        dc = 10
    #                if dc < 6:
    #                        dc = 6
    #                p.ChangeDutyCycle(dc)
    #except KeyboardInterrupt:
    #        pass
    #p.stop()
    #GPIO.cleanup()

if __name__ == "__main__":
      main()
Alex M
  • 89
  • 1
  • 2
  • 12
  • 2
    Before the line `cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)`, you must check if `image` is a valid numpy array using `if image is not None:` Then perform `cv2.cvtColor` – ZdaR May 21 '18 at 16:02
  • Piling on to @ZdaR's comment, you could make sure image is valid, either by `if image is not None` or by inspecting `ret`. According to [docs](https://docs.opencv.org/master/d8/dfe/classcv_1_1VideoCapture.html#a473055e77dd7faa4d26d686226b292c1)`ret` is False (0) if no frame was grabbed, so you can do `if ret:`. In general, I've noticed that OpenCV doesn't throw exceptions if image acquistion didn't go as planned and you are supposed to do it yourself. – bfris May 21 '18 at 17:53

0 Answers0