Tag Archives: Image Processing

Python SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3:

today Python digital image processing installed anaconda, using its default editor spyder. But an error occurred while running a simple program like the one shown below. The error is:

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

where the simple test code is:

 

# -*- coding: utf-8 -*-
"""
Created on Tue Oct 24 21:31:25 2017

@author: harchi
"""
from skimage import io
img=io.imread('C:\Users\harchi\Desktop\图像倄理\skeleton.bmp')
io.imshow(img)

the cause of the error is: imread(‘C:\Users\harchi\Desktop\ skeleton. BMP ‘) the “\” in the line represents an escape in Python.

the solution, of course, is not to let “\” stand for escape. So you can:

1, prefix the string with r or r, i.e. imread(r’C:\Users\harchi\Desktop\ skeleton. BMP ‘) where r or r denotes an unescaped string in python.

2, before “\” with “\” to achieve escape. Namely: imread (‘ C: \ \ Users \ \ harchi \ \ Desktop \ \ \ \ skeleton image processing BMP ‘) </ span> </ p>

</ span> 3, “\” change into “/”, namely: the imread (‘ C:/Users \ harchi/Desktop/image processing/skeleton BMP ‘) </ span> </ p>

</ span> finally: add python string prefix knowledge:

1, prefix the string with r or r to indicate that the string is an unescaped original string.

2, prefix a string with u or u to indicate that the string is unicode.

Extension of image edge by Python opencv

original image

expands the image outwards according to the pixel value of the image boundary, expanding 50 pixels in each direction.
a = cv2. CopyMakeBorder (img, 50,50,50,50, cv2. BORDER_REPLICATE)

near the boundary of the 50 pixels fold out (axisymmetric) :
a = cv2. CopyMakeBorder (img, 50,50,50,50, cv2 BORDER_REFLECT)

constant fill:

a = cv2.copymakeborder (img,50,50,50, cv2.border_constant,value=[0,255,0])