-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxArea.py
99 lines (82 loc) · 2.72 KB
/
maxArea.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from datetime import datetime
import cv2
import numpy as np
def maximalRectangle(matrix, mask=255):
"""二值矩阵获取最大矩形"""
n, m = matrix.shape
left = np.zeros([n, m])
for i in range(n):
for j in range(m):
if matrix[i, j] == mask:
if j == 0:
left[i, j] = 1
else:
left[i, j] = left[i, j - 1] + 1
ret = 0
ans_x = 0
ans_y = 0
ans_w = 0
ans_h = 0
for i in range(m):
tmp_ret, y, w, h = largestRectangleArea(left[:, i])
if tmp_ret > ret:
ret = tmp_ret
ans_x = i - w + 1
ans_y = y
ans_w = w
ans_h = h
return ans_x, ans_y, ans_w, ans_h
def largestRectangleArea(heights):
"""直方图获取最大矩形"""
n = len(heights)
left, right = [0] * n, [n] * n
mono_stack = list()
for i in range(n):
while mono_stack and heights[mono_stack[-1]] >= heights[i]:
right[mono_stack[-1]] = i
mono_stack.pop()
left[i] = mono_stack[-1] if mono_stack else -1
mono_stack.append(i)
ans = 0
y = 0
w = 0
h = 0
for i in range(n):
tmp = (right[i] - left[i] - 1) * heights[i]
if tmp > ans:
ans = tmp
y = left[i] + 1
w = int(heights[i])
h = right[i] - left[i] - 1
return ans, y, w, h
def get_binary_pic(img, threshold=0, mask=255):
"""获取二值图像"""
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, threshold, mask, cv2.THRESH_BINARY)
return thresh
def cut_picture(img, threshold=0):
"""返回最大有效内接矩形的图片"""
print("cut_picture start...", datetime.now())
ans_x, ans_y, ans_w, ans_h = maximalRectangle(get_binary_pic(img,threshold))
print("cut_picture end...", datetime.now())
return img[ans_y:ans_y + ans_h, ans_x:ans_x + ans_w, :]
def execute(img):
lower = np.array([0, 50, 100], dtype="uint8")
upper = np.array([25, 255, 255], dtype="uint8")
hsvImage = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
skinMask = cv2.inRange(hsvImage, lower, upper)
skin = cv2.bitwise_and(img, img, mask=skinMask)
new_img = cut_picture(skin)
return new_img
if __name__ == '__main__':
lower = np.array([0, 50, 100], dtype="uint8")
upper = np.array([25, 255, 255], dtype="uint8")
img = cv2.imread('./data/zxyROI/ROI1.1.jpg')
hsvImage = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
skinMask = cv2.inRange(hsvImage, lower, upper)
skin = cv2.bitwise_and(img,img,mask=skinMask)
new_img = cut_picture(skin)
cv2.imshow('skin',skin)
cv2.imshow('cut',new_img)
cv2.waitKey(0)
cv2.destroyAllWindows()