Skip to content

Instantly share code, notes, and snippets.

@mineshpatel1
Last active October 10, 2019 07:55
Show Gist options
  • Save mineshpatel1/22e86200eee86ebe3e221343b26fc3f3 to your computer and use it in GitHub Desktop.
Save mineshpatel1/22e86200eee86ebe3e221343b26fc3f3 to your computer and use it in GitHub Desktop.
Sudoku Solver 2 - Appendix 1 #blog #bernard
def display_points(in_img, points, radius=5, colour=(0, 0, 255)):
"""Draws circular points on an image."""
img = in_img.copy()
# Dynamically change to a colour image if necessary
if len(colour) == 3:
if len(img.shape) == 2:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
elif img.shape[2] == 1:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
for point in points:
img = cv2.circle(img, tuple(int(x) for x in point), radius, colour, -1)
show_image(img)
return img
def display_rects(in_img, rects, colour=255):
"""Displays rectangles on the image."""
img = in_img.copy()
for rect in rects:
img = cv2.rectangle(img, tuple(int(x) for x in rect[0]), tuple(int(x) for x in rect[1]), colour)
show_image(img)
return img
def plot_many_images(images, titles, rows=1, columns=2):
"""Plots each image in a given list in a grid format using Matplotlib."""
for i, image in enumerate(images):
plt.subplot(rows, columns, i+1)
plt.imshow(image, 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([]) # Hide tick marks
plt.show()
def show_image(img):
"""Shows an image until any key is pressed."""
cv2.imshow('image', img) # Display the image
cv2.waitKey(0) # Wait for any key to be pressed (with the image window active)
cv2.destroyAllWindows() # Close all windows
def show_digits(digits, colour=255):
"""Shows list of 81 extracted digits in a grid format"""
rows = []
with_border = [cv2.copyMakeBorder(img.copy(), 1, 1, 1, 1, cv2.BORDER_CONSTANT, None, colour) for img in digits]
for i in range(9):
row = np.concatenate(with_border[i * 9:((i + 1) * 9)], axis=1)
rows.append(row)
show_image(np.concatenate(rows))
def show_image(img):
"""Shows an image until any key is pressed"""
cv2.imshow('image', img) # Display the image
cv2.waitKey(0) # Wait for any key to be pressed (with the image window active)
cv2.destroyAllWindows() # Close all windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment