OpenCV-Day-016:图像ROI和ROI操作

代码

import cv2 as cv
import numpy as np

src = cv.imread("./apple.jpg", cv.IMREAD_COLOR)
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
h, w = src.shape[:2]
print(h, w)
# 获取ROI
cy = h // 2
cx = w // 2
roi = src[cy - 200:cy + 200, cx - 200:cx + 200, :]
cv.imshow("roi", roi)

# image是copy的,改变image,src不会改变
image = np.copy(roi)

# roi是src的一部分,改变roi,src也会改变
roi[:, :, 0] = 0
cv.imshow("change roi will change src", src)

#
image[:, :, 2] = 0
cv.imshow("change image and src's output", src)
cv.imshow("change image", image)

# generate background
result = np.zeros(src.shape, src.dtype)
result[:, :, 0] = 255

# combine background + person
mask = cv.bitwise_not(src)
dst = cv.bitwise_or(src, result)
dst = cv.add(dst, src)

cv.imshow("dst", dst)

cv.waitKey(0)
cv.destroyAllWindows()

效果

在这里插入图片描述

解释

发布了197 篇原创文章 · 获赞 35 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/PoGeN1/article/details/90897146
ROI