sbrunner / deskew

Library used to deskew a scanned document
MIT License
408 stars 46 forks source link

I'm getting an error here: grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #489

Open kylefoley76 opened 8 months ago

kylefoley76 commented 8 months ago

The error message is:

cv2.error: OpenCV(4.9.0) /Users/runner/work/opencv-python/opencv-python/opencv/modules/imgproc/src/color.cpp:196: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

I'm using Python 3.11.7 I have versions: opencv-python == 4.9.0.80

The code is:

def rotate(
        image: np.ndarray, angle: float, background: Union[int, Tuple[int, int, int]]
) -> np.ndarray:
    old_width, old_height = image.shape[:2]
    angle_radian = math.radians(angle)
    width = abs(np.sin(angle_radian) * old_height) + abs(np.cos(angle_radian) * old_width)
    height = abs(np.sin(angle_radian) * old_width) + abs(np.cos(angle_radian) * old_height)

    image_center = tuple(np.array(image.shape[1::-1]) / 2)
    rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
    rot_mat[1, 2] += (width - old_width) / 2
    rot_mat[0, 2] += (height - old_height) / 2
    return cv2.warpAffine(image, rot_mat, (int(round(height)), int(round(width))), borderValue=background)

def rotate2(file):

    image = cv2.imread(file)
    grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    angle = determine_skew(grayscale)
    rotated = rotate(image, angle, (0, 0, 0))
    cv2.imwrite(file, rotated)

    return
rotate2(file)
woctezuma commented 8 months ago

This is an error raised by OpenCV. Check if your image exists. See https://stackoverflow.com/a/52676770/376454