小编Mr *_*agi的帖子

在 OpenCV 中选择图像的非矩形 ROI 的最有效方法是什么?

我想创建一个二进制图像掩码,在 python 中只包含 1 和 0。感兴趣区域(白色)是非矩形的,由 4 个角点定义,例如如下所示: 在此处输入图片说明

在我的方法中,我首先计算上下 ROI 边界的线方程,然后检查每个掩码元素,如果它比边界小或大。代码正在运行,但速度太慢了。一个 2000x1000 的面具需要 4 秒的时间来处理我的机器。

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

def line_eq(line):
    """input:
            2 points of a line
       returns: 
            slope and intersection of the line
    """
    (x1, y1), (x2, y2) = line
    slope = (y2 - y1) / float((x2 - x1))
    intersect = int(slope * (-x1) + y1)

    return slope,intersect

def maskByROI(mask,ROI):
    """
        input: 
            ROI: with 4 corner points e.g. ((x0,y0),(x1,y1),(x2,y2),(x3,y3)) …
Run Code Online (Sandbox Code Playgroud)

python opencv numpy

7
推荐指数
1
解决办法
5356
查看次数

标签 统计

numpy ×1

opencv ×1

python ×1