File size: 8,324 Bytes
63f3cf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File   pram -> vis_seg
@IDE    PyCharm
@Author fx221@cam.ac.uk
@Date   07/02/2024 11:06
=================================================='''
import cv2
import numpy as np
from copy import deepcopy


def myHash(text: str):
    hash = 0
    for ch in text:
        hash = (hash * 7879 ^ ord(ch) * 5737) & 0xFFFFFFFF
    return hash


def generate_color_dic(n_seg=1000):
    out = {}
    for i in range(n_seg + 1):
        sid = i
        if sid == 0:
            color = (0, 0, 255)  # [b, g, r]
        else:
            # rgb_new = hash(str(sid * 319993))
            rgb_new = myHash(str(sid * 319993))
            r = (rgb_new & 0xFF0000) >> 16
            g = (rgb_new & 0x00FF00) >> 8
            b = rgb_new & 0x0000FF
            color = (b, g, r)
        out[i] = color
    return out


def vis_seg_point(img, kpts, segs=None, seg_color=None, radius=7, thickness=-1):
    outimg = deepcopy(img)
    for i in range(kpts.shape[0]):
        # print(kpts[i])
        if segs is not None and seg_color is not None:
            color = seg_color[segs[i]]
        else:
            color = (0, 255, 0)
        outimg = cv2.circle(outimg,
                            center=(int(kpts[i, 0]), int(kpts[i, 1])),
                            color=color,
                            radius=radius,
                            thickness=thickness, )

    return outimg


def vis_corr_incorr_point(img, kpts, pred_segs, gt_segs, radius=7, thickness=-1):
    outimg = deepcopy(img)
    for i in range(kpts.shape[0]):
        # print(kpts[i])
        p_seg = pred_segs[i]
        g_seg = gt_segs[i]
        if p_seg == g_seg:
            if g_seg != 0:
                color = (0, 255, 0)
            else:
                color = (255, 0, 0)
        else:
            color = (0, 0, 255)
        outimg = cv2.circle(outimg,
                            center=(int(kpts[i, 0]), int(kpts[i, 1])),
                            color=color,
                            radius=radius,
                            thickness=thickness, )
    return outimg


def vis_inlier(img, kpts, inliers, radius=7, thickness=1, with_outlier=True):
    outimg = deepcopy(img)
    for i in range(kpts.shape[0]):
        if not with_outlier:
            if not inliers[i]:
                continue
        if inliers[i]:
            color = (0, 255, 0)
        else:
            color = (0, 0, 255)
        outimg = cv2.rectangle(outimg,
                               pt1=(int(kpts[i, 0] - radius), int(kpts[i, 1] - radius)),
                               pt2=(int(kpts[i, 0] + radius), int(kpts[i, 1] + radius)),
                               color=color,
                               thickness=thickness, )

    return outimg


def vis_global_seg(cls, seg_color, radius=7, thickness=-1):
    all_patches = []
    for i in range(cls.shape[0]):
        if cls[i] == 0:
            continue
        color = seg_color[i]
        patch = np.zeros(shape=(radius, radius, 3), dtype=np.uint8)
        patch[..., 0] = color[0]
        patch[..., 1] = color[1]
        patch[..., 2] = color[2]

        all_patches.append(patch)
    if len(all_patches) == 0:
        color = seg_color[0]
        patch = np.zeros(shape=(radius, radius, 3), dtype=np.uint8)
        patch[..., 0] = color[0]
        patch[..., 1] = color[1]
        patch[..., 2] = color[2]
        all_patches.append(patch)
    return np.vstack(all_patches)


def plot_matches(img1, img2, pts1, pts2, inliers, radius=3, line_thickness=2, horizon=True, plot_outlier=False,
                 confs=None):
    rows1 = img1.shape[0]
    cols1 = img1.shape[1]
    rows2 = img2.shape[0]
    cols2 = img2.shape[1]
    # r = 3
    if horizon:
        img_out = np.zeros((max([rows1, rows2]), cols1 + cols2, 3), dtype='uint8')
        # Place the first image to the left
        img_out[:rows1, :cols1] = img1
        # Place the next image to the right of it
        img_out[:rows2, cols1:] = img2  # np.dstack([img2, img2, img2])
        for idx in range(inliers.shape[0]):
            # if idx % 10 > 0:
            #     continue
            if inliers[idx]:
                color = (0, 255, 0)
            else:
                if not plot_outlier:
                    continue
                color = (0, 0, 255)
            pt1 = pts1[idx]
            pt2 = pts2[idx]

            if confs is not None:
                nr = int(radius * confs[idx])
            else:
                nr = radius
            img_out = cv2.circle(img_out, (int(pt1[0]), int(pt1[1])), nr, color, 2)

            img_out = cv2.circle(img_out, (int(pt2[0]) + cols1, int(pt2[1])), nr, color, 2)

            img_out = cv2.line(img_out, (int(pt1[0]), int(pt1[1])), (int(pt2[0]) + cols1, int(pt2[1])), color,
                               line_thickness)
    else:
        img_out = np.zeros((rows1 + rows2, max([cols1, cols2]), 3), dtype='uint8')
        # Place the first image to the left
        img_out[:rows1, :cols1] = img1
        # Place the next image to the right of it
        img_out[rows1:, :cols2] = img2  # np.dstack([img2, img2, img2])

        for idx in range(inliers.shape[0]):
            # print("idx: ", inliers[idx])
            # if idx % 10 > 0:
            #     continue
            if inliers[idx]:
                color = (0, 255, 0)
            else:
                if not plot_outlier:
                    continue
                color = (0, 0, 255)

            if confs is not None:
                nr = int(radius * confs[idx])
            else:
                nr = radius

            pt1 = pts1[idx]
            pt2 = pts2[idx]
            img_out = cv2.circle(img_out, (int(pt1[0]), int(pt1[1])), nr, color, 2)

            img_out = cv2.circle(img_out, (int(pt2[0]), int(pt2[1]) + rows1), nr, color, 2)

            img_out = cv2.line(img_out, (int(pt1[0]), int(pt1[1])), (int(pt2[0]), int(pt2[1]) + rows1), color,
                               line_thickness)

    return img_out


def plot_kpts(img, kpts, radius=None, colors=None, r=3, color=(0, 0, 255), nh=-1, nw=-1, shape='o', show_text=None,
              thickness=5):
    img_out = deepcopy(img)
    for i in range(kpts.shape[0]):
        pt = kpts[i]
        if radius is not None:
            if shape == 'o':
                img_out = cv2.circle(img_out, center=(int(pt[0]), int(pt[1])), radius=radius[i],
                                     color=color if colors is None else colors[i],
                                     thickness=thickness)
            elif shape == '+':
                img_out = cv2.line(img_out, pt1=(int(pt[0] - radius[i]), int(pt[1])),
                                   pt2=(int(pt[0] + radius[i]), int(pt[1])),
                                   color=color if colors is None else colors[i],
                                   thickness=5)
                img_out = cv2.line(img_out, pt1=(int(pt[0]), int(pt[1] - radius[i])),
                                   pt2=(int(pt[0]), int(pt[1] + radius[i])), color=color,
                                   thickness=thickness)
        else:
            if shape == 'o':
                img_out = cv2.circle(img_out, center=(int(pt[0]), int(pt[1])), radius=r,
                                     color=color if colors is None else colors[i],
                                     thickness=thickness)
            elif shape == '+':
                img_out = cv2.line(img_out, pt1=(int(pt[0] - r), int(pt[1])),
                                   pt2=(int(pt[0] + r), int(pt[1])), color=color if colors is None else colors[i],
                                   thickness=thickness)
                img_out = cv2.line(img_out, pt1=(int(pt[0]), int(pt[1] - r)),
                                   pt2=(int(pt[0]), int(pt[1] + r)), color=color if colors is None else colors[i],
                                   thickness=thickness)

    if show_text is not None:
        img_out = cv2.putText(img_out, show_text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
                              (0, 0, 255), 3)
    if nh == -1 and nw == -1:
        return img_out
    if nh > 0:
        return cv2.resize(img_out, dsize=(int(img.shape[1] / img.shape[0] * nh), nh))
    if nw > 0:
        return cv2.resize(img_out, dsize=(nw, int(img.shape[0] / img.shape[1] * nw)))