Last active
October 10, 2019 07:55
-
-
Save mineshpatel1/22e86200eee86ebe3e221343b26fc3f3 to your computer and use it in GitHub Desktop.
Sudoku Solver 2 - Appendix 1 #blog #bernard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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