import cv2
import numpy as np
import matplotlib.pyplot as plt
def show_Image_Plt(Image, title=None, cmap=None):
plt.imshow(Image, cmap=cmap)
plt.title(title)
plt.axis(False)
plt.show()
def show_Image_Cv(Image, Name="Demo"):
cv2.namedWindow(Name,cv2.WINDOW_NORMAL)
cv2.imshow(Name, Image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def ReadImage(Path):
Image=cv2.imread(Path)
Image_Size=cv2.resize(Image,None,fx=0.6,fy=0.6)
Image_Gray=cv2.cvtColor(Image_Size,cv2.COLOR_BGR2GRAY)
return Image_Gray
def Sift(Image):
Sift = cv2.SIFT_create()
keypoint, describe = Sift.detectAndCompute(Image, None)
keypoint_xy = np.float32([i.pt for i in keypoint])
Image_draw = cv2.drawKeypoints(Image, keypoint, None, None)
return keypoint,describe,keypoint_xy
def Mosaic(Image_First, Image_Last,Col):
KeyPoint_F,Describe_F,KeyPoint_xy_F=Sift(Image_First)
KeyPoint_L,Describe_L,KeyPoint_xy_L=Sift(Image_Last)
matcher = cv2.BFMatcher()
rawMatcher = matcher.knnMatch(Describe_L, Describe_F, k=2)
Match = []
for n, m in rawMatcher:
if n.distance < m.distance * 0.75:
Match.append([n.trainIdx, n.queryIdx])
PtsA = np.float32([KeyPoint_xy_F[i] for (i, _) in Match])
PtsB = np.float32([KeyPoint_xy_L[i] for (_, i) in Match])
Mat, _ = cv2.findHomography(PtsB, PtsA, cv2.RANSAC, 4)
Image_Affine = cv2.warpPerspective(Image_Last, Mat, [384, Col+500])
Image_Affine[0:Image_First.shape[0], 0:Image_First.shape[1]] = Image_First
return Image_Affine
def Image_Resize(Image_Demo):
Row=int((np.nonzero(Image_Demo)[0].shape[0])/384)
Image=Image_Demo[0:Row,0:384]
return Image
Image_1=ReadImage("pcb_01.png")
Image_2=ReadImage("pcb_02.png")
Image_3=ReadImage("pcb_03.png")
Image_4=ReadImage("pcb_04.png")
Image_5=ReadImage("pcb_05.png")
Image_6=ReadImage("pcb_06.png")
Image_1_Col=Image_1.shape[0]
Image_12_Demo = Mosaic(Image_1, Image_2,Col=Image_1_Col)
show_Image_Cv(Image_12_Demo)
Image_12=Image_Resize(Image_12_Demo)
show_Image_Cv(Image_12_Demo)
Image_12_Col=Image_12.shape[0]
Image_123_Demo = Mosaic(Image_12, Image_3,Col=Image_12_Col)
show_Image_Cv(Image_123_Demo)
Image_123=Image_Resize(Image_123_Demo)
show_Image_Cv(Image_123)
Image_123_Col=Image_123.shape[0]
Image_1234_Demo = Mosaic(Image_123, Image_4,Col=Image_123_Col)
show_Image_Cv(Image_1234_Demo)
Image_1234=Image_Resize(Image_1234_Demo)
show_Image_Cv(Image_1234)
Image_12334_Col=Image_1234.shape[0]
Image_12345_Demo = Mosaic(Image_1234, Image_5,Col=Image_12334_Col)
show_Image_Cv(Image_12345_Demo)
Image_12345=Image_Resize(Image_12345_Demo)
show_Image_Cv(Image_12345)
Image_123345_Col=Image_12345.shape[0]
Image_123456_Demo = Mosaic(Image_12345, Image_6,Col=Image_123345_Col)
show_Image_Cv(Image_123456_Demo)
Image_123456=Image_Resize(Image_123456_Demo)
show_Image_Cv(Image_123456)