File size: 4,038 Bytes
a637d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.

import numpy as np
import cv2
import base64


def cv2_base64(image):
    base64_str = cv2.imencode('.png',image)[1].tobytes()
    base64_str = base64.b64encode(base64_str)
    return base64_str.decode('utf-8')

def base64_cv2(base64_str):
    imgString = base64.b64decode(base64_str)
    nparr = np.frombuffer(imgString,np.uint8)
    image = cv2.imdecode(nparr,cv2.IMREAD_COLOR)
    return image



def image_pose_mask(imagepath : str):
    img = base64_cv2(imagepath)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnt = contours[-1]
    hull = cv2.convexHull(cnt)
    length = len(hull)
    if length > 4:
        for i in range(length):
            cv2.line(img, tuple(hull[i][0]), tuple(hull[(i + 1) % length][0]), (255, 255, 255), 2)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    _, threshold = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
    mask = img.copy()
    kernel = np.ones((30, 30), dtype=np.uint8)
    dilated = cv2.dilate(threshold, kernel, 20)
    dilated = cv2.dilate(dilated, kernel, 20)
    contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    valid = len(contours) > 0

    area = []

    for k in range(len(contours)):
        area.append(cv2.contourArea(contours[k]))
    max_idx = np.argmax(np.array(area))

    mask2 = cv2.drawContours(mask, contours, max_idx, (255, 255, 255), thickness=-1)
    img_base64 = cv2_base64(mask2)

    return img_base64



def image_pose_mask_numpy(image):
    img = np.uint8(image)
    print("shape:" + str(img.shape))

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnt = contours[-1]
    hull = cv2.convexHull(cnt)
    length = len(hull)
    if length > 4:
        for i in range(length):
            cv2.line(img, tuple(hull[i][0]), tuple(hull[(i + 1) % length][0]), (255, 255, 255), 2)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    _, threshold = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
    mask = img.copy()
    kernel = np.ones((30, 30), dtype=np.uint8)

    dilated = cv2.dilate(threshold, kernel, 20)

    dilated = cv2.dilate(dilated, kernel, 20)
    contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    valid = len(contours) > 0

    area = []

    for k in range(len(contours)):
        area.append(cv2.contourArea(contours[k]))
    max_idx = np.argmax(np.array(area))

    mask2 = cv2.drawContours(mask, contours, max_idx, (255, 255, 255), thickness=-1)

    img_base64 = cv2_base64(mask2)

    return img_base64

def image_canny(imagepath:str):

    img = base64_cv2(imagepath)

    image_map  = cv2.Canny(img, 100, 200)

    contours, hierarchy = cv2.findContours(image_map, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnt = contours[-1]
    hull = cv2.convexHull(cnt)
    length = len(hull)
    if length > 4:
        for i in range(length):
            cv2.line(img, tuple(hull[i][0]), tuple(hull[(i + 1) % length][0]), (0, 0, 255), 2)

    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    valid = len(contours) > 0
    area = []
    for k in range(len(contours)):
        area.append(cv2.contourArea(contours[k]))
    max_idx = np.argmax(np.array(area))

    mask2 = cv2.drawContours(image_map, contours, max_idx, (0, 0, 255), thickness=3)

    img_base64 = cv2_base64(mask2)

    return img_base64

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    image_canny(imagepath='download (13).png')