# Python學習筆記 0005 (合併視窗)
## 合併(hstack、vstack)
```javascript=
import cv2
import numpy as np
frameWidth = 400
frameHeight = 400
path = "img12.jpg"
img = cv2.imread(path,0 )
img = cv2.resize(img, (frameWidth, frameHeight))
imgCanny = cv2.Canny(img, 100, 100 )
print(img.shape)
print(imgCanny.shape)
img1 = cv2.resize(img, (0, 0), None, 0.5, 0.5)
img2 = cv2.resize(imgCanny, (0, 0), None, 0.5, 0.5)
hor = np.hstack((img1, img2))
ver = np.vstack((img1, img2))
cv2.imshow("hor", hor)
cv2.imshow("ver", ver)
cv2.waitKey(0)
```

- img2 = cv2.resize(imgCanny, (0, 0), None, 0.5, 0.5)
0.5, 0.5是長度x0.5和寬度x0.5
- print out
陣列相同 才能做合併
(400, 400)
(400, 400)

- print out 陣列不同 合併會出現錯誤(有解)
(400, 400, 3)
(400, 400)
## 陣列不同的合併
```javascript=
import cv2
import numpy as np
frameWidth = 400
frameHeight = 400
path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
imgCanny = cv2.Canny(img, 100, 100 )
print("____調整前_____")
print(img.shape)
print(imgCanny.shape)
img1 = cv2.resize(img, (0, 0), None, 0.5, 0.5)
img2 = cv2.resize(imgCanny, (0, 0), None, 0.5,0.5)
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
print("____img調整後、合併前_____")
print(img1.shape)
print(img2.shape)
hor = np.hstack((img1, img2))
ver = np.vstack((img1, img2))
print("____合併後_____")
print(hor.shape)
print(ver.shape)
cv2.imshow("hor", hor)
cv2.imshow("ver", ver)
cv2.waitKey(0)
```
- print out shape
____調整前_____
(400, 400, 3)
(400, 400)
____img調整後、合併前_____
(200, 200)
(200, 200)
____合併後_____
(200, 400)
(400, 200)
- img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
可以得知 img在調整(resize)前是三個陣列
## 大合併(stack)
```javascript=
import cv2
import numpy as np
frameWidth = 200
frameHeight = 200
kernel = np.ones((5,5),np.uint8)
path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
Gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Blur = cv2.GaussianBlur(Gray, (5, 5), 0)
Canny = cv2.Canny(img, 100, 100 )
Dilation = cv2.dilate(Canny,kernel,iterations = 1)
Erode =cv2.erode(Dilation,kernel,iterations = 1)
print("img shape " + str(img.shape))
print("Gray shape " + str(Gray.shape))
print("Blur shape " + str(Blur.shape))
print("Canny shape " + str(Canny.shape))
print("Dilation shape " + str(Dilation.shape))
print("Erode shape " + str(Erode.shape))
cv2.waitKey(0)
```

- 大合併之前必須觀察他們的陣列是否一致
img shape (200, 200, 3)
Gray shape (200, 200)
Blur shape (200, 200)
Canny shape (200, 200)
Dilation shape (200, 200)
Erode shape (200, 200)
## stackImages 能調整陣列
```javascript=
import cv2
import numpy as np
frameWidth = 200
frameHeight = 200
kernel = np.ones((5,5),np.uint8)
def stackImages(scale,imgArray):.....
path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
Gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Blur = cv2.GaussianBlur(Gray, (5, 5), 0)
Canny = cv2.Canny(img, 100, 100 )
Dilation = cv2.dilate(Canny,kernel,iterations = 1)
Erode =cv2.erode(Dilation,kernel,iterations = 1)
StackedImages = stackImages(0.8, ([img, Gray, Blur],[Canny, Dilation, Erode]))
cv2.imshow("Stack", StackedImages)
cv2.waitKey(0)
```
```javascript=
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range ( 0, rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
ver = hor
return ver
```

合併成果
### 大合併-影片版
```javascript=
import cv2
import numpy as np
frameWidth = 400
frameHeight = 400
kernel = np.ones((5,5),np.uint8)
cap = cv2.VideoCapture("2.mp4")
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range ( 0, rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
ver = hor
return ver
while True:
sucess, img = cap.read()
img = cv2.resize(img, (frameWidth, frameHeight))
Gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Blur = cv2.GaussianBlur(Gray, (5, 5), 0)
Canny = cv2.Canny(img, 100, 100)
Dilation = cv2.dilate(Canny, kernel, iterations=1)
Erode = cv2.erode(Dilation, kernel, iterations=1)
StackedImages = stackImages(0.8, ([img, Gray, Blur], [Canny, Dilation, Erode]))
cv2.imshow("Stack", StackedImages)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
```

