最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用opencv相關(guān)函數(shù)確定圖片中的直線問題

 更新時間:2022年11月10日 10:26:25   作者:фора 快跑  
這篇文章主要介紹了使用opencv相關(guān)函數(shù)確定圖片中的直線問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

使用opencv相關(guān)函數(shù)確定圖片中的直線

 
#pip install opencv-python==4.4.0.42 opencv-contrib-python==4.4.0.42
 
import cv2
import numpy as np
from matplotlib import pyplot as plt
 
import matplotlib.image as mpimg
import matplotlib as mpl
from PIL import Image
mpl.rcParams['figure.dpi'] = 200
 
#加載圖像
img = cv2.imread('test.png')
plt.axis('off')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
#灰度圖像
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#cornerHarris要求img是flaot32位
img1 = np.float32(img1)
#Harris角點(diǎn)檢測函數(shù)
#  ? img - 數(shù)據(jù)類型為 float32 的輸入圖像。
#  ? blockSize - 角點(diǎn)檢測中要考慮的領(lǐng)域大小。
#  ? ksize - Sobel 求導(dǎo)中使用的窗口大小
#  ? k - Harris 角點(diǎn)檢測方程中的自由參數(shù),取值參數(shù)為 [0,04,0.06].
dst = cv2.cornerHarris(img1,10,3,0.04)
plt.axis('off')
plt.imshow(dst, cmap='gray')
plt.show()
#膨脹
dst = cv2.dilate(dst,None)
plt.axis('off')
plt.imshow(dst, cmap='gray')
plt.show()
#顯示經(jīng)過處理后的圖片
threshold = 0.01*dst.max()
img[dst>threshold]=[255,0,0] 
#[255,0,0] - 點(diǎn)的顏色:藍(lán)色, [0,255,0] - 綠色, [0,0,255] - 紅色,
#[0,0,0] -白色, [255,255,255] - 黑色
plt.axis('off')
plt.imshow(img, cmap='gray')
plt.show()
 
#使用另一種角點(diǎn)檢測函數(shù)
corners = cv2.goodFeaturesToTrack(gray, 6, 0.01, 5)
corners = np.int0(corners) 
for i in corners:
    x, y = i.ravel()
    cv2.circle(img, (x, y), 5, [0,0,0], -1) 
plt.axis('off')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
 
#直線檢測
 
img = cv2.imread("road.jpeg")
plt.axis('off')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
 
#canny 方法檢測邊緣 返回二值圖像
edges = cv2.Canny(gray, 150, 300)
plt.axis('off')
plt.imshow(edges, cmap='gray')
plt.show()
#HoughLinesP方法判斷哪些邊緣是直線
 
lines = cv2.HoughLinesP(edges, rho=1.0,theta=np.pi/180,threshold=20,minLineLength=30,maxLineGap=10)
line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8) 
line_color = [0, 255, 0]
line_thickness = 2
dot_color = [0, 255, 0]
dot_size = 3
 
#講檢測的直線疊加到原圖
for line in lines:
    for x1, y1, x2, y2 in line:
        cv2.line(line_img, (x1, y1), (x2, y2), line_color, line_thickness)
        cv2.circle(line_img, (x1, y1), dot_size, dot_color, -1)
        cv2.circle(line_img, (x2, y2), dot_size, dot_color, -1)
final = cv2.addWeighted(img, 0.8, line_img, 1.0, 0.0) 
plt.axis('off')
plt.imshow(cv2.cvtColor(final, cv2.COLOR_BGR2RGB))
plt.show()
 

OpenCV:直線檢測

主要介紹OpenCV自帶的直線檢測函數(shù)HoughLines()的用法,這個函數(shù)的第一個參數(shù)是一個二值化圖像,所以在進(jìn)行霍夫變換之前要首先進(jìn)行二值化,或者進(jìn)行Canny 邊緣檢測。第二和第三個值分別代表β 和 θ 的精確度。

第四個參數(shù)是閾值,只有累加其中的值高于閾值時才被認(rèn)為是一條直線,也可以把它看成能檢測到的直線的最短長度(以像素點(diǎn)為單位)。返回值就是(β; θ)。β 的單位是像素,θ的單位是弧度。

看代碼 

#直線檢測
#使用霍夫直線變換做直線檢測,前提條件:邊緣檢測已經(jīng)完成
import cv2 as cv
import numpy as np
import matplotlib.pylab as plt
 
