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: 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”.
This is the code that works fine:
Code:
import cv2import numpy as npfilename = 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 = 1000Min = 1000def blank(x): # Null function for trackbar passcv2.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 breakcv2.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 notcv2.imwrite("final_img_new.jpg", final_img)
However, here is the code that does not work so well:
Code:
#program4.pyimport cv2import numpy as nprect1=(0,654) #up-left point of the maskrect2=(1000,1005) #down-right point of the maskfilename = 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 = 1000Min = 1000def blank(x): # Null function for trackbar pass scale = 0.5new_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 breakcv2.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 nocv2.imwrite("hallway_edges.jpg", output)
Statistics: Posted by ricky2777 — Sun Feb 16, 2025 4:49 am