程式:
# -*-coding:utf-8 -*-
import cv2 as cv2
import numpy as np
# 標準霍夫變換
img = cv2.imread('new.png')
# img = cv2.imread('new1.png')
# img = cv2.imread('new2.png')
house = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 獲取灰度圖
edges = cv2.Canny(house, 50, 200)
lines = cv2.HoughLines(edges, 1, np.pi/180, 260) # 霍夫變換回傳的就是極坐標系中的兩個引數 rho和theta
print(np.shape(lines))
lines = lines[:, 0, :] # 將資料轉換到二維
for rho, theta in lines:
a = np.cos(theta)
b = np.sin(theta)
# 從圖b中可以看出x0 = rho x cos(theta)
# y0 = rho x sin(theta)
x0 = a*rho
y0 = b*rho
# 由引數空間向實際坐標點轉換
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*a)
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*a)
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 1)
cv2.imshow('img', img)
cv2.imshow('edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
運行結果:

結束!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/299773.html
標籤:其他
上一篇:解決無法在嗶哩嗶哩(b站)上使用HTML5視頻播放器腳本插件/油猴等插件失效的問題
下一篇:使用OpenCV識別二維碼
