【輔助駕駛】透視變換、仿射變換(包含鳥瞰圖、俯檢視、正檢視)[2]——俯檢視
阿新 • • 發佈:2018-12-24
一、效果
二、程式碼實現
1、python程式碼,基於opencv庫和imutils庫
來源:https://blog.csdn.net/qq_34199383/article/details/79571318
from imutils import perspective from skimage.filters import threshold_local import cv2 import imutils # 邊緣掃描 image = cv2.imread("./picture/5.png") ratio = image.shape[0] / 500.0 # 比例 orig = image.copy() image = imutils.resize(image, height = 500) # 灰度轉換及邊緣查詢 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(gray, 75, 200) # 邊緣檢測 # 只保留輪廓 cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)#通過邊緣影象找到輪廓 cnts = cnts[0] if imutils.is_cv2() else cnts[1] # 用以區分OpenCV2.4和OpenCV3 cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5] # 保留最大輪廓 for c in cnts: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * peri, True) # 輪廓點 if len(approx) == 4: # 表明找到四個輪廓點 screenCnt = approx break # 轉為鳥瞰圖 warped = perspective.four_point_transform(orig, screenCnt.reshape(4, 2) * ratio) warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) # 灰度轉換 T = threshold_local(warped, 11, offset = 10, method = "gaussian") warped = (warped > T).astype("uint8") * 255 cv2.imshow("Original", imutils.resize(orig, height = 650)) cv2.imshow("Scanned", imutils.resize(warped, height = 650)) cv2.waitKey(0)
2、C++實現
基於opencv的getPerspectiveTransform()函式和warpPerspective()函式
程式碼:https://blog.csdn.net/t6_17/article/details/78729097
效果:
3、Image perspective transformation and text recognition
功能:
- Image cropping automation
- Bird's eye view transformation
- Optical character recognition