-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathROI.py
More file actions
61 lines (54 loc) · 1.79 KB
/
ROI.py
File metadata and controls
61 lines (54 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
图像处理:ROI分割
"""
import cv2
import numpy as np
class ROI:
"从图像中截取感兴趣区域,使用归一化坐标和长宽进行定义,得到具体坐标和长宽"
def __init__(
self,
image: np.ndarray, # 图像
x1: float, # 归一化坐上坐标
y1: float,
width: float, # 长宽
height: float,
ifExist=True,
): # 是否暂存图像数据
"ROI类的实例化,使用归一化的左上点横纵坐标和归一化的长宽"
imageHeight = image.shape[0]
imageWidth = image.shape[1]
self.width = int(width * imageWidth)
self.height = int(height * imageHeight)
self.x1 = int(x1 * imageWidth)
self.y1 = int(y1 * imageHeight)
if ifExist:
self.ROI = image[
self.y1 : (self.y1 + self.height),
self.x1 : (self.x1 + self.width),
].copy()
def draw(self, image: np.ndarray, color=(180, 0, 0), width=3):
"将ROI区域在图片上绘制出来"
cv2.rectangle(
image,
(self.x1, self.y1),
(self.x1 + self.width, self.y1 + self.height),
color,
width,
)
def isInside(self, point):
"判断point点是否位于ROI区域内"
x, y = point
if (x < self.x1 or x > self.x1 + self.width) or (
y < self.y1 or y > self.y1 + self.height
):
return False
else:
return True
def getRatio(self, *, isNone=False):
"返回ROI内某一白色或者黑色像素的占比,isNone是否取反"
num = cv2.countNonZero(self.ROI)
ratio = num / (self.height * self.width)
if isNone:
return 1 - ratio
else:
return ratio