简体   繁体   English

多条水平线和垂直线未显示

[英]Multiple Horizontal & Vertical Lines are not showing

Here i am trying to find the height of the water using opencv, hough transform. 在这里,我尝试使用opencv,霍夫变换来查找水的高度。 This program consist of canny edge detection, background subtraction, then the hough transform and height estimation. 该程序包括Canny边缘检测,背景减法,霍夫变换和高度估计。 Then using the height it plot the graph.But this only detects the vertical lines. 然后使用高度绘制图表,但这只会检测垂直线。 Multiple Horizontal & vertical lines are not showing. 没有显示多条水平和垂直线。 Any problem with the code? 代码有问题吗?

Output of the program: 程序输出: 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()

You only draw one line because your for loop over lines is only looping over a single line. 您只画一条线,因为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