|
| 1 | +#coding:utf-8 |
| 2 | +#By:Eastmount CSDN 2020-12-22 |
| 3 | +import cv2 |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +def dodgeNaive(image, mask): |
| 7 | + # determine the shape of the input image |
| 8 | + width, height = image.shape[:2] |
| 9 | + |
| 10 | + # prepare output argument with same size as image |
| 11 | + blend = np.zeros((width, height), np.uint8) |
| 12 | + |
| 13 | + for col in range(width): |
| 14 | + for row in range(height): |
| 15 | + # do for every pixel |
| 16 | + if mask[col, row] == 255: |
| 17 | + # avoid division by zero |
| 18 | + blend[col, row] = 255 |
| 19 | + else: |
| 20 | + # shift image pixel value by 8 bits |
| 21 | + # divide by the inverse of the mask |
| 22 | + tmp = (image[col, row] << 8) / (255 - mask) |
| 23 | + # print('tmp={}'.format(tmp.shape)) |
| 24 | + # make sure resulting value stays within bounds |
| 25 | + if tmp.any() > 255: |
| 26 | + tmp = 255 |
| 27 | + blend[col, row] = tmp |
| 28 | + |
| 29 | + return blend |
| 30 | + |
| 31 | +def dodgeV2(image, mask): |
| 32 | + return cv2.divide(image, 255 - mask, scale=256) |
| 33 | + |
| 34 | +def burnV2(image, mask): |
| 35 | + return 255 - cv2.divide(255 - image, 255 - mask, scale=256) |
| 36 | + |
| 37 | +def rgb_to_sketch(src_image_name, dst_image_name): |
| 38 | + img_rgb = cv2.imread(src_image_name) |
| 39 | + img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) |
| 40 | + # 读取图片时直接转换操作 |
| 41 | + # img_gray = cv2.imread('example.jpg', cv2.IMREAD_GRAYSCALE) |
| 42 | + |
| 43 | + img_gray_inv = 255 - img_gray |
| 44 | + img_blur = cv2.GaussianBlur(img_gray_inv, ksize=(21, 21), |
| 45 | + sigmaX=0, sigmaY=0) |
| 46 | + img_blend = dodgeV2(img_gray, img_blur) |
| 47 | + |
| 48 | + cv2.imshow('original', img_rgb) |
| 49 | + cv2.imshow('gray', img_gray) |
| 50 | + cv2.imshow('gray_inv', img_gray_inv) |
| 51 | + cv2.imshow('gray_blur', img_blur) |
| 52 | + cv2.imshow("pencil sketch", img_blend) |
| 53 | + cv2.waitKey(0) |
| 54 | + cv2.destroyAllWindows() |
| 55 | + cv2.imwrite(dst_image_name, img_blend) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + src_image_name = 'scenery.png' |
| 60 | + dst_image_name = 'sketch_example.jpg' |
| 61 | + rgb_to_sketch(src_image_name, dst_image_name) |
0 commit comments