python 图像变化检测_python hough变换检测直线的实现方法
1 原理
2 檢測步驟
將參數空間(ρ,θ) 量化成m*n(m為ρ的等份數,n為θ的等份數)個單元,并設置累加器矩陣,初始值為0;
對圖像邊界上的每一個點(x,y)帶入ρ=xcosθ+ysinθ,求得每個θ對應的ρ值,并在ρ和θ所對應的單元,將累加器加1,即:Q(i,j)=Q(i,j)+1;
檢驗參數空間中每個累加器的值,累加器最大的單元所對應的ρ和θ即為直角坐標系中直線方程的參數。
3 接口
image:二值圖像,canny邊緣檢測輸出。這里是result。
rho: 以像素為單位的距離精度,這里為1像素。如果想要檢測的線段更多,可以設為0.1。
theta: 以弧度為單位的角度精度,這里為numpy.pi/180。如果想要檢測的線段更多,可以設為0.01 * numpy.pi/180。
threshod: 閾值參數,int類型,超過設定閾值才被檢測出線段,這里為10。
minLineLength:線段以像素為單位的最小長度。
maxLineGap:同一方向上兩條線段判定為一條線段的最大允許間隔。
4 代碼及結果
import os
import numpy as np
import cv2
from PIL import Image, ImageEnhance
import math
def img_processing(img):
# 灰度化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
# canny邊緣檢測
edges = cv2.Canny(binary, 50, 150, apertureSize=3)
return edges
def line_detect(img):
img = Image.open(img)
img = ImageEnhance.Contrast(img).enhance(3)
# img.show()
img = np.array(img)
result = img_processing(img)
# 霍夫線檢測
lines = cv2.HoughLinesP(result, 1, 1 * np.pi/180, 10, minLineLength=10, maxLineGap=5)
# print(lines)
print("Line Num : ", len(lines))
# 畫出檢測的線段
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 0), 1)
pass
img = Image.fromarray(img, 'RGB')
img.show()
if __name__ == "__main__":
line_detect("1.jpg")
pass
原圖如下:
檢測結果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
總結
以上是生活随笔為你收集整理的python 图像变化检测_python hough变换检测直线的实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。