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

Combining colors and outlines to produce a cartoon

The last step is to combine the two previously achieved effects. Simply fuse the two effects together into a single image using cv2.bitwise_and. The complete function is as follows:

def cartoonize(rgb_image, *,
num_pyr_downs=2, num_bilaterals=7):
# STEP 1 -- Apply a bilateral filter to reduce the color palette of
# the image.
downsampled_img = rgb_image
for _ in range(num_pyr_downs):
downsampled_img = cv2.pyrDown(downsampled_img)

for _ in range(num_bilaterals):
filterd_small_img = cv2.bilateralFilter(downsampled_img, 9, 9, 7)

filtered_normal_img = filterd_small_img
for _ in range(num_pyr_downs):
filtered_normal_img = cv2.pyrUp(filtered_normal_img)

# make sure resulting image has the same dims as original
if filtered_normal_img.shape != rgb_image.shape:
filtered_normal_img = cv2.resize(
filtered_normal_img, rgb_image.shape[:2])

# STEP 2 -- Convert the original color image into grayscale.
img_gray = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2GRAY)
# STEP 3 -- Apply amedian blur to reduce image noise.
img_blur = cv2.medianBlur(img_gray, 7)

# STEP 4 -- Use adaptive thresholding to detect and emphasize the edges
# in an edge mask.
gray_edges = cv2.adaptiveThreshold(img_blur, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 9, 2)
# STEP 5 -- Combine the color image from step 1 with the edge mask
# from step 4.
rgb_edges = cv2.cvtColor(gray_edges, cv2.COLOR_GRAY2RGB)
return cv2.bitwise_and(filtered_normal_img, rgb_edges)

The result looks like this:

In the next section, we'll set up the main script and design a GUI application.

主站蜘蛛池模板: 布尔津县| 吴旗县| 柘荣县| 双桥区| 临泽县| 教育| 墨江| 琼结县| 灵川县| 南昌市| 北票市| 万荣县| 榆林市| 恩施市| 松原市| 庄浪县| 惠东县| 凤城市| 岗巴县| 三台县| 固安县| 苍山县| 荃湾区| 岱山县| 平利县| 通许县| 太原市| 保靖县| 迭部县| 磐安县| 钦州市| 香港| 秦安县| 玉溪市| 海伦市| 安塞县| 红河县| 壶关县| 苍山县| 茂名市| 若尔盖县|