This is my 14th day of the August Genwen Challenge

preface

Cartoonization of photos using Python and some image manipulation techniques, let’s get started

The development tools

Python version: 3.6.4

Related modules:

Cv2 module;

Numpy module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Introduction of the principle

Plan 1

Step1:

The original image is denoised by edge preserving with bilateral filter.

The code is as follows:

Step1: Edge preserving and denoising of the original image using a bilateral filter
	img_bilateral = img
	# the sampling
	for _ in range(2):
		img_bilateral = cv2.pyrDown(img_bilateral)
	# Perform multiple bilateral filtering
	# Parameter meaning (in order): original image, neighborhood diameter, spatial Gaussian function standard deviation, gray value similarity standard deviation
	for _ in range(7):
		img_bilateral = cv2.bilateralFilter(img_bilateral, d=9, sigmaColor=9, sigmaSpace=7)
	# on the sample
	for _ in range(2):
		img_bilateral = cv2.pyrUp(img_bilateral)
Copy the code

The effect is as follows:

Step2:

After graying the image obtained in step 1, a median filter is used to denoise the image.

The code is as follows:

	Step2: After graying the image obtained in Step 1, use the median filter to denoise.
	img_gray = cv2.cvtColor(img_bilateral, cv2.COLOR_RGB2GRAY)
	img_median = cv2.medianBlur(img_gray, 7)
Copy the code

The effect is as follows:

Step3:

The contour of the original image is obtained by using adaptive threshold value for the image obtained in Step 2.

The code is as follows:

Step3: apply the adaptive threshold value to the image obtained in step 2 to obtain the contour of the original imageParameter meaning (in order): Original image, pixel upper limit, adaptive method, assignment method, neighborhood size, constant IMg_edge = cv2.adaptiveThreshold(img_median, 255, cv2.adaptive_thresh_mean_c, Img_edge = cv2.cvtcolor (img_edge, cv2.color_gray2rgb)Copy the code

The effect is as follows:

Step4:

Merge the image obtained in step 1 with the image contour obtained in Step 3 to achieve the effect of turning the photo into a cartoon image.

The code is as follows:

	Step4: merge the image obtained in step 1 with the image contour obtained in step 3 to achieve the effect of turning the photo into a cartoon picture.
	img_cartoon = cv2.bitwise_and(img_bilateral, img_edge)
	cv2.imwrite(os.path.join(savepath, savename), img_cartoon)
	# cv2.imshow('cartoonise', img_cartoon)
	# cv2.waitKey(0)
	# cv2.destroyAllWindows()
Copy the code

The effect is as follows:

Scheme 2

Step1:

Images were transferred from BGR space to HSV space, and histogram equalization, median filtering and morphological transformation were performed in HSV space.

The code is as follows:

	Step 1: Image BGR space is transformed into HSV space, histogram equalization, median filtering and morphological transformation are performed in HSV space.
	img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
	h, s, v = cv2.split(img_hsv)
	# Histogram equalization
	v = cv2.equalizeHist(v)
	img_hsv = cv2.merge((h, s, v))
	# Median filtering
	img_hsv = cv2.medianBlur(img_hsv, 7)
	Morphologic transform - open/close operation
	kernel = np.ones((5.5), np.uint8)
	# img_hsv = cv2.morphologyEx(img_hsv, cv2.MORPH_OPEN, kernel, iterations=3)
	img_hsv = cv2.morphologyEx(img_hsv, cv2.MORPH_CLOSE, kernel, iterations=2)
	# Median filtering
	img_hsv = cv2.medianBlur(img_hsv, 7)
Copy the code

The effect is as follows:

Step2:

The contour of the original image is obtained by using the adaptive threshold value for the image obtained in step 1.

The code is as follows:

	Step 2: Obtain the contour of the original image by applying adaptive thresholds to the image obtained in Step 1.
	img_mask = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)
	img_mask = cv2.cvtColor(img_mask, cv2.COLOR_RGB2GRAY)
	img_mask = cv2.medianBlur(img_mask, 7)
	img_edge = cv2.adaptiveThreshold(img_mask, 
									 255,
									 cv2.ADAPTIVE_THRESH_MEAN_C,
									 cv2.THRESH_BINARY,
									 blockSize=9,
									 C=2)
	img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
Copy the code

The effect is as follows:

Step3:

The image contour obtained in Step 2 is merged with the original image to achieve the effect of turning the photo into a cartoon image.

The code is as follows:

	Step 3: Merge the image contour obtained in Step 2 with the original image to achieve the effect of turning the photo into a cartoon picture.
	img_cartoon = cv2.bitwise_and(img, img_edge)
	cv2.imwrite(os.path.join(savepath, savename), img_cartoon)
Copy the code

The effect is as follows:

The effect of a closed operation looks like this:

That’s the end of the article, thanks for watching, next share Python implements a wave of scratchcard effects.

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

③ project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)

⑤ A Roadmap for Learning Python

All done~ See personal homepage introduction to obtain the complete source code.