编程语言
首页 > 编程语言> > python OpenCV学习笔记(七):ROI与泛洪填充

python OpenCV学习笔记(七):ROI与泛洪填充

作者:互联网

1.ROI

什么是ROI?
即为你感兴趣的区域

import cv2 as cv
import numpy as np

src = cv.imread("D:/......")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
face = src[50:250, 100:300] #截取图像中指定长宽范围的图像
cv.imshow("face", face)
gray = cv.cvtColor(face, cv_COLOR_BGR2GRAY)#转换为灰度图像
cv.imshow("gray", gray)
backface = cv.cvtColor(gray, cv_COLOR_GRAY2BGR)#转换为彩色图像
src[50:250, 100:300] = backface
cv.imshow("src", src)

cv.waitKey(0)

cv.destroyALLWindows()

2.彩色泛洪填充

import cv2 as cv
import numpy as np

def fill_color_demo(image):
	copyImg = image.copy()
	h, w = image.shape[:2]
	mask = np.zeros(h+2, w+2,np.uint8) 
	cv.floodFill(copyImg, mask, (30, 30),(0,255,255), (100, 100, 100),(50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
	cv.imshow("fill_color_demo",image)
fill_color_demo(src)

3.二值泛洪填充

import cv2 as cv
import numpy as np

def fill_binary(image):
	image = np.zeros([400,400,3],np.uint8)#长400,宽400,三通道
	image[100:300, 100:300, : ] = 255
	cv.imshow("fill_binary",image)
	mask = np.ones([402, 402, 1], np.uint8)
	mask[101:301, 101:301] = 0
	cv.floodFill(image, mask, (200,200),(0,0,255),cv.FLOODFILL_MASK_ONLY)
	cv.imshow("filled binary", image)
fill_binary(image)

标签:ROI,src,python,image,imshow,np,泛洪,100,cv
来源: https://blog.csdn.net/qq_39624528/article/details/88078945