gpt4 book ai didi

python - OpenCV Python将图像的某些部分复制到另一部分

转载 作者:行者123 更新时间:2023-12-02 17:44:12 25 4
gpt4 key购买 nike

我想计算两个图像之间的差异。我只对图像的特定部分的差异值感兴趣。为此,我将图像的所需部分复制到临时图像,并对这些图像进行操作。但是,使用http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html上指定的像素分配。在这里给出
ball = img[280:340, 330:390]
img[273:333, 100:160] = ball

使用类似的逻辑,我编写了一个python程序,

import cv2
import numpy as np

img_file = 'carparking2_1.jpg'
img = cv2.imread(img_file, cv2.IMREAD_COLOR)
img_withoutcar = 'carparking2_1.jpg'
img_withcar = 'carparking2.jpg'

img1 = img_withoutcar[47:151, 106:157]
img2 = img_withcar[47:151, 106:157]

diff1 = cv2.absdiff(img1, img2)
diff2 = cv2.absdiff(img1, img1)

print 'RGB shape: ', img.shape # Rows, cols, channels
print 'Difference with car and without: ', diff1
print 'Difference with car and with car: ', diff2

但是,即时通讯获取输出消息:
File "D:/Projects/IoT Smart Parking/differenceinframes.py", line 8, in <module>
img1 = img_withoutcar[47:151, 106:157]
TypeError: string indices must be integers, not tuple

我在Windows 10上运行带有OpenCV 3.1.0的Python 2.7。

最佳答案

您收到错误消息是因为您的命令试图将字符串'carparking2_1.jpg'像图像数据一样进行 slice 。

#First assign the file names:
file_name_without_car='carparking2_1.jpg'
file_name_with_car='carparking2.jpg'

#load the images
img_withoutcar= cv2.imread(file_name_without_car, cv2.IMREAD_COLOR)
img_withcar= cv2.imread(file_name_with_car, cv2.IMREAD_COLOR)

#now you can slice regions of the images
#note that color images have a third dimension for color channel.
img1 = img_withoutcar[47:151, 106:157,:]
img2 = img_withcar[47:151, 106:157,:]

关于python - OpenCV Python将图像的某些部分复制到另一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38750688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com