python - 使用opencv用另一个填充空白图像

标签 python opencv

我正在使用带有 cv2 库的 python。我有一个小图像,我想填充周围的一些空白区域。假设空白图像如下:(每个 x 表示一个像素,[255,255,255])

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

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

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]

最佳答案

使用 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)

关于python - 使用opencv用另一个填充空白图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26012392/

相关文章:

python - MySQL sql 作为列

python - 如何最有效地可视化一维卷积?

linux - cv2 imdecode不返回任何base64字符串

python - 如何从图像中裁剪出最大的矩形

android - 倾斜检测Opencv Android OCR

python - pandas 计算每个类别的计数和总和组

python - 经过训练的 TensorFlow 模型始终输出零

c++ - c++ 中是否有使用 opencv 和 travis ci 的示例项目?

python - OpenCV 断言失败错误:(-215) scn == 3 ||函数 cv::cvtColor 中的 scn == 4 交替工作

python - dplyr R 排列 pandas 中的等效函数