亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用python opencv測量同一圖像中兩條線之間的角度?

如何使用python opencv測量同一圖像中兩條線之間的角度?

料青山看我應如是 2022-03-09 20:01:49
我使用霍夫變換檢測到一條不直的車道邊界線,然后分別提取該線。然后與另一個具有直線的圖像混合?,F在我需要計算這兩條線之間的角度,但我不知道這些線的坐標。所以我嘗試使用給出垂直線坐標的代碼,但它無法具體識別這些坐標。有沒有辦法測量這些線之間的角度?這是我的坐標計算代碼和兩行混合圖像import cv2 as cvimport numpy as npsrc = cv.imread("blended2.png", cv.IMREAD_COLOR)if len(src.shape) != 2:    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)else:    gray = srcgray = cv.bitwise_not(gray)bw = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 15, -2)horizontal = np.copy(bw)vertical = np.copy(bw)cols = horizontal.shape[1]horizontal_size = int(cols / 30)horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))horizontal = cv.erode(horizontal, horizontalStructure)horizontal = cv.dilate(horizontal, horizontalStructure)cv.imwrite("img_horizontal8.png", horizontal)h_transpose = np.transpose(np.nonzero(horizontal))print("h_transpose")print(h_transpose[:100])rows = vertical.shape[0]verticalsize = int(rows / 30)verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize))vertical = cv.erode(vertical, verticalStructure)vertical = cv.dilate(vertical, verticalStructure)cv.imwrite("img_vertical8.png", vertical)v_transpose = np.transpose(np.nonzero(vertical))print("v_transpose")print(v_transpose[:100])img = src.copy()# edges = cv.Canny(vertical,50,150,apertureSize = 3)minLineLength = 100maxLineGap = 200lines = cv.HoughLinesP(vertical,1,np.pi/180,100,minLineLength,maxLineGap)for line in lines:    for x1,y1,x2,y2 in line:        cv.line(img,(x1,y1),(x2,y2),(0,255,0),2)cv.imshow('houghlinesP_vert', img)cv.waitKey(0)
查看完整描述

1 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

一種方法是使用霍夫變換來檢測線并獲得每條線的角度。然后可以通過減去兩條線之間的差來找到兩條線之間的角度。


我們首先使用算術平均來np.mean對導致此結果的圖像進行閾值處理。


image = cv2.imread('2.png')


# Compute arithmetic mean

image = np.mean(image, axis=2)

http://img1.sycdn.imooc.com//622898980001d5de05130384.jpg

現在我們執行skimage.transform.hough_line檢測線


# Perform Hough Transformation to detect lines

hspace, angles, distances = hough_line(image)


# Find angle

angle=[]

for _, a , distances in zip(*hough_line_peaks(hspace, angles, distances)):

    angle.append(a)

http://img1.sycdn.imooc.com//622898a600013c1e05150385.jpg

接下來我們獲得每條線的角度并找到差異以獲得我們的結果


# Obtain angle for each line

angles = [a*180/np.pi for a in angle]


# Compute difference between the two lines

angle_difference = np.max(angles) - np.min(angles)

print(angle_difference)

16.08938547486033


完整代碼


from skimage.transform import (hough_line, hough_line_peaks)

import numpy as np

import cv2


image = cv2.imread('2.png')


# Compute arithmetic mean

image = np.mean(image, axis=2)


# Perform Hough Transformation to detect lines

hspace, angles, distances = hough_line(image)


# Find angle

angle=[]

for _, a , distances in zip(*hough_line_peaks(hspace, angles, distances)):

    angle.append(a)


# Obtain angle for each line

angles = [a*180/np.pi for a in angle]


# Compute difference between the two lines

angle_difference = np.max(angles) - np.min(angles)

print(angle_difference)


查看完整回答
反對 回復 2022-03-09
  • 1 回答
  • 0 關注
  • 459 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號