Feb-16-2025, 04:48 AM
(This post was last modified: Feb-16-2025, 04:50 AM by rickyw2777.)
I am creating a program to give me a trackbar that can adjust the value of minv and maxv for the canny edge detection. Strangely, when I not include any resizing into it, it worked. When I resized the picture all it does is a returning a black picture.
This is the code that works fine:
It returns this:
![[Image: file.php?id=71841]](https://forums.raspberrypi.com/download/file.php?id=71841)
The above is the code that works well.
However, here is the code that does not work so well:
Here is what I have got by printing the “output”, and “masked_img” and “small_img”.
![[Image: file.php?id=71842]](https://forums.raspberrypi.com/download/file.php?id=71842)
This is the code that works fine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import cv2 import numpy as np filename = input ( "Enter name of file to process: " ) original = cv2.imread(filename) if original is None : print ( "Error: Unable to load image." ) exit() height, width = original.shape[: 2 ] Max = 1000 Min = 1000 def blank(x): # Null function for trackbar pass 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' ) original = cv2.GaussianBlur(original, ( 3 , 3 ), 0 ) edge = cv2.Canny(original, minv, maxv) lines = cv2.HoughLinesP(edge, 1 , np.pi / 180 , threshold = 100 , minLineLength = 20 , maxLineGap = 10 ) output = original.copy() # Avoid modifying the original image if lines is not None : # Check if lines were detected for i in range ( len (lines)): for x1, y1, x2, y2 in lines[i]: cv2.line(output, (x1, y1), (x2, y2), ( 0 , 255 , 0 ), 2 ) cv2.imshow( "houghline" , output) if cv2.waitKey( 1 ) = = 32 : # Stop when space bar is hit break cv2.destroyAllWindows() outstring = ( f 'MaxVal:{maxv} MinVal:{minv}' ) final_img = cv2.putText(output, outstring, (height / / 10 , width / / 10 ), cv2.FONT_HERSHEY_SIMPLEX, 1 , ( 0 , 0 , 255 ), 3 ) # 1 is for font size, 3 is for black or not cv2.imwrite( "final_img_new.jpg" , final_img) |
The above is the code that works well.
However, here is the code that does not work so well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#program4.py import cv2 import numpy as np rect1 = ( 0 , 654 ) #up-left point of the mask rect2 = ( 1000 , 1005 ) #down-right point of the mask filename = input ( "Enter name of file to process: " ) original = cv2.imread(filename) if original is None : print ( "Error: Unable to load image." ) exit() height, width = original.shape[: 2 ] Max = 1000 Min = 1000 def blank(x): # Null function for trackbar pass scale = 0.5 new_size = ( int (height * scale), int (width * scale)) small_img = cv2.resize(original, new_size, interpolation = cv2.INTER_LINEAR) mask = np.zeros(small_img.shape[: 2 ], dtype = np.uint8) mask = cv2.rectangle(mask,rect1,rect2, 255 , - 1 ) masked_img = cv2.bitwise_and(small_img, small_img, mask = mask) 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' ) small_img = cv2.GaussianBlur(masked_img, ( 3 , 3 ), 0 ) edge = cv2.Canny(small_img, minv, maxv) lines = cv2.HoughLinesP(edge, 1 , np.pi / 180 , threshold = 100 , minLineLength = 20 , maxLineGap = 10 ) output = small_img.copy() # Avoid modifying the original image if lines is not None : # Check if lines were detected for i in range ( len (lines)): for x1, y1, x2, y2 in lines[i]: cv2.line(output, (x1, y1), (x2, y2), ( 0 , 255 , 0 ), 2 ) cv2.imshow( "houghline" , output) # cv2.imshow("masked",masked_img) # cv2.imshow("smallsize",small_img) # cv2.imshow("origin",original) # output = edge.copy() # Avoid modifying the original image # cv2.imshow("canny edge detection", edge) if cv2.waitKey( 1 ) = = 32 : # Stop when space bar is hit break cv2.destroyAllWindows() outstring = ( f 'MaxVal:{maxv} MinVal:{minv}' ) final_img = cv2.putText(output, outstring, (height / / 10 , width / / 10 ), cv2.FONT_HERSHEY_SIMPLEX, 1 ,( 255 , 255 , 255 ), 3 ) # 1 is for font size, 3 is for black or no cv2.imwrite( "hallway_edges.jpg" , output) |