|
5 | 5 | import cv2 |
6 | 6 | import numpy as np |
7 | 7 | from skimage import measure |
8 | | -import matplotlib.pyplot as plt |
9 | 8 | from wired_table_rec.utils import OrtInferSession, resize_img |
10 | 9 | from wired_table_rec.utils_table_line_rec import ( |
11 | 10 | get_table_line, |
@@ -144,94 +143,6 @@ def postprocess(self, img, pred, **kwargs): |
144 | 143 | rotated_polygons = polygons.copy() |
145 | 144 | return polygons, rotated_polygons |
146 | 145 |
|
147 | | - def find_max_corners(self, line_img): |
148 | | - # 找到所有轮廓 |
149 | | - contours, _ = cv2.findContours( |
150 | | - line_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE |
151 | | - ) |
152 | | - |
153 | | - # 如果没有找到轮廓,返回空列表 |
154 | | - if not contours: |
155 | | - return [] |
156 | | - |
157 | | - # 找到面积最大的轮廓 |
158 | | - max_contour = max(contours, key=cv2.contourArea) |
159 | | - # 计算最大轮廓的最小外接矩形 |
160 | | - rect = cv2.minAreaRect(max_contour) |
161 | | - |
162 | | - # 获取最小外接矩形的四个角点 |
163 | | - box = cv2.boxPoints(rect) |
164 | | - box = np.int0(box) |
165 | | - # |
166 | | - # 对角点进行排序 |
167 | | - # 计算中心点 |
168 | | - center = np.mean(box, axis=0) |
169 | | - |
170 | | - # 计算每个点与中心点的角度 |
171 | | - angles = np.arctan2(box[:, 1] - center[1], box[:, 0] - center[0]) |
172 | | - |
173 | | - # 按角度排序 |
174 | | - sorted_indices = np.argsort(angles) |
175 | | - sorted_box = box[sorted_indices] |
176 | | - |
177 | | - # 确保顺序为左上、右上、右下、左下 |
178 | | - top_left = sorted_box[0] |
179 | | - top_right = sorted_box[1] |
180 | | - bottom_right = sorted_box[2] |
181 | | - bottom_left = sorted_box[3] |
182 | | - |
183 | | - # 创建一个纯黑色背景图像 |
184 | | - black_img = np.zeros_like(line_img) |
185 | | - |
186 | | - # 可视化最大轮廓和四个角点 |
187 | | - plt.figure(figsize=(10, 10)) |
188 | | - plt.imshow(black_img, cmap="gray") |
189 | | - plt.title("Max Contour and Corners on Black Background") |
190 | | - |
191 | | - # 绘制最大轮廓 |
192 | | - max_contour = max_contour.reshape(-1, 2) |
193 | | - plt.plot(max_contour[:, 0], max_contour[:, 1], "b-", linewidth=2) |
194 | | - |
195 | | - # 绘制四个角点 |
196 | | - plt.scatter( |
197 | | - [top_left[0], top_right[0], bottom_right[0], bottom_left[0]], |
198 | | - [top_left[1], top_right[1], bottom_right[1], bottom_left[1]], |
199 | | - c="g", |
200 | | - s=100, |
201 | | - marker="o", |
202 | | - ) |
203 | | - |
204 | | - plt.axis("off") |
205 | | - plt.show() |
206 | | - |
207 | | - return [top_left, top_right, bottom_right, bottom_left] |
208 | | - |
209 | | - def extend_image_and_adjust_coordinates(self, img, corners, polygons): |
210 | | - # 计算扩展边界 |
211 | | - min_x = min(point[0] for point in corners) |
212 | | - min_y = min(point[1] for point in corners) |
213 | | - max_x = max(point[0] for point in corners) |
214 | | - max_y = max(point[1] for point in corners) |
215 | | - |
216 | | - # 计算扩展的宽度和高度 |
217 | | - left = -min_x if min_x < 0 else 0 |
218 | | - top = -min_y if min_y < 0 else 0 |
219 | | - right = max_x - img.shape[1] if max_x > img.shape[1] else 0 |
220 | | - bottom = max_y - img.shape[0] if max_y > img.shape[0] else 0 |
221 | | - |
222 | | - # 扩展图像 |
223 | | - new_width = img.shape[1] + left + right |
224 | | - new_height = img.shape[0] + top + bottom |
225 | | - extended_img = np.zeros((new_height, new_width), dtype=img.dtype) |
226 | | - extended_img[top : top + img.shape[0], left : left + img.shape[1]] = img |
227 | | - |
228 | | - # 调整角点和多边形坐标 |
229 | | - adjusted_corners = [(point[0] + left, point[1] + top) for point in corners] |
230 | | - adjusted_polygons = polygons.copy() |
231 | | - adjusted_polygons[:, 0::2] += left |
232 | | - adjusted_polygons[:, 1::2] += top |
233 | | - return extended_img, adjusted_corners, adjusted_polygons |
234 | | - |
235 | 146 | def cal_region_boxes(self, tmp): |
236 | 147 | labels = measure.label(tmp < 255, connectivity=2) # 8连通区域标记 |
237 | 148 | regions = measure.regionprops(labels) |
|
0 commit comments