OpenCV-Python 选择ROI

OpenCV-Python选择ROI

      • 1. 矩形ROI,直接使用切片,简单粗暴
      • 2. 使用OpenCV中的一些函数获取ROI区域,这个用的最多
      • 3. 使用鼠标获取ROI区域
      • 4. 鼠标获取多边形ROI

1. 矩形ROI,直接使用切片,简单粗暴

import cv2

src = cv2.imread("D:\\python_script\\ffff\\qipan.png")
roi = src[20:150, 50:300 ]

cv2.imshow('roi',roi)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. 使用OpenCV中的一些函数获取ROI区域,这个用的最多

import numpy as np
from matplotlib import pyplot as plt
import cv2

src = cv2.imread("D:\\python_script\\ffff\\qipan.png")

ROI = np.zeros(src.shape, np.uint8) #感兴趣区域ROI
proimage0 = src.copy()               #复制原图

"""提取轮廓"""
proimage1=cv2.cvtColor(proimage0,cv2.COLOR_BGR2GRAY)   #转换成灰度图
proimage2=cv2.adaptiveThreshold(proimage1,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,7,7)
proimage3,contours,hierarchy=cv2.findContours(proimage2,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE) #提取所有的轮廓


"""ROI提取"""
cv2.drawContours(ROI, contours, 1,(255,255,255),-1)       #ROI区域填充白色,轮廓ID1
ROI=cv2.cvtColor(ROI,cv2.COLOR_BGR2GRAY)                  #转换成灰度图
ROI=cv2.adaptiveThreshold(ROI,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,7,7)                                     #自适应阈值化
imgroi= cv2.bitwise_and(ROI,proimage3)                   #图像交运算 ,获取的是原图处理——提取轮廓后的ROI


titles = ['Original Image', 'proimage1','proimage2', 'proimage3']
images = [proimage0, proimage1, proimage2, proimage3]
for i in range(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])
plt.show()


cv2.imshow('roi',roi)
cv2.imshow('imgroi',imgroi)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. 使用鼠标获取ROI区域

OpenCV-python自带 API 可直接选择矩形区域作为ROI,该API位于目标追踪模块,主要是cv2.selectROI()函数

import cv2

img = cv2.imread("D:\\python_script\\ffff\\qipan.png")


roi = cv2.selectROI(windowName="roi", img=img, showCrosshair=True, fromCenter=False)
x, y, w, h = roi

cv2.rectangle(img=img, pt1=(x, y), pt2=(x + w, y + h), color=(0, 0, 255), thickness=2)
cv2.imshow("roi", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

4. 鼠标获取多边形ROI

多边形ROI,主要利用鼠标交互进行绘制:

  1. 单击左键,选择多边形的点;
  2. 单击右键,删除最近一次选择的点;
  3. 单击中键,确定ROI区域并可视化。
  4. 按”S“键,将多边形ROI区域的点保存到本地”config.pkl"文件中

import cv2
import numpy as np
import joblib

pts = []  # 用于存放点


# 统一的:mouse callback function
def draw_roi(event, x, y, flags, param):
    img2 = img.copy()

    if event == cv2.EVENT_LBUTTONDOWN:  # 左键点击,选择点
        pts.append((x, y))
    if event == cv2.EVENT_RBUTTONDOWN:  # 右键点击,取消最近一次选择的点
        pts.pop()
    if event == cv2.EVENT_MBUTTONDOWN:  # 中键绘制轮廓
        mask = np.zeros(img.shape, np.uint8)
        points = np.array(pts, np.int32)
        points = points.reshape((-1, 1, 2))

        # 画多边形
        mask = cv2.polylines(mask, [points], True, (255, 255, 255), 2)
        mask2 = cv2.fillPoly(mask.copy(), [points], (255, 255, 255))  # 用于求 ROI
        mask3 = cv2.fillPoly(mask.copy(), [points], (0, 255, 0))      # 用于 显示在桌面的图像
        show_image = cv2.addWeighted(src1=img, alpha=0.8, src2=mask3, beta=0.2, gamma=0)

        cv2.imshow("mask", mask2)
        cv2.imshow("show_img", show_image)

        ROI = cv2.bitwise_and(mask2, img)
        cv2.imshow("ROI", ROI)
        cv2.waitKey(0)

    if len(pts) > 0:
        # 将pts中的最后一点画出来
        cv2.circle(img2, pts[-1], 3, (0, 0, 255), -1)

    if len(pts) > 1:
        # 画线
        for i in range(len(pts) - 1):
            cv2.circle(img2, pts[i], 5, (0, 0, 255), -1)  # x ,y 为鼠标点击地方的坐标
            cv2.line(img=img2, pt1=pts[i], pt2=pts[i + 1], color=(255, 0, 0), thickness=2)

    cv2.imshow('image', img2)


# 创建图像与窗口并将窗口与回调函数绑定
img = cv2.imread("D:\\python_script\\ffff\\qipan.png")
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_roi)
print("[INFO] 单击左键:选择点,单击右键:删除上一次选择的点,单击中键:确定ROI区域")
print("[INFO] 按‘S’确定选择区域并保存")
print("[INFO] 按 ESC 退出")

while True:
    key = cv2.waitKey(1) & 0xFF
    if key == 27:
        break
    if key == ord("s"):
        saved_data = {"ROI": pts}
        joblib.dump(value=saved_data, filename="config.pkl")
        print("[INFO] ROI坐标已保存到本地.")
        break
cv2.destroyAllWindows()

读取获取的ROI区域

def Load_Model(filepath):
    img = cv2.imread("D:\\python_script\\ffff\\qipan.png")
    model = joblib.load(filepath)
    print(type(model))
    print(model)
    return model

Load_Model('config.pkl')

你可能感兴趣的:(OpenCV,计算机视觉)