#標(biāo)準(zhǔn)霍夫線變換
def line_detection(image):
    gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
    edges = cv.Canny(gray, 50, 150, apertureSize=3)  #apertureSize參數(shù)默認(rèn)其實(shí)就是3
    cv.imshow("edges", edges)
    lines = cv.HoughLines(edges, 1, np.pi/180, 80)
    for line in lines:
        rho, theta = line[0]  #line[0]存儲的是點(diǎn)到直線的極徑和極角,其中極角是弧度表示的。
        a = np.cos(theta)   #theta是弧度
        b = np.sin(theta)
        x0 = a * rho    #代表x = r * cos(theta)
        y0 = b * rho    #代表y = r * sin(theta)
        x1 = int(x0 + 1000 * (-b)) #計算直線起點(diǎn)橫坐標(biāo)
        y1 = int(y0 + 1000 * a)    #計算起始起點(diǎn)縱坐標(biāo)
        x2 = int(x0 - 1000 * (-b)) #計算直線終點(diǎn)橫坐標(biāo)
        y2 = int(y0 - 1000 * a)    #計算直線終點(diǎn)縱坐標(biāo)    注:這里的數(shù)值1000給出了畫出的線段長度范圍大小,數(shù)值越小,畫出的線段越短,數(shù)值越大,畫出的線段越長
        cv.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)    #點(diǎn)的坐標(biāo)必須是元組,不能是列表。
    cv.imshow("image-lines", image)
 
#統(tǒng)計概率霍夫線變換
def line_detect_possible_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
    edges = cv.Canny(gray, 50, 150, apertureSize=3)  # apertureSize參數(shù)默認(rèn)其實(shí)就是3
    lines = cv.HoughLinesP(edges, 1, np.pi / 180, 60, minLineLength=60, maxLineGap=5)
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
    cv.imshow("line_detect_possible_demo",image)
 
src = cv.imread(r'..\edge.jpg')
print(src.shape)
cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE)
cv.imshow('input_image', src)
line_detection(src)
src = cv.imread(r'..\edge.jpg') #調(diào)用上一個函數(shù)后,會把傳入的src數(shù)組改變,所以調(diào)用下一個函數(shù)時,要重新讀取圖片
line_detect_possible_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

看效果

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用Anaconda創(chuàng)建Pytorch虛擬環(huán)境的排坑詳細(xì)教程

    使用Anaconda創(chuàng)建Pytorch虛擬環(huán)境的排坑詳細(xì)教程

    PyTorch是一個開源的Python機(jī)器學(xué)習(xí)庫,基于Torch,用于自然語言處理等應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于使用Anaconda創(chuàng)建Pytorch虛擬環(huán)境的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • 淺談Python 命令行參數(shù)argparse寫入圖片路徑操作

    淺談Python 命令行參數(shù)argparse寫入圖片路徑操作

    這篇文章主要介紹了淺談Python 命令行參數(shù)argparse寫入圖片路徑操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Python常用編碼的區(qū)別介紹

    Python常用編碼的區(qū)別介紹

    這篇文章介紹了Python常用編碼的區(qū)別,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 一些讓Python代碼簡潔的實(shí)用技巧總結(jié)

    一些讓Python代碼簡潔的實(shí)用技巧總結(jié)

    隨著項目代碼行數(shù)的增加,不可避免的遇到軟件架構(gòu)腐敗的問題,所以如何寫出簡潔的代碼至關(guān)重要,這篇文章主要給大家介紹了一些讓Python代碼簡潔的實(shí)用技巧,需要的朋友可以參考下
    2021-08-08
  • Python使用pip安裝報錯:is not a supported wheel on this platform的解決方法

    Python使用pip安裝報錯:is not a supported wheel on this platform的解決

    這篇文章主要介紹了Python使用pip安裝報錯:is not a supported wheel on this platform的解決方法,結(jié)合實(shí)例形式分析了在安裝版本正確的情況下pip安裝報錯的原因與相應(yīng)的解決方法,需要的朋友可以參考下
    2018-01-01
  • 遷移現(xiàn)有的python項目到pyproject.toml

    遷移現(xiàn)有的python項目到pyproject.toml

    本文將詳細(xì)介紹將現(xiàn)有的?Python?項目遷移到?pyproject.toml,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04
  • 最新評論

    高要市| 齐河县| 东丰县| 上饶县| 胶南市| 锡林浩特市| 原阳县| 安塞县| 石屏县| 延长县| 信宜市| 平阳县| 金华市| 克拉玛依市| 白沙| 巴东县| 安宁市| 新津县| 海兴县| 翁源县| 乡宁县| 大余县| 龙江县| 武乡县| 武夷山市| 读书| 虎林市| 桓台县| 武汉市| 北流市| 临夏市| 右玉县| 类乌齐县| 万全县| 泸西县| 宜春市| 陇西县| 同心县| 瓮安县| 鹤山市| 绥江县|