官术网_书友最值得收藏!

Applying pencil sketch transformation

With the tricks that we learned from the previous sections in our bag, we are now ready to take a look at the entire procedure.

The final code can be found in the convert_to_pencil_sketch function within the tools.py file.

The following procedure shows you how to convert a color image into grayscale. After that, we aim to blend the grayscale image with its blurred negative:

  1. First, we convert an RGB image (imgRGB) into grayscale:
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY) 

As you can see, we have used cv2.COLOR_RGB2GRAY as a parameter to the cv2.cvtColor function, which changes the color spaces. Note that it does not matter whether the input image is RGB or BGR (which is the default for OpenCV); we will get a nice grayscale image in the end.

  1. Then, we invert the image and blur it with a large Gaussian kernel of size (21,21):
    inv_gray = 255 - gray_image
blurred_image = cv2.GaussianBlur(inv_gray, (21, 21), 0, 0)
  1. We use dodge to blend the original grayscale image with the blurred inverse:
    gray_sketch = cv2.divide(gray_image, 255 - blurred_image, 
scale=256)

The resulting image looks like this:

Image credit—"Lenna" by Conor Lawless is licensed under CC BY 2.0

Did you notice that our code can be optimized further? Let's take a look at how to optimize with OpenCV next.

主站蜘蛛池模板: 郎溪县| 昌黎县| 鹰潭市| 保康县| 内乡县| 赣榆县| 哈巴河县| 彭山县| 安平县| 涟源市| 丰原市| 芮城县| 佛山市| 尖扎县| 锡林浩特市| 苏尼特左旗| 文登市| 兰考县| 平舆县| 沙田区| 富川| 富锦市| 正镶白旗| 巴林左旗| 兴安县| 丹东市| 兴业县| 木兰县| 蓝山县| 公主岭市| 焉耆| 太白县| 娄烦县| 四会市| 乌审旗| 巩义市| 扎赉特旗| 左贡县| 巢湖市| 灵台县| 邢台县|