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:
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)
It returns this:
The above is the code that works well.
However, here is the code that does not work so well:
#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)
I only got a black image returned