💾 Archived View for gemini.ctrl-c.club › ~supatoad › hough.py captured on 2024-06-16 at 13:09:45.
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
from PIL import Image import sys, math, time # Variables to track potential lines and targeted pixels potentiallines = 0 targetedpixels = 0 # Function to find points on the line and accumulate votes in Hough space def findline(r): # , theta): global pixels, linecolour, tolerance, houghspace for theta in range(360): for x in range(img.size[0]): # Calculate the corresponding y coordinate for the given x, r, and theta sin_theta = math.sin(math.radians(theta)) if sin_theta != 0: # Avoid division by zero y = int((r - x * math.cos(math.radians(theta))) / sin_theta) try: colour = pixels[x, y] if ( colour[0] <= linecolour[0] + tolerance and colour[0] >= linecolour[0] - tolerance and colour[1] <= linecolour[1] + tolerance and colour[1] >= linecolour[1] - tolerance and colour[2] <= linecolour[2] + tolerance and colour[2] >= linecolour[2] - tolerance ): # Found colour similar to the target line colour # Increment the accumulator at the corresponding (theta, r) pair houghspace[(theta, int(r))] = houghspace.get((theta, int(r)), 0) + 1 except: pass # Command-line arguments for the target line colour and tolerance linecolour = (int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3])) tolerance = int(sys.argv[4]) # Open the input image and load its pixel data img = Image.open(sys.argv[5]) pixels = img.load() # Calculate the size of the Hough space based on the image dimensions size = int(math.sqrt(img.size[0] ** 2 + img.size[1] ** 2)) # Initialize the Hough space accumulator dictionary with empty houghspace = {} # Iterate over each pixel in the image for x in range(img.size[0]): for y in range(img.size[1]): # For every pixel in the image colour = pixels[x, y] if ( colour[0] <= linecolour[0] + tolerance and colour[0] >= linecolour[0] - tolerance and colour[1] <= linecolour[1] + tolerance and colour[1] >= linecolour[1] - tolerance and colour[2] <= linecolour[2] + tolerance and colour[2] >= linecolour[2] - tolerance ): # Found a pixel similar to the target line colour # Calculate distance to origin (r) for this pixel r = math.sqrt(x ** 2 + y ** 2) # Look for other points on the line and accumulate votes in Hough space findline(r) # , theta) print("\n\n\n\n\n\n") print(houghspace) #generate image size = int(math.sqrt(img.size[0]**2 + img.size[1]**2)) hough = Image.new('RGB', (size, 360), "black") houghpixels = hough.load() for item in houghspace.items(): print(item) x=item[0][1] y=item[0][0] col=item[1] houghpixels[x,y]=(col,col,col) hough.show()