2014-09-24 52 views
1

我正在使用python与cv2库。我有一个小图像,我想填充一些空白区域。说空白图像是如下:(每x表示的像素,[255255255])用opencv填充另一个空白图像

x x x x
x x x x
x x x x
x x x x

我想从另一个图像交换它的某些部分的数据(每一个是指从另一图像的像素)

x x x x
x a a x
x a a x
x x x x

什么是最快的方法呢?我试图循环遍历每个像素并完成这项工作,但它似乎非常低效。

import cv2 
import numpy as np 
tmp=np.zeros((1024,768,3),np.uint_8) 
image= .. #image captured from camera 
for(i in range(480)): 
    for(j in range(640)): 
    tmp[i+144][j+197]=image[i][j] 
+0

看看感兴趣的区域opencv doc – biquette 2014-09-24 09:03:12

+0

哪些是感兴趣的区域? – 2014-09-24 09:14:04

回答

2

使用numpy切片

tmp = np.zeros((1024,768,3),np.uint_8) 

img[y1:y2, x1:x2] = tmp[y3:y4, x3:x4] # given that (y2-y1) = (y4 - y3) and same for x 

或填充一些颜色

img[y1:y2, x1:x2] = (255, 255, 255) 
+0

谢谢!这解决了问题:) – 2014-09-24 10:02:35

0

总之,你可以做这样的:

Size taille = new_image.size(); // New_image is the image to be inserted 
Mat white_roi(white_image, Rect(x, y, taille.width, taille.height)); 
            // white_roi is a subimage of white_image 
            //   that start from the point (x,y) 
            // and have the same dimension as a new_image 
new_image.copyTo(white_roi); // You copy the new_image into the white_roi, 
            //   this in the original image 

这是C++代码,你必须适应蟒蛇。

+0

谢谢,但垫类不存在蟒蛇,所以它有点难以迁移此代码 – 2014-09-24 09:51:16

0

更改图像的阵列的第一和然后做替换。

试试这个:

import cv2 
import numpy as np 
tmp=np.zeros((1024,768,3),np.uint_8) 
image= .. #image captured from camera 
src = np.array(image) 

tmp[144:, 197:] = src 

请元素的数量肯定是正确的首次。

+0

嗯,似乎给出错误: 'A = np.zeros((5,5,3),np.uint8 ) b = np.zeros((3,3,3-),np.uint8) 一个[:,:] = [255,255,255] 一个[1:,1:] = b #it说:ValueError异常:操作数可能不能与形状一起播出(4,4,3)(3,3,3)' – 2014-09-24 09:45:08

0

如果你想更换一些大的零部件如矩形,然后用投资回报率来代替(如其他答案已经说明)。但是如果你正在处理随机分布的像素或复杂的形状,那么你可以试试这个。

1)创建的掩模的二值图像,MaskImage,对于部分为真其余部分被设置为假更换。

2)Result = MasterImage(MaskImage)) + PickerImageMaskImage

PS:我没有用过蟒蛇,因为我不知道Python的,它很容易表达好运