:::spoiler 一篇一個OpenCV函式(Blur)
- cv2.blur
原型:blur(src,ksize,dst=None,anchor=None,borderType=None)
作用:對影象進行算術平均值模糊
引數:ksize,卷積核的大小。dst,若填入dst,則將影象寫入到dst矩陣。
- cv2.medianBlur
原型:mediaBlur(src,ksize,dst=None)
作用:對影象進行中值模糊
- cv2.GaussianBlur
原型:GaussianBlur(src,ksize,sigmaX,dst=None,sigmaY=None,borderType=None)
作用:對影象進行高斯模糊
引數:sigmaX,X方向上的方差,一般設為0讓系統自動計算。
https://www.itread01.com/content/1550310670.html
:::
:::spoiler 一篇一個numpy函式(multiply、dot、*)
np.multiply()、np.dot()和星號(*)三種乘法運算的區別
- 數組和矩陣對應位置相乘,輸出與相乘數組/矩陣的大小一致
```javascript=
import numpy as np
A = np.arange(0,4).reshape(2,2)
B = np.arange(11,15).reshape(2,2)
C = np.multiply(A,B)
print('A')
print(A)
print('B')
print(B)
print('C')
print(C)
```
A
[[0 1]
[2 3]]
B
[[11 12]
[13 14]]
C
[[11 24]
[39 56]]
- 矩陣對應元素位置相乘,且用np.mat()將數組變成陣列
```javascript=
import numpy as np
A = np.arange(0,4).reshape(2,2)
B = np.arange(11,15).reshape(2,2)
C = np.multiply(np.mat(A),np.mat(B))
D = np.sum(np.multiply(np.mat(A),np.mat(B)))
print('A')
print(A)
print('B')
print(B)
print('C')
print(C)
print('D')
print(D)
```
- np.sum()是將各個得值相加
A
[[0 1]
[2 3]]
B
[[11 12]
[13 14]]
C
[[ 0 12]
[26 42]]
D
80
- np.dot() 秩不為1
對於秩為1的數組,執行對應位置相乘,然後再相加;
對於秩不為1的二維數組,執行矩陣乘法運算
```javascript=
A = np.arange(0,4).reshape(2,2)
B = np.arange(11,15).reshape(2,2)
C = np.multiply(A,B)
D = np.dot(A,B)
print('A')
print(A)
print('B')
print(B)
print('C')
print(C)
print('D')
print(D)
```
A
[[0 1]
[2 3]]
B
[[11 12]
[13 14]]
C
[[ 0 12]
[26 42]]
D
[[13 14]
[61 66]]
- np.dot() 秩為1
```javascript=
import numpy as np
A = np.arange(0,4)
B = np.arange(11,15)
C = np.multiply(A,B)
D = np.dot(A,B)
print('A')
print(A)
print('B')
print(B)
print('C')
print(C)
print('D')
print(D)
```
A
[0 1 2 3]
B
[11 12 13 14]
C
[ 0 12 26 42]
D
80
```javascript=
import numpy as np
A = np.arange(0,4).reshape(2,2)
B = np.arange(11,15).reshape(2,2)
C = np.multiply(A,B)
D = np.dot(A,B)
E = np.dot(np.mat(A),np.mat(B))//執行矩陣乘法運算
print('A')
print(A)
print('B')
print(B)
print('C')
print(C)
print('D')
print(D)
print('E')
print(E)
```
A
[[0 1]
[2 3]]
B
[[11 12]
[13 14]]
C
[[ 0 12]
[26 42]]
D
[[13 14]
[61 66]]
E
[[13 14]
[61 66]]
- 星號(*)乘法運算
對數組執行對應位置相乘
對矩陣執行矩陣乘法運算
```javascript=
import numpy as np
A = np.arange(0,4).reshape(2,2)
B = np.arange(11,15).reshape(2,2)
C = A*B
D = (np.mat(A))*(np.mat(B))//執行矩陣運算
print('A')
print(A)
print('B')
print(B)
print('C')
print(C)
print('D')
print(D)
```
A
[[0 1]
[2 3]]
B
[[11 12]
[13 14]]
C
[[ 0 12]
[26 42]]
D
[[13 14]
[61 66]]
https://blog.csdn.net/zenghaitao0128/article/details/78715140
:::
###