Python报错:(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXX

IDE: VScode
环境:python3.6
启动调试报错:

(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Python报错:(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXX_第1张图片
调试代码发现是读取文件路径问题,只要在文件路径前加一个 r 即可。
原代码
在这里插入图片描述

#coding=utf-8
import cv2
import numpy as np

def CannyThreshold(lowThreshold):
    detected_edges = cv2.GaussianBlur(gray, (3, 3), 0)
    detected_edges = cv2.Canny(
        detected_edges, lowThreshold, lowThreshold*ratio, apertureSize=kernel_size)
    # just add some colours to edges from original image.
    dst = cv2.bitwise_and(img, img, mask=detected_edges)
    cv2.imshow('canny demo', dst)


lowThreshold = 0
max_lowThreshold = 100
ratio = 3
kernel_size = 3

img = cv2.imread('C:\Users\huang\Desktop\EdgeDetectionCode\1.BMP')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.namedWindow('canny demo')

cv2.createTrackbar('Min threshold', 'canny demo',
                   lowThreshold, max_lowThreshold, CannyThreshold)

CannyThreshold(0)  # initialization
if cv2.waitKey(0) == 27:
    cv2.destroyAllWindows()


#coding=utf-8
import cv2
import numpy as np  
 
img = cv2.imread("D:/lion.jpg", 0)
 
img = cv2.GaussianBlur(img,(3,3),0)
canny = cv2.Canny(img, 50, 150)
 
cv2.imshow('Canny', canny)
cv2.waitKey(0)
cv2.destroyAllWindows()

你可能感兴趣的:(Python编程,代码报错)