Python Forum
opencv troubleshooting when using Canny
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
opencv troubleshooting when using Canny
#1
I was trying to use a tracebar to change the value of shresholds for Canny. Here is my code:
#HoughLinesP 
import cv2
import numpy as np
filename  = input("Enter name of file to process: ")
original  = cv2.imread(filename)
height, width = original.shape[:2]
Max=1000
Min=1000
def blank(x):       # null function for trackbar
   pass;
MaxVal = (f'MaxVal 0: {Max-1}')
MinVal = (f'MinVall 0: {Min-1}')
cv2.namedWindow('window', cv2.WINDOW_NORMAL)
cv2.resizeWindow('window', width, height)
cv2.createTrackbar(MaxVal, 'window', 0, Max-1, blank)
cv2.createTrackbar(MinVal, 'window', 0, Min-1,  blank)

while True:
	maxv = cv2.getTrackbarPos(MaxVal, 'window')
	minv = cv2.getTrackbarPos(MinVal, 'window')
	edge=cv2.Canny(original,minv,maxv)
	lines=cv2.HoughLinesP(
		edge,  
		1, 
		np.pi/180, 
		threshold =100, 
		# lines        
		minLineLength=20, 
		maxLineGap =10)   
	for i in range(len(lines)):
		for x1, y1, x2, y2 in lines[i]:
			cv2.line(original, (x1, y1), (x2, y2), (0, 255, 0), 2)
	cv2.imshow("houghline",original)
	# cv2.waitKey()
	# cv2.destroyAllWindows()
	if cv2.waitKey(1) == 32:     # stop when space bar hit
		cv2.destroyAllWindows()
	
When I run, I got this error:

Quote:Enter name of file to process: sidewalk.jpg
QSocketNotifier: Can only be used with threads started with QThread
QStandardPaths: wrong permissions on runtime directory /run/user/1000, 0770 instead of 0700
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
Traceback (most recent call last):
File "/home/ricky/Desktop/module练习/module5.py", line 227, in <module>
for i in range(len(lines)):
^^^^^^^^^^
TypeError: object of type 'NoneType' has no len()

Please help
Reply
#2
lines is your problem there. I got:

Quote:Traceback (most recent call last):
File "<pyshell#21>", line 13, in <module>
for i in range(len(lines)):
TypeError: object of type 'NoneType' has no len()

If I change the for loop to:

for i in range(len(lines)-1):
        for x1, y1, x2, y2 in lines[i]:
            cv2.line(original, (x1, y1), (x2, y2), (0, 255, 0), 2)
It runs without a problem.

Not sure exactly what you want to do though!

This is info on trackbars

This is info on Hough lines
Reply
#3
Thanks for replying. But I was trying to use a window with tracebar that can change the lower threshold and upper threshold for Canny Edge Detection. I tried the method. But After I changed the value for Canny, it still gives me this error:
And Problem solved thanks to this: https://forums.raspberrypi.com/viewtopic...0#p2293400
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem When using canny edge detection,black image returned rickyw2777 1 464 Feb-17-2025, 03:22 AM
Last Post: rickyw2777
  Troubleshooting Jupyter Notebook installation with Python using pip and a venv Drone4four 1 2,218 Jun-04-2024, 10:55 PM
Last Post: Drone4four
  OBS Script Troubleshooting Jotatochips 0 915 Feb-10-2024, 06:18 PM
Last Post: Jotatochips
  Troubleshooting with PySpy Positron79 0 1,421 Jul-24-2022, 03:22 PM
Last Post: Positron79
  Troubleshooting site packages ('h5py' to be specific) aukhare 2 2,940 Nov-02-2020, 03:04 PM
Last Post: snippsat
  Troubleshooting simple script and printing class objects Drone4four 11 10,385 Dec-16-2017, 05:12 AM
Last Post: Drone4four

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020