|
| 1 | +from cv2 import cv2 |
| 2 | +import numpy as np |
| 3 | +import sys |
| 4 | + |
| 5 | +image_name = input("Enter the name of the input image: ") |
| 6 | + |
| 7 | +# reading the image |
| 8 | +img = cv2.imread(image_name) |
| 9 | + |
| 10 | +while img is None: |
| 11 | + image_name = input("Enter the name of the input image or Enter 'exit' to end program : ") |
| 12 | + # if end the program |
| 13 | + if image_name == "exit": |
| 14 | + sys.exit() |
| 15 | + |
| 16 | + # reading the image |
| 17 | + img = cv2.imread(image_name) |
| 18 | + |
| 19 | +# resize the image to fit it to show in the screen |
| 20 | +img = cv2.resize(img, (0, 0), None, .75, .75) |
| 21 | + |
| 22 | +# converting the image to grayscale |
| 23 | +img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
| 24 | +# inverting the image |
| 25 | +img_invert = cv2.bitwise_not(img_gray) |
| 26 | +# bluring or smoothing the inverted image with the kernel size (25,25) |
| 27 | +img_blur = cv2.blur(img_invert, (25, 25)) |
| 28 | + |
| 29 | +# Applying another type of blur for cheaking |
| 30 | +img_blur_2 = cv2.GaussianBlur(img_invert, (23, 23),0) |
| 31 | + |
| 32 | +# The Dodge blend function divides the bottom layer by the inverted top layer. |
| 33 | +# This lightens the bottom layer depending on the value of the top layer. |
| 34 | +# We have the blurred image, which highlights the boldest edges. |
| 35 | +def dodgeV2(image, mask): |
| 36 | + # inverting color with 255 - ... |
| 37 | + return cv2.divide(image, 255 - mask, scale=256) |
| 38 | + |
| 39 | + |
| 40 | +final_img = dodgeV2(img_gray, img_blur) |
| 41 | + |
| 42 | +final_img_2 = dodgeV2(img_gray, img_blur_2) |
| 43 | +# final_img_2 = cv2.blur(final_img_2, (3, 3),0) |
| 44 | +final_img_2 = cv2.bilateralFilter(final_img_2,9,50,50) |
| 45 | + |
| 46 | +# convert to bgr for showing the input and output in the same window |
| 47 | +# this will convert the output from 2 channel to 3 channel |
| 48 | +final_img = cv2.cvtColor(final_img, cv2.COLOR_GRAY2BGR) |
| 49 | +# concatenate both the input and output |
| 50 | +numpy_vertical_concat = np.concatenate((img, final_img), axis=1) |
| 51 | + |
| 52 | +# cv2.imshow('image', final_img) |
| 53 | +# cv2.imshow('image_2', final_img_2) |
| 54 | + |
| 55 | +# displaying the sketch image |
| 56 | +cv2.imshow('image', numpy_vertical_concat) |
| 57 | +print("Press 'Esc' button to exit or Press 's' to save the image and exit.") |
| 58 | + |
| 59 | +k = cv2.waitKey(0) |
| 60 | +# if escape is pressed |
| 61 | +if k == 27: |
| 62 | + cv2.destroyAllWindows() |
| 63 | +# save the output image |
| 64 | +elif k == ord('s'): |
| 65 | + cv2.imwrite('output.png', final_img) |
| 66 | + cv2.imwrite('combined.png',numpy_vertical_concat) |
| 67 | + cv2.destroyAllWindows() |
0 commit comments