image1

image2

这些是我的照片,我想

  • 将PDF文档(file_1.pdffile_2.pdf)转换为图像.
  • 比较图像以检测它们之间的任何差异.
  • 突出显示两个图像之间的变化或差异区域.

这是我的代码

from pdf2image import convert_from_path
import cv2
import numpy as np
from PIL import Image, ImageOps
from IPython.display import display`

def process_and_display_image(pdf_path, target_size=(800, 600),save_path='processed_image.jpeg'):
    images = convert_from_path(pdf_path)
    image = images[0]
    image = ImageOps.exif_transpose(image)
    image.thumbnail(target_size, Image.Resampling.LANCZOS)
    image_np = np.array(image)
    image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
    image_processed = Image.fromarray(image_np)
    display(image_processed)
    image_processed.save(save_path, 'JPEG')
    print(f"Image saved as {save_path}")

# Display image from PDF
process_and_display_image("file_1.pdf",save_path='file_1.jpeg')
process_and_display_image("file_2.pdf",save_path='file_2.jpeg')

import matplotlib.pyplot as plt
image1 = cv2.imread('file_1.jpeg', cv2.IMREAD_UNCHANGED)
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
image2 = cv2.imread('file_2.jpeg', cv2.IMREAD_UNCHANGED)
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)

if image1.shape == image2.shape:
    difference = cv2.absdiff(image1,image2)
difference = 255 - difference

if image1.shape == image2.shape:
    
    overlay = cv2.addWeighted(image1, 0.5, image2, 0.5, 0) 
       
difference = overlay
plt.imshow(difference)
plt.axis('off')
plt.show()

My output is -> My_output

Expected output -> Expected output

推荐答案

我try 使用1.jpg2.jpg作为输入文件,所以我不得不修剪你关于convert pdf to image的代码,我在SO上的某个地方添加了两个函数,以更好地可视化我的环境,代码中的结果:

# from pdf2image import convert_from_path
import cv2
import numpy as np
from PIL import Image, ImageOps
# from IPython.display import display
import matplotlib.pyplot as plt

def ResizeWithAspectRatio(image, width=None, height=None, inter=cv2.INTER_AREA):
    dim = None
    (h, w) = image.shape[:2]

    if width is None and height is None:
        return image
    if width is None:
        r = height / float(h)
        dim = (int(w * r), height)
    else:
        r = width / float(w)
        dim = (width, int(h * r))

    return cv2.resize(image, dim, interpolation=inter)


draw_windows = True  ## change fo False for no windows only calc


def drawWindow(window_name, image):
    if draw_windows:
        
        resize = ResizeWithAspectRatio(image, width= 500)
        
        cv2.imshow(window_name, resize)
        
        cv2.moveWindow(window_name, 600, 200)
        
        cv2.waitKey(0)
        cv2.destroyAllWindows()

# def process_and_display_image(pdf_path, target_size=(800, 600),save_path='processed_image.jpeg'):
#     images = convert_from_path(pdf_path)
#     image = images[0]
#     image = ImageOps.exif_transpose(image)
#     image.thumbnail(target_size, Image.Resampling.LANCZOS)
#     image_np = np.array(image)
#     image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
#     image_processed = Image.fromarray(image_np)
#     display(image_processed)
#     image_processed.save(save_path, 'JPEG')
#     print(f"Image saved as {save_path}")

# # Display image from PDF
# process_and_display_image("file_1.pdf",save_path='file_1.jpeg')
# process_and_display_image("file_2.pdf",save_path='file_2.jpeg')

# import matplotlib.pyplot as plt

##READ IMAGE 1
image1 = cv2.imread('1.jpg', cv2.IMREAD_UNCHANGED)


image1_gray = image1.copy()


## https://stackoverflow.com/questions/39058177/how-to-change-the-black-color-to-red-with-opencv-python
ret, mask = cv2.threshold(image1_gray, 0, 255, cv2.THRESH_BINARY_INV |cv2.THRESH_OTSU)

image1 = cv2.cvtColor(image1, cv2.COLOR_GRAY2RGB)

image1_copy = image1.copy()

print('mask : ', mask.shape , mask.size, mask.ndim , np.min(mask) , np.max(mask), len(np.unique(mask)))

image1[mask == 255] = [0, 0, 255]

drawWindow('image1', image1)

cv2.imwrite('image1_red.png' , image1)

print('image1 : ', image1.shape , image1.size, image1.ndim , np.min(image1) 
                  
                          , np.max(image1), len(np.unique(image1)))



##READ IMAGE 2
image2 = cv2.imread('2.jpg', cv2.IMREAD_UNCHANGED)

image2_gray = image2.copy()

ret, mask = cv2.threshold(image2_gray, 0, 255,cv2.THRESH_BINARY_INV |cv2.THRESH_OTSU)

image2 = cv2.cvtColor(image2, cv2.COLOR_GRAY2RGB)

image2_copy = image2.copy()

image2[mask == 255] = [255, 0, 0]

drawWindow('image2', image2)

cv2.imwrite('image2_blue.png' , image2)


if image1.shape == image2.shape:
    
    overlay = cv2.addWeighted(image1, 0.5, image2, 0.5, 0) 
       
difference = overlay


print('difference : ', difference.shape , difference.size, difference.ndim ,
      
                  np.min(difference) , np.max(difference), len(np.unique(difference)))


drawWindow('difference', difference)


cv2.imwrite('difference.png' , difference)


difference2 = np.where((image1_copy == image2_copy) , image1_copy , difference)


print('difference2 : ', difference2.shape , difference2.size, difference2.ndim ,
      
                  np.min(difference2) , np.max(difference2), len(np.unique(difference2)))


drawWindow('difference2', difference2)


cv2.imwrite('difference2.png' , difference2)



diffs = difference2 - difference

print('diffs : ', diffs.shape , diffs.size, diffs.ndim ,
      
                  np.min(diffs) , np.max(diffs), len(np.unique(diffs)))


drawWindow('diffs', diffs)


cv2.imwrite('diffs.png' , diffs)


plt.imshow(difference2)
plt.axis('off')
plt.show()

让我们来看看difference.png个:

enter image description here

difference2.png查看像素放大倍数的上角,以获得差异:

enter image description here

最后是difference.pngdifference2.png之间的区别;diffs.png:

enter image description here

我不能准确地得到你的输出,有些东西不符合我的逻辑,但这是我最大的努力,如果你找到更好的更快的解决方案,请让我们知道. 我利用How to change black color to Red with OpenCV Python?获得了红色和蓝色图像,不知道是否有更好的解决方案可以简化输出图像中不同部分的识别,其中我的:

difference2 = np.where((image1_copy == image2_copy) , image1_copy , difference)

未能达到预期效果

Python相关问答推荐

Python多处理:当我在一个巨大的pandas数据框架上启动许多进程时,程序就会陷入困境

Pandas实际上如何对基于自定义的索引(integer和非integer)执行索引

对整个 pyramid 进行分组与对 pyramid 列子集进行分组

删除所有列值,但判断是否存在任何二元组

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

如何将Docker内部运行的mariadb与主机上Docker外部运行的Python脚本连接起来

如何在python polars中停止otherate(),当使用when()表达式时?

Godot:需要碰撞的对象的AdditionerBody2D或Area2D以及queue_free?

基于索引值的Pandas DataFrame条件填充

连接一个rabrame和另一个1d rabrame不是问题,但当使用[...]'运算符会产生不同的结果

无法连接到Keycloat服务器

可以bcrypts AES—256 GCM加密损坏ZIP文件吗?

Flash只从html表单中获取一个值

如何在海上配对图中使某些标记周围的黑色边框

为什么Python内存中的列表大小与文档不匹配?

大型稀疏CSR二进制矩阵乘法结果中的错误

是否需要依赖反转来确保呼叫方和被呼叫方之间的分离?

来自任务调度程序的作为系统的Python文件

如何批量训练样本大小为奇数的神经网络?

为什么fizzbuzz在两个数字的条件出现在一个数字的条件之后时不起作用?