内容纲要
def transform_projection(image_size, start_crop_pt, crop_size, person_scale): """ Given the orthogonal projection of an image, when we resize and crop the image we calculate the transformations from this projection to new projection :param image_size: tuple or list, [height, width] :param crop_size: tuple or list, [height, width] :param start_crop_pt: the left and top point, from which start to crop :param person_scale: the height of person in crop_img / the height of person in ori_image :return: transformation 4 * 4 """ crop_h, crop_w = crop_size img_h, img_w = image_size m1 = np.array([ [2 / crop_w, 0, 0, -1], [0, 2 / crop_h, 0, -1], [0, 0, 1, 0], [0, 0, 0, 1] ], np.float32) m2 = np.array([ [person_scale, 0, 0, - start_crop_pt[0]], [0, person_scale, 0, - start_crop_pt[1]], [0, 0, 1, 0], [0, 0, 0, 1] ], np.float32) m3 = np.array([ [img_w / 2, 0, 0, img_w / 2], [0, img_h / 2, 0, img_h / 2], [0, 0, 1, 0], [0, 0, 0, 1] ], np.float32) return np.matmul(np.matmul(m1, m2), m3)
具体推导关系关系就不细说了。
矩阵m3的作用是将归一化的顶点坐标(范围为-1到1)变成(x,y),其中x范围(0,w)y范围(0,h)。
矩阵m2的作用是图片当图片进行resize时,比如person在原图的高度为120 在crop里的高度为150,那么person相当于进行了缩放,缩放比例为150/120。crop的起始坐标就是其实坐标。
矩阵m1其实是m3的逆矩阵,只不过是w,h是crop的宽和高而已。
通过上述转换矩阵,就可以通过trans右乘投影矩阵来得到变换后的投影矩阵
注意:
1、目前仅仅测试于正交投影,因为在变换的时候,无法对z轴进行变换,也就是说深度没有变换。
2、一般来讲,无论是原图的w,h还是crop的w, h,最好相近。
3、投影得到的坐标都在-1,1 范围内
留言
请问 如果是透视投影关系如何推导呢?图片进行裁剪缩放时,透视投影的投影矩阵变换推导