---
# System prepended metadata

title: OpenCV 基礎篇-惡搞Lena part1
tags: [OpenCV, 影像相加]

---

###### tags: `OpenCV`,`影像相加`
# OpenCV 基礎篇-惡搞Lena part1

**numpy.random.randint**

random.randint(low, high=None, size=None, dtype=int)
Return random integers from low (inclusive) to high (exclusive).

```python=
#Generate a 2 x 4 array of ints between 0 and 4, inclusive:
a=np.random.randint(5, size=(2, 4))
print(a)
#[[0 4 1 0]
# [3 1 3 4]]
```


```python=
#使用亂數產生3D模擬陣列 #array只能裝數字
import cv2
import numpy as np
a=cv2.imread("lenacolor.png",cv2.IMREAD_UNCHANGED)
cv2.imshow("original",a)
face=np.random.randint(0,256,(180,100,3))#產生一個(0-255)亂碼矩陣
#180(220~400區間),100(250~350區間),3個色彩矩陣
a[220:400,250:350]=face #把亂碼丟到lena的臉,對Lena的臉打馬賽克
cv2.imshow("mask_lena",a)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
![](https://i.imgur.com/4NMS1uk.png)


```python=
import cv2
lena=cv2.imread("lena512.bmp",cv2.IMREAD_UNCHANGED)
dollar=cv2.imread("dollar.bmp",cv2.IMREAD_UNCHANGED)
cv2.imshow("lena",lena)
cv2.imshow("dollar",dollar)
face=lena[220:400,250:350]#長:180,寬:100
dollar[160:340,200:300]=face #dollar位置要自己抓一下
#lena的影像矩陣貼到美元影像,長寬都要一致
cv2.imshow("result",dollar)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
![](https://i.imgur.com/XUTy6xi.png)







