您好,登錄后才能下訂單哦!
這篇文章主要講解了“python+opencv怎么實現(xiàn)堆疊圖片”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“python+opencv怎么實現(xiàn)堆疊圖片”吧!
代碼如下:
# import cv2 # import numpy as np # # img = cv2.imread('../images/full.jpg') # # img_hor = np.hstack((img,img)) # img_ver = np.vstack((img,img)) # # cv2.imshow('Horizontal',img_hor) # cv2.imshow('Vertical',img_ver) # # cv2.waitKey(0) import cv2 import numpy as np 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 img = cv2.imread('../images/lanbojini.jpg') imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) imgStack = stackImages(0.5,([img,imgGray,img],[img,img,img])) # imgStack = stackImages(0.5,([img,imgGray,img],[img,img,img])) # imgHor = np.hstack((img,img)) # imgVer = np.vstack((img,img)) # # cv2.imshow("Horizontal",imgHor) # cv2.imshow("Vertical",imgVer) cv2.imshow("ImageStack",imgStack) cv2.waitKey(0)
效果:
感謝各位的閱讀,以上就是“python+opencv怎么實現(xiàn)堆疊圖片”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對python+opencv怎么實現(xiàn)堆疊圖片這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。