|
import cv2 |
|
import numpy as np |
|
import MBD_utils |
|
import torch |
|
import torch.nn.functional as F |
|
|
|
|
|
def mask_base_dewarper(image,mask): |
|
''' |
|
input: |
|
image -> ndarray HxWx3 uint8 |
|
mask -> ndarray HxW uint8 |
|
return |
|
dewarped -> ndarray HxWx3 uint8 |
|
grid (optional) -> ndarray HxWx2 -1~1 |
|
''' |
|
|
|
|
|
|
|
contours,hierarchy = cv2.findContours(mask,cv2.RETR_EXTERNAL,method=cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
|
|
four_corners, maxArea, contour= MBD_utils.DP_algorithm(contours) |
|
four_corners = MBD_utils.reorder(four_corners) |
|
|
|
|
|
new_mask = np.zeros_like(mask) |
|
new_mask = cv2.drawContours(new_mask,[contour],-1,255,cv2.FILLED) |
|
|
|
|
|
|
|
ratios = [0.25,0.5,0.75] |
|
|
|
middle = MBD_utils.findMiddle(corners=four_corners,mask=new_mask,points=ratios) |
|
|
|
|
|
source_points = np.concatenate((four_corners,middle),axis=0) |
|
|
|
|
|
h,w = image.shape[:2] |
|
padding = 0 |
|
target_points = [[padding, padding],[w-padding, padding], [padding, h-padding],[w-padding, h-padding]] |
|
for ratio in ratios: |
|
target_points.append([int((w-2*padding)*ratio)+padding,padding]) |
|
for ratio in ratios: |
|
target_points.append([int((w-2*padding)*ratio)+padding,h-padding]) |
|
for ratio in ratios: |
|
target_points.append([padding,int((h-2*padding)*ratio)+padding]) |
|
for ratio in ratios: |
|
target_points.append([w-padding,int((h-2*padding)*ratio)+padding]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
source_points = source_points.reshape(-1,2)/np.array([image.shape[:2][::-1]]).reshape(1,2) |
|
source_points = torch.from_numpy(source_points).float().cuda() |
|
source_points = source_points.unsqueeze(0) |
|
source_points = (source_points-0.5)*2 |
|
target_points = np.asarray(target_points).reshape(-1,2)/np.array([image.shape[:2][::-1]]).reshape(1,2) |
|
target_points = torch.from_numpy(target_points).float() |
|
target_points = (target_points-0.5)*2 |
|
|
|
model = MBD_utils.TPSGridGen(target_height=256,target_width=256,target_control_points=target_points) |
|
model = model.cuda() |
|
grid = model(source_points).view(-1,256,256,2).permute(0,3,1,2) |
|
grid = F.interpolate(grid,(h,w),mode='bilinear').permute(0,2,3,1) |
|
dewarped = MBD_utils.torch2cvimg(F.grid_sample(MBD_utils.cvimg2torch(image).cuda(),grid))[0] |
|
return dewarped,grid[0].cpu().numpy() |
|
|
|
def mask_base_cropper(image,mask): |
|
''' |
|
input: |
|
image -> ndarray HxWx3 uint8 |
|
mask -> ndarray HxW uint8 |
|
return |
|
dewarped -> ndarray HxWx3 uint8 |
|
grid (optional) -> ndarray HxWx2 -1~1 |
|
''' |
|
|
|
|
|
_, contours, hierarchy = cv2.findContours(mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) |
|
|
|
|
|
|
|
four_corners, maxArea, contour= MBD_utils.DP_algorithm(contours) |
|
four_corners = MBD_utils.reorder(four_corners) |
|
|
|
|
|
new_mask = np.zeros_like(mask) |
|
new_mask = cv2.drawContours(new_mask,[contour],-1,255,cv2.FILLED) |
|
|
|
|
|
rect = cv2.minAreaRect(contour) |
|
box = cv2.boxPoints(rect) |
|
box = np.int0(box) |
|
box = box.reshape((4,1,2)) |
|
|
|
|
|
|
|
|