rust_face.md 3.4 KB
Newer Older
F
feilong 已提交
1 2 3 4
# 还原挤地铁的参赛作品

下图左边的参赛作品(图片来自网络)掉到地上,重新做太麻烦了,据说改了个《挤地铁》的名字获奖了。

F
feilong 已提交
5
![](https://gitcode.net/csdn/skill_tree_git_md_linux/-/raw/master/data/1.OpenCV初阶/5.几何变换和图像特征/2.单应性变换/rust_face_origin.jpeg)
F
feilong 已提交
6
<br/>
F
feilong 已提交
7 8 9 10 11

我们可以通过 OpenCV 单应性变换的方式帮助挤地铁的人脸还原。

把没挤扁和挤扁的图的坐标标注出来

F
feilong 已提交
12
![](https://gitcode.net/csdn/skill_tree_git_md_linux/-/raw/master/data/1.OpenCV初阶/5.几何变换和图像特征/2.单应性变换/rust_face_rule.jpg)
F
feilong 已提交
13
<br/>
F
feilong 已提交
14 15 16

可以看到还原后的侧脸

F
feilong 已提交
17
![](https://gitcode.net/csdn/skill_tree_git_md_linux/-/raw/master/data/1.OpenCV初阶/5.几何变换和图像特征/2.单应性变换/rust_face_homography.jpeg)
F
feilong 已提交
18
<br/>
F
feilong 已提交
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

以下正确的实现是

## 答案

```python
import cv2
import numpy as np

if __name__ == '__main__':
    img_src = cv2.imread('rust_face_src.jpeg')
    pts_src = np.array([[0, 0], [570, 0], [570, 1078], [0, 1078]])

    img_dst = cv2.imread('rust_face_dest.jpeg')
    pts_dst = np.array([[63, 285], [378, 224], [427, 689], [84, 820]])

    dw, dh = img_dst.shape[1], img_dst.shape[0]

    h, status = cv2.findHomography(pts_src, pts_dst)
    img_out = cv2.warpPerspective(img_src, h, (dw, dh))

    images = np.concatenate((img_src[0: dh, 0:dw, 0:3], img_dst, img_out), axis=1)
    cv2.imshow('homography', images)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
```

## 选项

### 映射位置不匹配

```python
import cv2
import numpy as np

if __name__ == '__main__':
    img_src = cv2.imread('rust_face_src.jpeg')
    pts_src = np.array([[0, 0], [0, 1078], [570, 0], [570, 1078]])

    img_dst = cv2.imread('rust_face_dest.jpeg')
    pts_dst = np.array([[63, 285], [378, 224], [427, 689], [84, 820]])

    dw, dh = img_dst.shape[1], img_dst.shape[0]

    h, status = cv2.findHomography(pts_src, pts_dst)
    img_out = cv2.warpPerspective(img_src, h, (dw, dh))

    images = np.concatenate((img_src[0: dh, 0:dw, 0:3], img_dst, img_out), axis=1)
    cv2.imshow('homography', images)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
```

### 源图和目标图片错位

```python
import cv2
import numpy as np

if __name__ == '__main__':
    img_src = cv2.imread('rust_face_src.jpeg')
    pts_src = np.array([[0, 0], [0, 1078], [570, 0], [570, 1078]])

    img_dst = cv2.imread('rust_face_dest.jpeg')
    pts_dst = np.array([[63, 285], [378, 224], [427, 689], [84, 820]])

    dw, dh = img_dst.shape[1], img_dst.shape[0]

    h, status = cv2.findHomography(pts_dst, pts_src)
    img_out = cv2.warpPerspective(img_src, h, (dw, dh))

    images = np.concatenate((img_src[0: dh, 0:dw, 0:3], img_dst, img_out), axis=1)
    cv2.imshow('homography', images)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
```

### 目标长宽用错

```python
import cv2
import numpy as np

if __name__ == '__main__':
    img_src = cv2.imread('rust_face_src.jpeg')
    pts_src = np.array([[0, 0], [0, 1078], [570, 0], [570, 1078]])

    img_dst = cv2.imread('rust_face_dest.jpeg')
    pts_dst = np.array([[63, 285], [378, 224], [427, 689], [84, 820]])

    dw, dh = img_dst.shape[1], img_dst.shape[0]

    h, status = cv2.findHomography(pts_dst, pts_src)
    img_out = cv2.warpPerspective(img_src, h, (dh, dw))

    images = np.concatenate((img_src[0: dh, 0:dw, 0:3], img_dst, img_out), axis=1)
    cv2.imshow('homography', images)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
```