Python3+OpenCV3+Pycharm编程:提取ROI区域与泛洪填充

提取ROI区域

def roi_demo(image):
    """
    ROI(region of interest)
    感兴趣区域,从被处理的图像以方框、圆、椭圆、
    不规则多边形等方式勾勒出需要处理的区域
    """
    face = src[200:400, 200:400]  # 获取ROI区域:高度,宽度
    gray_face = cv.cvtColor(face, cv.COLOR_BGR2GRAY)  # ROI区域转化单通道GRAY图像
    back_face = cv.cvtColor(gray_face, cv.COLOR_GRAY2BGR)  # 单通道GRAY图像转化三通道RGB图像(三通道值相同)
    # get_image_info(back_face)
    src[200:400, 200:400] = back_face
    cv.imshow("roi demo", back_face)

Python3+OpenCV3+Pycharm编程:提取ROI区域与泛洪填充_第1张图片

Python3+OpenCV3+Pycharm编程:提取ROI区域与泛洪填充_第2张图片

  泛洪填充

def fill_color_demo(image):
    """泛洪填充"""
    copy_img = image.copy()
    h, w = image.shape[:2]
    mask = np.zeros([h+2, w+2], np.uint8)  # 单通道8位
    cv.floodFill(copy_img, mask, (200, 250), (0, 255, 255), (100, 100, 100), (50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
    # 开始填充位置:(200, 250),填充的值:(0, 255, 255),(100, 100, 100):取(200, 250)像素值-(...)=要填充最低像素,
    # (50, 50, 50):取(200, 250)像素值+(...)=要填充最高像素,彩色图像填充:cv.FLOODFILL_FIXED_RANGE
    cv.imshow("color demo", copy_img)

区域泛洪填充

def fill_binary_demo():
    """二值范围泛洪填充"""
    img = np.zeros([400, 400, 3], np.uint8)
    img[100:300, 100:300, :] = 255
    cv.imshow("binary img", img)

    mask = np.ones([402, 402, 1], np.uint8)  # mask(遮罩)层单通道8位值为1
    mask[101:301, 101:301, :] = 0  # mask层填充区域要为0
    cv.floodFill(img, mask, (200, 200), (0, 0, 255), cv.FLOODFILL_MASK_ONLY)
    # 在mask区域内填充:FLOODFILL_MASK_ONLY
    cv.imshow("binary demo", img)

Python3+OpenCV3+Pycharm编程:提取ROI区域与泛洪填充_第3张图片

代码

# -*- coding: utf-8 -*-
# By:iloveluoluo
# 2019.3.24
import cv2 as cv
import numpy as np


def get_image_info(image):
    """属性读取"""
    # print(image.shape)  # (高,宽,通道数)
    height = image.shape[0]
    width = image.shape[1]
    channels = image.shape[2]  # Blue, Green, Red
    print("height:%s, width:%s, channels:%s, pixels:%s" % (height, width, channels, image.size))
    img_date = np.array(image)
    print(img_date)  # 输出数组信息


def roi_demo(image):
    """
    ROI(region of interest)
    感兴趣区域,从被处理的图像以方框、圆、椭圆、
    不规则多边形等方式勾勒出需要处理的区域
    """
    face = src[200:400, 200:400]  # 获取ROI区域:高度,宽度
    gray_face = cv.cvtColor(face, cv.COLOR_BGR2GRAY)  # ROI区域转化单通道GRAY图像
    back_face = cv.cvtColor(gray_face, cv.COLOR_GRAY2BGR)  # 单通道GRAY图像转化三通道RGB图像(三通道值相同)
    # get_image_info(back_face)
    src[200:400, 200:400] = back_face
    cv.imshow("roi demo", back_face)


def fill_color_demo(image):
    """泛洪填充"""
    copy_img = image.copy()
    h, w = image.shape[:2]
    mask = np.zeros([h+2, w+2], np.uint8)  # 单通道8位
    cv.floodFill(copy_img, mask, (200, 250), (0, 255, 255), (100, 100, 100), (50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
    # 开始填充位置:(200, 250),填充的值:(0, 255, 255),(100, 100, 100):取(200, 250)像素值-(...)=要填充最低像素,
    # (50, 50, 50):取(200, 250)像素值+(...)=要填充最高像素,彩色图像填充:cv.FLOODFILL_FIXED_RANGE
    cv.imshow("color demo", copy_img)


def fill_binary_demo():
    """二值范围泛洪填充"""
    img = np.zeros([400, 400, 3], np.uint8)
    img[100:300, 100:300, :] = 255
    cv.imshow("binary img", img)

    mask = np.ones([402, 402, 1], np.uint8)  # mask(遮罩)层单通道8位值为1
    mask[101:301, 101:301, :] = 0  # mask层填充区域要为0
    cv.floodFill(img, mask, (200, 200), (0, 0, 255), cv.FLOODFILL_MASK_ONLY)
    # 在mask区域内填充:FLOODFILL_MASK_ONLY
    cv.imshow("binary demo", img)


src = cv.imread('E:/MyFile/Picture/date/lenademo.png')  # 读取图片

# roi_demo(src)
# fill_color_demo(src)
# cv.imshow("demo", src)
fill_binary_demo()

cv.waitKey(0)
cv.destroyAllWindows()

 

你可能感兴趣的:(Python+OpenCV)