簡體   English   中英

多條水平線和垂直線未顯示

[英]Multiple Horizontal & Vertical Lines are not showing

在這里,我嘗試使用opencv,霍夫變換來查找水的高度。 該程序包括Canny邊緣檢測,背景減法,霍夫變換和高度估計。 然后使用高度繪制圖表,但這只會檢測垂直線。 沒有顯示多條水平和垂直線。 代碼有問題嗎?

程序輸出: 1 2

from imutils.perspective import four_point_transform
from imutils import paths
import numpy as np
import imutils 
import argparse
import cv2
import random
import math
import matplotlib.pyplot as plt

scaling_factorx = 0.8
scaling_factory = 0.8

cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractorMOG2()
cap.set(3,640)
cap.set(4,480)
count = 0
height = []

while(1):

    ret, frame = cap.read()

    if frame is None:
        break
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 1, 100)
    lines = cv2.HoughLinesP(edges, rho = 1,theta = 2*np.pi/180,threshold = 10,minLineLength = 100,maxLineGap = 10);
    if lines is not None:
        for line in lines[0]:
            dot1 = (line[0],line[1])
            dot2 = (line[2],line[3])
            cv2.line(frame, dot1, dot2, (255,0,0), 3)
            length = line[1] - line[3]
            print(length)
            height.append(length)

    cv2.imshow("output", frame)

    frame = cv2.resize(frame, None, fx = scaling_factorx, fy = scaling_factory, interpolation = cv2.INTER_AREA)

    fgmask = fgbg.apply(frame)
    cv2.imshow('frame', fgmask)

    gray_vid = cv2.cvtColor(frame, cv2.IMREAD_GRAYSCALE)
    cv2.imshow('Original', frame)

    edged_frame = cv2.Canny(frame, 1, 100)
    cv2.imshow('Edges', edged_frame)

    if cv2.waitKey(1) & 0xFF ==ord('q'):
        break

x = []
y = []

for i in range(len(height)):
    x.append(i)
    y.append(height[i])

cap.release()
cv2.destroyAllWindows()
print(x,y) 
plt.plot(x, y)  
plt.xlabel('x - axis') 
plt.ylabel('y - axis')  
plt.title('Height') 
plt.show()

您只畫一條線,因為for循環線僅循環一條線。

if lines is not None:
    for line in lines: #for each line...not just one of them
        for x1,y1,x2,y2 in line:
            dot1 = (x1,y1)
            dot2 = (x2,y2)
            cv2.line(frame, dot1, dot2, (255,0,0), 3)
            length = y1 - y2
            print(length)
            height.append(length)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM