1.ROI感兴趣区域的操作
寻找感兴趣的区域主要就是利用矩阵的切片功能来提取.
如face = image[100:200, 300:400]
import cv2 as cv
image = cv.imread('./data/lena.jpg', 1)
cv.imshow('source image', image)
# 提取感兴趣区域
face = image[250:400, 200:350]
# 将感兴趣区域转换成灰度图
gray = cv.cvtColor(face, cv.COLOR_BGR2GRAY)
# 将灰度图转换成BGR格式为了后边赋值操作保持通道一致
# 这里灰度转换并不会变成彩色一定要注意
backface = cv.cvtColor(gray, cv.COLOR_GRAY2BGR)
# 重新赋值
image[250:400, 200:350] = backface
cv.imshow('face_part', image)
cv.waitKey(0)
cv.destroyAllWindows()
2.泛洪填充
泛洪填充,如何填充一个对象内部区域
- FLOODFILL_FIXED_RANGE- 改变图像,泛洪填充
- FLOODFILL_MASK_ONLY - 不改变图像,只填充遮罩层本身,忽略新的颜色值参数
- floodFill(Mat image, Mat mask, Point seedPoint, Scalar newVal)
- floodFill(image, mask, seedPoint, newVal, rect, loDiff, upDiff, flags)
src(x,y)=[src(seed.x, seed,y)-loDiff, src(seed.x, seed,y)+upDiff]
import cv2 as cv
import numpy as np
# 彩色图像的填充
def fill_color_demo(image):
copy_image = image.copy()
h, w = image.shape[:2]
mask = np.zeros([h+2, w+2], np.uint8)
cv.floodFill(copy_image, mask, (30, 30), (0, 255, 255),
(100, 100, 100), (50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
cv.imshow("flood image demo", copy_image)
# 灰度图填充
# 灰度图填充
def fill_binary():
img = np.zeros([400, 400, 3], np.uint8)
img[100:300, 100:300, :] = 255
cv.imshow("fill_binary", img)
mask = np.ones([402, 402, 1], np.uint8)
mask[101:301, 101:301] = 0
cv.floodFill(img, mask, (200, 200),(0, 0, 255), cv.FLOODFILL_MASK_ONLY)
cv.imshow("filled binary", img)
image = cv.imread('./data/lena.jpg', 1)
cv.imshow('source image', image)
fill_color_demo(image)
fill_binary()
cv.waitKey(0)
cv.destroyAllWindows()
后续:
递归算法和扫描非递归算法,后者更快
泛洪填充中的图像与掩码做与操作