0

I'm trying to "fix" data output from a function (OpenCV v3, HoughLinesP). The first "row" of the output is always "None"... while the rest of the output seems to work just fine.
I'm trying to check for that first row (actually, any row) that is "None" and assign it a value of "0" so that I can iterate over the array.
Right now I have this (but it doesn't seem to work):

import cv2
import numpy as np
import imutils 

np.set_printoptions(threshold=np.inf) #to print entire array, no truncation

LOWER_BOUND = 55   #cv2.threshold()
UPPER_BOUND = 255  #cv2.threshold()

CANNY_LOWER_BOUND = 10  #cv2.Canny()
CANNY_UPPER_BOUND = 250 #cv2.Canny()

MIN_LINE_LENGTH = 2  #HoughLinesP()
MAX_LINE_GAP = 100     #HoughLinesP() 
HOUGH_THETA = np.pi/180 #HoughLinesP() angle resolution of the accumulator, radians
HOUGH_THRESHOLD = 25 #HoughLinesP() 
HOUGH_RHO = 1         #HoughLinesP() rho, Distance resolution of the accumulator, pixels


bkgnd = cv2.bgsegm.createBackgroundSubtractorMOG()
camera = cv2.VideoCapture('/home/odroid/Desktop/python_scripts/test/test_images/Edited_Foam_Dispense_Short.mp4')

run_once = 0

while(run_once <= 1):
    (grabbed, frame) = camera.read()

    img_sub = bkgnd.apply(frame)#, learningRate = 0.001)

    canny_threshold = cv2.Canny(img_sub, CANNY_LOWER_BOUND, CANNY_UPPER_BOUND)  

    hlines = cv2.HoughLinesP(canny_threshold, HOUGH_RHO, HOUGH_THETA, MIN_LINE_LENGTH, MAX_LINE_GAP)

#************************* The if statement for the empty element*****
    if hlines is None:
        hlines[0] = [0, 0, 0, 0]
    else:

        for l in hlines:
            leftx, boty, rightx, topy = l[0]
            line = Line((leftx, boty), (rightx, topy)) 
            line.draw(frame, (0, 255, 0), 2)
#*************************************************************      

cv2.imshow('canny_threshold', canny_threshold)
cv2.imshow("frame", frame)

run_once = 1 + run_once 

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

camera.release()
cv2.destroyAllWindows()

And here's the error I'm getting:

    Traceback (most recent call last):

File "test3.py", line 41, in hlines[0] = [0, 0, 0, 0] TypeError: 'NoneType' object does not support item assignment

What I'm trying to do is assign that first row of hlines to [[0,0,0,0]] so it matches the rest of the array (meaning that there's data). Why is this not working?

slow_one
  • 113
  • 1
  • 4
  • 13
  • The error occurs earlier in your code. It hasn't even reached the `None` test. – hpaulj Apr 28 '17 at 17:52
  • which is weird because if I remove this check ... then it errors out on the "None" value of the array. – slow_one Apr 28 '17 at 17:54
  • That's probably occurring on an earlier loop. What does `cv2.HoughLinesP` normally produce, and why/when might it produce `None` instead? If `hlines` is indeed `None`, then it does not make sense to do `hlines[0]`. That's like writing `None[0]`. `hlines is None` does not test for an empty array or list containing `None`. It tests whether the variable itself is `None. – hpaulj Apr 28 '17 at 19:02
  • It seems like HoughLinesP returns "None" for the first pass ... and then succeeds on every subsequent pass though of my While loop. Which, should be fine ... as long as I can get rid of the None or have it continue to iterate if there is a "None" – slow_one Apr 28 '17 at 19:04
  • Do you need to do anything if it returns `None`? You could do `hlines=[[0,0,0,0]]`, but why do even that? Your code doesn't pass that to the `else` body. – hpaulj Apr 28 '17 at 19:07
  • i've tried doing that and I get an error – slow_one Apr 28 '17 at 19:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142932/discussion-between-slow-one-and-hpaulj). – slow_one Apr 28 '17 at 19:15
  • Now, I've also tried filtering out the None elements of the array ... but if I do that (hlines[hlines != np.array(None)] ) Then I get an error stating: TypeError: 'NoneType' object has no attribute '__getitem__' – slow_one Apr 28 '17 at 19:24
  • What is this `array` supposed to contain? What's its dtype and shape. I find it highly unusual to be talking about an array containing `None` elements. – hpaulj Apr 28 '17 at 19:34
  • the array is an output from HoughLinesP. It is a multidimensional array (as far as I understand it). It contains start and end points of the points of lines detected by the function HoughLinesP shaped like so: [[[(leftx, boty), (rightx, topy)]][[(leftx, boty), (rightx, topy)]][[(leftx, boty), (rightx, topy)]] ... [[(leftx, boty), (rightx, topy)]]] It seems that the "first" frame processed by my script always returns a HoughLinesP array of "None" – slow_one Apr 28 '17 at 19:46

1 Answers1

1

The HoughLinesP function was returning an empty array on the first frame (returning "None"). This caused errors down the line. Adding in a check for that fixed the issue.

if hlines is None: #in case HoughLinesP fails to return a set of lines
        #make sure that this is the right shape [[ ]] and ***not*** []
        hlines = [[0,0,0,0]]
    else:
        for l in hlines:
            leftx, boty, rightx, topy = l[0]
            cv2.line(frame, (leftx, boty), (rightx, topy), (0, 255, 0), 2)
slow_one
  • 113
  • 1
  • 4
  • 13