| |
|
|
| import cv2 |
| import os |
| import numpy as np |
| from skimage import measure |
| from matplotlib import pyplot as plt |
|
|
| def uniformsample(pgtnp_px2, newpnum): |
| |
| pnum, cnum = pgtnp_px2.shape |
| assert cnum == 2 |
|
|
| idxnext_p = (np.arange(pnum, dtype=np.int32) + 1) % pnum |
| pgtnext_px2 = pgtnp_px2[idxnext_p] |
| edgelen_p = np.sqrt(np.sum((pgtnext_px2 - pgtnp_px2) ** 2, axis=1)) |
| edgeidxsort_p = np.argsort(edgelen_p) |
| |
|
|
| |
| |
| |
| if pnum > newpnum: |
| edgeidxkeep_k = edgeidxsort_p[pnum - newpnum:] |
| edgeidxsort_k = np.sort(edgeidxkeep_k) |
| pgtnp_kx2 = pgtnp_px2[edgeidxsort_k] |
| assert pgtnp_kx2.shape[0] == newpnum |
| return pgtnp_kx2 |
|
|
| else: |
| edgenum = np.round(edgelen_p * newpnum / np.sum(edgelen_p)).astype(np.int32) |
| for i in range(pnum): |
| if edgenum[i] == 0: |
| edgenum[i] = 1 |
|
|
| |
| edgenumsum = np.sum(edgenum) |
| if edgenumsum != newpnum: |
|
|
| if edgenumsum > newpnum: |
|
|
| id = -1 |
| passnum = edgenumsum - newpnum |
| while passnum > 0: |
| edgeid = edgeidxsort_p[id] |
| if edgenum[edgeid] > passnum: |
| edgenum[edgeid] -= passnum |
| passnum -= passnum |
| else: |
| passnum -= edgenum[edgeid] - 1 |
| edgenum[edgeid] -= edgenum[edgeid] - 1 |
| id -= 1 |
| else: |
| id = -1 |
| edgeid = edgeidxsort_p[id] |
| edgenum[edgeid] += newpnum - edgenumsum |
|
|
| assert np.sum(edgenum) == newpnum |
|
|
| psample = [] |
| for i in range(pnum): |
| pb_1x2 = pgtnp_px2[i:i + 1] |
| pe_1x2 = pgtnext_px2[i:i + 1] |
|
|
| pnewnum = edgenum[i] |
| wnp_kx1 = np.arange(edgenum[i], dtype=np.float32).reshape(-1, 1) / edgenum[i] |
|
|
| pmids = pb_1x2 * (1 - wnp_kx1) + pe_1x2 * wnp_kx1 |
| psample.append(pmids) |
|
|
| psamplenp = np.concatenate(psample, axis=0) |
| return psamplenp |
|
|
| |
| def close_contour(contour): |
| if not np.array_equal(contour[0], contour[-1]): |
| contour = np.vstack((contour, contour[0])) |
| return contour |
|
|
|
|
| |
| def binary_mask_to_polygon(root, tolerance=0): |
| |
| mask = cv2.imread(root,0) |
| mask = np.array(mask) |
| mask = mask/128 |
| binary_mask = mask.astype(int) |
| polygons = [] |
| |
| padded_binary_mask = np.pad(binary_mask, pad_width=1, mode='constant', constant_values=0) |
| contours = measure.find_contours(padded_binary_mask, 0.5) |
|
|
| import matplotlib.pyplot as plt |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| contours = [np.subtract(contour, 1) for contour in contours] |
| for contour in contours: |
| contour = close_contour(contour) |
| contour = measure.approximate_polygon(contour, tolerance) |
| if len(contour) < 3: |
| continue |
| contour = np.flip(contour, axis=1) |
| segmentation = contour.ravel().tolist() |
| |
| segmentation = [0 if i < 0 else i for i in segmentation] |
| polygons.append(segmentation) |
| polys=[] |
| |
| for j in range(len(polygons)): |
| poly=[] |
| for i in range(int(len(polygons[j])/2)): |
| poly.append([polygons[j][2*i],polygons[j][2*i+1]]) |
| polys.append([np.array(poly)]) |
| |
| return polys |
|
|
|
|
|
|
|
|
| if __name__ == '__main__': |
| np.set_printoptions(threshold=np.inf) |
| |
| root = 'E:\PyCharm-ZRC\PyCharm-Project\DeepSnake_remove_c\multiple_segmentdata\images600_2/1_mask_2.jpg' |
| poly = binary_mask_to_polygon(root) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|