首页 > 解决方案 > 正确对齐图像

问题描述

嗨,我正在尝试仅从图像中获取手写数据,因为我拍摄了一张空图像和一张填充图像,然后我正在执行 ImageChops.difference 以从中获取数据。现在的问题在于图像的对齐方式,两者在深度方面的对齐方式并不相同,因此结果不正确。

从 PIL 导入 Image、ImageChops

def compare_images(path_one, path_two, diff_save_location):
    """
    Compares to images and saves a diff image, if there
    is a difference

    @param: path_one: The path to the first image
    @param: path_two: The path to the second image
    """
    image_one = Image.open(path_one).convert('LA')
    image_two = Image.open(path_two).convert('LA')

    diff = ImageChops.difference(image_one, image_two)
    if diff.getbbox():
        diff.convert('RGB').save(diff_save_location)


if __name__ == '__main__':
    compare_images('images/blank.jpg',
                   'images/filled.jpg',
                   'images/diff.jpg')

这是我得到的结果。 在此处输入图像描述

我正在寻找的结果: 在此处输入图像描述

谁能帮我这个。谢谢。

标签: python-3.ximageimage-processingpython-imaging-library

解决方案


This site may be helpful: https://www.learnopencv.com/image-alignment-feature-based-using-opencv-c-python/ . The main idea is to first detect keypoints use SIFT, SURF or other algorithms in both images; then match the keypoints from the empty image with the keypoints from the handwritten image, to get a homography matrix; then use this matrix to align the two images.

After image alignment, post processing may be needed due to illumination or noise.


推荐阅读