使用opencv用另一个填充空白图像 [英] Filling a blank image with another with opencv

查看:104
本文介绍了使用opencv用另一个填充空白图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 python 和 cv2 库.我有一个小图像,我想填充周围的一些空白空间.假设空白图像如下:(每个x代表一个像素,[255,255,255])

I'm using python with cv2 library. I have a small image that I want to fill some blank space around. Say the blank image is as following: (each x means a pixel, [255,255,255])

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

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

我想将其中的某些部分交换为来自另一幅图像的数据(每个 a 表示来自另一幅图像的一个像素)

I want to exchange some parts of it to the data from another image (each a means a pixel from another image)

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

最快的方法是什么?我尝试遍历每个像素并完成这项工作,但似乎效率极低.

What would be the quickest way of doing it? I tried looping through each pixel and do the job, but it seems to be highly inefficient.

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]

推荐答案

使用 numpy 切片.

Use numpy slicing.

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)

这篇关于使用opencv用另一个填充空白图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