简体   繁体   English

我如何使用 pil 在 python 中查看两个图像之间的区别?

[英]how do i see the difference between two images in python using pil?

I'm trying to make a program that looks from my macbook's camera (or any camera) and lets me know when something happened (lights turned on/off, any motion, etc), the program takes a screenshot every 1 second and compares the last image taken with the one that it just took.我正在尝试制作一个程序,它可以从我的 macbook 的相机(或任何相机)中查看并让我知道发生了什么事情(灯光打开/关闭、任何动作等),该程序每 1 秒截取一次屏幕截图并比较最后一张照片是用刚刚拍摄的照片拍摄的。

Here is the code in my while loop这是我的 while 循环中的代码

while True:
    time.sleep(1)

    image = capture_image()

    if last_image == None:
        last_image = image

    # compare the two images

    print('image', image)
    print('last image', last_image)
    print('')

    last_image = image

Here is an example to know the difference between 2 pictures.Maybe it could solve your problem.这是一个了解 2 张图片之间差异的示例。也许它可以解决您的问题。

from PIL import Image
from PIL import ImageChops
img1 = Image.open(yourPath)
img2 = Image.open(yourPath)
# make sure img1,img2 have the same picture width and height.
diff = ImageChops.difference(img1, img2)
diff.show()

In your circumstance,this may works.在您的情况下,这可能有效。

from PIL import ImageChops
While True:
    time.sleep(1)

.......

    diff = ImageChops.difference(Now_Image,Last_Image)
    If diff.getbbox() is None:
        print("Now_Image and Last_Image are same.")    
    # diff.show()
    # or you can handle the diff picture.

The diff Image will show the difference between those picture. diff Image 将显示这些图片之间的差异。 The difference between two pictures will show you and the same component will be black.两张图片之间的差异将向您展示,相同的组件将是黑色的。

This will give you the difference of two images but you certainly want to add some tolerance, maybe through a statistical measure.这会给你两个图像的差异,但你当然想增加一些容差,也许通过统计测量。 I am not sure you want to work with live images though, maybe separate your concerns by capturing images and then running a script a second later independently.不过,我不确定您是否想使用实时图像,也许可以通过捕获图像然后稍后独立运行脚本来分离您的关注点。 Depends on your application purpose though.但这取决于您的应用目的。

from PIL import Image
import numpy as np

image1Url = "./img1.jpg"
image2Url = "./img2.jpg"
image1  = Image.open(image1Url)
image2  = Image.open(image2Url)

analyze_img1 = np.asarray(image1)
analyze_img2 = np.asarray(image2)

substr_img  = analyze_img1 - analyze_img2

last_image  = Image.fromarray(substr_img)

last_image.show()

Your question somewhat implies you are at the initial stages of this project and things are a bit vague, so I have just put together a few general ideas till you are further down the road.你的问题在某种程度上暗示你处于这个项目的初始阶段,事情有点模糊,所以我只是把一些一般性的想法放在一起,直到你走得更远。

  1. Consider moving to OpenCV as it has an abundance of useful functions and there are plenty of examples on Stack Overflow.考虑迁移到OpenCV,因为它具有大量有用的功能,并且在 Stack Overflow 上有很多示例。 Look for cv2.VideoCapture() to grab your camera stream and cv2.imshow() and cv2.waitKey() to display your frames.查找cv2.VideoCapture()以获取您的相机流,并使用cv2.imshow()cv2.waitKey()来显示您的帧。 PIL is not very good for displaying dynamically changing images such as video and you will be much better served by cv2.imshow() . PIL 不太适合显示动态变化的图像,例如视频, cv2.imshow()会为您提供更好的服务。 Also, you will find yourself converting PIL images to Numpy arrays to do processing, so you might as well use OpenCV and work directly with Numpy arrays already.此外,您会发现自己将 PIL 图像转换为 Numpy 数组进行处理,因此您不妨使用 OpenCV 并直接使用 Numpy 数组。

  2. If you want to detect changes in lighting, try converting your image to greyscale cv2.cvtColor(...BGR2GRAY...) and taking the mean with np.mean() .如果您想检测光照变化,请尝试将您的图像转换为灰度cv2.cvtColor(...BGR2GRAY...)并使用np.mean()取平均值。 Try running your loop, which is looking about right, and just printing the mean in the Terminal at each iteration as you turn the lights and off.尝试运行您的循环,这看起来是正确的,并在每次迭代时在终端中打印平均值,同时打开和关闭灯。 Then you can see how much it changes and work out a sensible threshold.然后你可以看到它有多少变化并计算出一个合理的阈值。

  3. As regards movement, you are probably not looking for movement at the pixel level, else you will detect the wind moving a leaf visible out of your window.至于运动,您可能不是在像素级别寻找运动,否则您会检测到风在移动窗外可见的叶子。 So, you want large-scale changes.所以,你想要大规模的改变。 So, if your camera is 1080p (1920x1080), try resizing your image down to 16 pixels by 9 with cv2.resize(im, (16,9),... INTER_LINEAR ...) then you just have 144 pixels to quickly look at and compare between images.因此,如果您的相机是 1080p (1920x1080),请尝试使用cv2.resize(im, (16,9),... INTER_LINEAR ...)将您的图像大小调整为 16 x 9,那么您只有 144 像素可以快速查看并比较图像。 You can always scale these images back up to a side length of 300-400 cv2.resize(...NEAREST_NEIGHBOUR..) to display previous and current frames.您始终可以将这些图像缩放到 300-400 的边长cv2.resize(...NEAREST_NEIGHBOUR..)以显示前一帧和当前帧。 Then look at the colour distances between each of the 144 image rectangles and see if any of them exceed a threshold you derive from your testing.然后查看 144 个图像矩形中的每一个之间的颜色距离,看看它们中的任何一个是否超过了您从测试中得出的阈值。

Try to develop your code a bit and get some experience, then come back and ask another question if you get stuck - questions, and answers, are free.尝试开发您的代码并获得一些经验,然后如果遇到问题,请回来再问一个问题 - 问题和答案都是免费的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM