Python Forum
Problem When using canny edge detection,black image returned
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem When using canny edge detection,black image returned
#1
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:
[Image: file.php?id=71841]
   

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)
Here is what I have got by printing the “output”, and “masked_img” and “small_img”.

[Image: file.php?id=71842]
   
Reply
#2
Problem Solved, it turns out that I have got the wrong upper-left and lower-right coordinates for the mask.
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why it gives me a black image and libpng warning: iCCP rickyw2777 1 1,085 Feb-16-2025, 08:26 PM
Last Post: rickyw2777
  opencv troubleshooting when using Canny rickyw2777 2 1,420 Feb-08-2025, 06:17 PM
Last Post: rickyw2777
  Bright Black Screen Issue in Tkinter GUI Application rommy 2 2,210 Nov-29-2024, 10:50 PM
Last Post: woooee
  Black jack game simulation RoxaneParis1 3 2,035 Sep-11-2024, 06:57 AM
Last Post: indel635kanojia
  tkinter photo image problem jacksfrustration 5 5,884 Jun-27-2024, 12:06 AM
Last Post: AdamHensley
  element in list detection problem jacksfrustration 5 2,742 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  How to access values returned from inquirer cspower 6 4,436 Dec-26-2023, 09:34 PM
Last Post: cspower
  SQLAlchemy Object Missing when Null is returned Personne 1 4,631 Feb-19-2022, 02:50 AM
Last Post: Larz60+
  remove all color but red, then replace it with black kucingkembar 14 15,107 Dec-29-2021, 07:50 PM
Last Post: deanhystad
  Help me with Image detection GoA_TrancE 1 3,613 Oct-18-2021, 02:03 PM
Last Post: GoA_TrancE

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.