-
Notifications
You must be signed in to change notification settings - Fork 0
/
crop.py
81 lines (66 loc) · 1.95 KB
/
crop.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
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 3 13:59:28 2020
@author: HP
"""
import numpy as np
from numba import jit
import cv2
def find_region(img0):
img = img0.copy()
r, g, b = cv2.split(img)
ret,thresh_r = cv2.threshold(r,254,255,cv2.THRESH_BINARY)
ret,thresh_g = cv2.threshold(g,0,255,cv2.THRESH_BINARY_INV)
ret,thresh_b = cv2.threshold(b,0,255,cv2.THRESH_BINARY_INV)
temp = cv2.bitwise_and(thresh_r,thresh_g)#,mask = thresh_g)
img = cv2.bitwise_and(temp, thresh_b)
return calculate_region(img)
@jit
def calculate_region(img):
row = img.shape[0]
column = img.shape[1]
left, top = 0, 0
right, bottom = row-1, column-1
#找top
for i in range(row):
count = 0
for j in range(column):
if img[i,j] == 255:
count += 1
if count == 4:
top = i
break
#找bottom
for i in range(row):
count = 0
for j in range(column):
if img[row-1-i, j] == 255:
count += 1
if count == 4:
bottom = row-i
break
#找left
for i in range(column):
count = 0
for j in range(row):
if img[j, i] == 255:
count += 1
if count == 4:
left = i
break
#找right
for i in range(column):
count = 0
for j in range(row):
if img[j, column-1-i] == 255:
count += 1
if count == 4:
right = column-i
break
right = bottom - top + left
print(left, top, right, bottom)
return left, top, right, bottom
if __name__ == '__main__':
screenshot = cv2.imread('F:/Desktop2020.1.17/AutoCut/screenshot2.bmp')
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_BGR2RGB)
left, top, right, bottom = find_region(screenshot)