0 Python-Opencv错误:错误:(-215)scn == 3 || scn == 4在函数cvtColor中

时间:2018-05-21 16:00:01

标签: python opencv

我正在使用Respberry Pi3做无人驾驶汽车项目。以下代码用于测试,从网络摄像头捕获图像并将其存储到我的文件夹中。

然而,我收到错误:( - 1515)scn == 3 || scn == 4 in function cvtColor error,on gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)。但是所有图像都成功存储。请给我一个提示,我的代码出错了?谢谢

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()

0 个答案:

没有答案