褪色图像的着色
介绍
在本文中,我们将使用 Python 制作一个ML(机器学习)模型,该模型将能够为旧的、褪色的图像着色。本文将实现使用机器学习对图像进行着色的目标。
我们要做的是将灰度图像输入模型,并且模型的输出必须是着色后的图像,例如,如果我们输入西班牙猎犬的灰度图像,我们就不应该得到一只粉红色的狗作为输出。我们将使用CNN(卷积神经网络)给照片上色。
着色在深度学习中的工作原理:
来源:http://richzhang.github.io/colorization/
我们将不使用传统的 RGB 颜色空间,而是使用“ Lab Space of colours ”。由于我们将只处理灰度图像作为输入,我们只能坚持使用 L 空间。
在这个特定的空间中,我们可以进一步使用它来预测a和b的结果。这样,最终的ab图像就可以组合起来得到所需的输出。最后,我们将把Lab图像转换为RGB格式。
数据集
现在我们已经了解了项目的基本工作流程,让我们继续进行下去。
加载数据集
将数据集加载到你选择的编辑器目录中,或者你也可以将其存储在任何位置并复制该特定目录的路径;两者都可以。我将跳过这些部分并将此数据集加载部分留给你,因为它非常基础。你可以下载你选择的任何数据集。
请记住一件事,数据集越大,模型的训练效果就越好,但随之而来的是模型需要更长时间的训练。
数据集可以从这里下载:https://pyimagesearch.com/2019/02/25/black-and-white-image-colorization-with-opencv-and-deep-learning/
在下载数据集时,你会看到它包含两个 python 脚本 bw2color_image 和 bw2color_video。在本文中,我们将只处理图像部分。
为灰度图像添加颜色
现在让我们为灰度图像添加颜色。
我们将把我们的代码库添加到 bw2color_image.py 文件中。
导入必要的依赖项:
import numpy as np
import argparse
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str, required=True,
help="path to input black and white image")
ap.add_argument("-p", "--prototxt", type=str, required=True,
help="path to Caffe prototxt file")
ap.add_argument("-m", "--model", type=str, required=True,
help="path to Caffe pre-trained model")
ap.add_argument("-c", "--points", type=str, required=True,
help="path to cluster center points")
args = vars(ap.parse_args())
现在让我们添加 CLI 参数,以便将来如果我们想针对不同的自定义输入测试我们的模型,我们可以轻松完成。
加载模型和集群
print("Loading the model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
pts = np.load(args["points"])
class8 = net.getLayerId("class8_ab")
conv8 = net.getLayerId("conv8_313_rh")
pts = pts.transpose().reshape(2, 313, 1, 1)
net.getLayer(class8).blobs = [pts.astype("float32")]
net.getLayer(conv8).blobs = [np.full([1, 313], 2.606, dtype="float32")]
cv2.dnn.readNetFromCaffe() 函数直接将 CLI 中的值读取到 Caffe 模型中。
然后我们为 ab 通道加载中心。
加载和转换图像(预处理)
image = cv2.imread(args["image"])
scaled = image.astype("float32") / 255.0
lab = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)
我们将像素强度从 0 缩放到 1,并将 BGR 转换为 Lab Space
我们现在要将图像大小调整为 224 x 224,这是网络尺寸所需的输入。我们还将每个点作为 1×1 卷积处理,以进一步添加到我们的模型中。
resized = cv2.resize(lab, (224, 224))
L = cv2.split(resized)[0]
L -= 50
我们在这里所做的是,取 L 通道并对其执行“平均减法”。
现在我们可以将“L”作为输入传递并预测“ab”通道。
'print("Colorizing image...")'
net.set input(cv2.dnn.blobFromImage(L))
ab = net.forward()[0, :, :, :].transpose((1, 2, 0))
ab = cv2.resize(ab, (image.shape[1], image.shape[0])
一些小的后期处理
L = cv2.split(lab)[0]
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
colorized = np.clip(colorized, 0, 1)
# the current colorized image is represented as a floating point
# data type in the range [0, 1] -- let's convert to an unsigned
# 8-bit integer representation in the range [0, 255]
colorized = (255 * colorized).astype("uint8")
# show the original and output colorized images
cv2.imshow("Original", image)
cv2.imshow("Colorized", colorized)
cv2.waitKey(0)
我们上面所做的是,去掉任何可能在 0 到 1 范围之外的像素值。我们还把强度范围从 0 调整到255
检查我们模型的结果
现在在终端中,你可以尝试运行我们刚刚编写的脚本——bw2color-image.py。
你可以使用以下命令:
python bw2color_image.py
--prototxt model/colorization_deploy_v2.prototxt
--model model/colorization_release_v2.caffemodel
--points model/pts_in_hull.npy
--image images/robin_williams.jpg
输出结果是:
同样,我们可以通过在 CLI 中调整和自定义路径来对各种灰度图像执行相同的操作。
你可以观察到显影图像中也存在一些灰色区域。但是该模型的性能足以展示项目的功能。
结论
在本文中,我们看到了如何使用 OpenCV 和深度学习的一些概念对图像进行着色。使用这个特定的模型,我们可以在很大程度上将灰度图像的输入着色为彩色图像。
我们还可以将图像着色的概念应用到视频着色项目中。我们所要做的就是使用VideoStream读取视频帧和时间,在相机模块启动时暂停代码库一段时间。
可以通过使用更大的数据集来改进模型来对其进行训练,但这是有代价的,即模型需要更多的时间来训练。
但请注意你使用的数据集。如果考虑使用具有多种数据的数据集,模型可能会过拟合,最终给出不正确的结果。
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。