Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the cartoon effect of image by Opencv

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this article Xiaobian for you to introduce in detail "Opencv how to achieve image cartoon effect", the content is detailed, the steps are clear, the details are handled properly, I hope this "Opencv how to achieve image cartoon effect" article can help you solve your doubts, the following follow the editor's ideas slowly in depth, together to learn new knowledge.

The full name of OpenCV is Open Source Computer Vision Library. OpenCV is a cross-platform computer visual library based on (open source) distribution that can run on Linux, Windows and Mac OS operating systems. It is lightweight and efficient-it consists of a series of C functions and a small number of C++ classes, provides interfaces to Python, Ruby, MATLAB and other languages, and implements many general algorithms in image processing and computer vision.

Pencil sketch effect

The input image is grayed out and retrograde Gaussian filtering, in which the larger the kernel number is, the more blurred the kernel number is because the Gaussian filter obeys normal distribution. The last step is to divide the original gray image by the blurred grayscale image. This gives the rate of change between each pixel in the two images. The stronger the blur effect, the greater the change in the value of each pixel relative to its origin, so it makes our pencil sketch clearer. The kernel number of the first picture is 25 and that of the second picture is 75. (understand the beauty of division here)

Import cv2img=cv2.imread ('bird.jpg') cv2.imshow (' bird',img) gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) # Blur the image using GaussianBlur # Gaussian kernel obeys normal distribution. The larger the kernel number, the more blurred gray_blur = cv2.GaussianBlur (gray, (75, 75), 0) # Convert the image into pencil sketchcartoon = cv2.divide (gray, gray_blur, scale=250.0) cv2.imshow ('cartoon.jpg',cartoon) cv2.waitKey () cv2.destroyAllWindows () traditional Chinese painting effect

Chinese painting effect: sharpen the image, smooth the color and enhance the edge effect.

1. The edge image of the original image after median filtering and adaptive mean filtering.

two。 Sharpen the original RGB image based on the detail enhancement filter in figure 1 below. (when the detail enhancement filter is replaced with a bilateral filter, it appears smoother, figure 2 below)

3. The sharpened image is bitwise with the edge image and the final effect image is obtained.

Import cv2img=cv2.imread ('bird.jpg') cv2.imshow (' bird',img) # convert the image into grayscale imagegray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) # Blur the grayscale image with median blur median filter gray _ blur = cv2.medianBlur (gray, 3) # Apply adaptive thresholding to detect edges detect image edge edges = cv2.adaptiveThreshold (gray_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9 9) # Adaptive mean filter # Sharpen the image sharpened image color = cv2.detailEnhance (img, sigma_s=5, sigma_r=0.5) cv2.imshow ('color',color) # detail enhancement function sigma_s controls the size of the neighborhood The size of the neighborhood will be weighted to replace the pixel value in the image. The higher the value, the larger the neighborhood. This makes the image smoother #: this is important if you want to preserve the edges when smoothing the image. Smaller values will only result in very similar colors to be averaged (that is, smoothed), while very different colors will remain unchanged # Merge the colors of same images using "edges" as a maskcartoon = cv2.bitwise_and (color, color, mask=edges) cv2.imshow ('cartoon',cartoon) cv2.waitKey () cv2.destroyAllWindows () abstract effect

Laplace filter: highlight the edge of the object through the grayscale of the object and the background intensity of the image

Import cv2img=cv2.imread ('bird.jpg') gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) cv2.imshow (' img.jpg', img) gray = cv2.medianBlur (gray, 3) edges = cv2.Laplacian (gray,-1, ksize=5) edges_inv = 255edgesdummy, cartoon = cv2.threshold (edges_inv, 0,255, cv2.THRESH_BINARY) cv2.imshow ('cartoon.jpg', cartoon) cv2.waitKey () cv2.destroyAllWindows () here This article "Opencv how to achieve the cartoon effect of images" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report