From 130f1bf05a0bee47b54d7a00211325c81f702a4b Mon Sep 17 00:00:00 2001 From: "A.Star" Date: Thu, 26 Jul 2018 11:21:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: A.Star --- README.md | 37 --------------- README.rst | 3 ++ requirements | 5 ++ setup.py | 47 +++++++++++++++++++ snowland/__init__.py | 14 ++++++ snowland/image/__init__.py | 13 ++++++ snowland/image/api/__init__.py | 11 +++++ snowland/image/api/img_to_cartoon.py | 52 +++++++++++++++++++++ snowland/image/color/__init__.py | 11 +++++ snowland/image/color/color.py | 68 ++++++++++++++++++++++++++++ snowland/test/test.py | 19 ++++++++ 11 files changed, 243 insertions(+), 37 deletions(-) delete mode 100644 README.md create mode 100644 README.rst create mode 100644 requirements create mode 100644 setup.py create mode 100644 snowland/__init__.py create mode 100644 snowland/image/__init__.py create mode 100644 snowland/image/api/__init__.py create mode 100644 snowland/image/api/img_to_cartoon.py create mode 100644 snowland/image/color/__init__.py create mode 100644 snowland/image/color/color.py create mode 100644 snowland/test/test.py diff --git a/README.md b/README.md deleted file mode 100644 index a98f405..0000000 --- a/README.md +++ /dev/null @@ -1,37 +0,0 @@ -# snowland-img2cartoon - -#### 项目介绍 -python 生成卡通风格的照片 - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本项目 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 码云特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目 -5. 码云官方提供的使用手册 [http://git.mydoc.io/](http://git.mydoc.io/) -6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) \ No newline at end of file diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..169ca53 --- /dev/null +++ b/README.rst @@ -0,0 +1,3 @@ +====== +scikit-snowland +====== diff --git a/requirements b/requirements new file mode 100644 index 0000000..fd3c442 --- /dev/null +++ b/requirements @@ -0,0 +1,5 @@ +opencv-python==3.4.1.15 +numpy>=1.0.0 +scikit-image>=0.13 +matplotlib>=2.1.2 +wand>=0.4.4 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..ca268d2 --- /dev/null +++ b/setup.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author : 河北雪域网络科技有限公司 A.Star +# @contact: astar@snowland.ltd +# @site: +# @file: setup.py +# @time: 2018/7/11 15:13 +# @Software: PyCharm + + +from setuptools import setup, find_packages +from snowland import __version__ +setup( + name='scikit-snowland', + version=__version__, + description=( + 'scikit tool for image ' + ), + long_description=open('README.rst').read(), + author='A.Star', + author_email='astar@snowland.ltd', + maintainer='A.Star', + maintainer_email='astar@snowland.ltd', + license='BSD License', + packages=find_packages(), + platforms=["all"], + url='https://gitee.com/hoops/snowland-img2cartoon', + classifiers=[ + 'Development Status :: 4 - Beta', + 'Operating System :: OS Independent', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: BSD License', + 'Programming Language :: Python', + 'Programming Language :: Python :: Implementation', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Topic :: Software Development :: Libraries' + ], + install_requires=[ + 'opencv-python>=3.4.1.15', + 'numpy>=1.0.0', + 'scikit-image>=0.13', + 'matplotlib>=2.1.2' + ] +) diff --git a/snowland/__init__.py b/snowland/__init__.py new file mode 100644 index 0000000..f207a5e --- /dev/null +++ b/snowland/__init__.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author : 河北雪域网络科技有限公司 A.Star +# @contact: astar@snowland.ltd +# @site: +# @file: __init__.py.py +# @time: 2018/7/26 10:30 +# @Software: PyCharm + +__version__ = '0.1.0' + + +if __name__ == '__main__': + pass \ No newline at end of file diff --git a/snowland/image/__init__.py b/snowland/image/__init__.py new file mode 100644 index 0000000..2c9e902 --- /dev/null +++ b/snowland/image/__init__.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author : 河北雪域网络科技有限公司 A.Star +# @contact: astar@snowland.ltd +# @site: +# @file: __init__.py.py +# @time: 2018/7/26 10:31 +# @Software: PyCharm + + +from .color import * +from .api import * +from .thirdparty import * \ No newline at end of file diff --git a/snowland/image/api/__init__.py b/snowland/image/api/__init__.py new file mode 100644 index 0000000..994ef2b --- /dev/null +++ b/snowland/image/api/__init__.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author : 河北雪域网络科技有限公司 A.Star +# @contact: astar@snowland.ltd +# @site: +# @file: __init__.py.py +# @time: 2018/7/26 10:32 +# @Software: PyCharm + + +from .img_to_cartoon import cartoonise \ No newline at end of file diff --git a/snowland/image/api/img_to_cartoon.py b/snowland/image/api/img_to_cartoon.py new file mode 100644 index 0000000..e7a5f2e --- /dev/null +++ b/snowland/image/api/img_to_cartoon.py @@ -0,0 +1,52 @@ +# !/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author : 河北雪域网络科技有限公司 A.Star +# @contact: astar@snowland.ltd +# @site: +# @file: main.py +# @time: 2018/7/11 14:34 +# @Software: PyCharm + +import cv2 + + +def cartoonise(img_rgb, num_down=2, num_bilateral=7): + """ + :param img_rgb: 输入图像 + :param num_down: 缩减像素采样的数目 + :param num_bilateral: 定义双边滤波的数目 + :return: + """ + # 用高斯金字塔降低取样 + img_color = img_rgb + for _ in range(num_down): + img_color = cv2.pyrDown(img_color) + # 重复使用小的双边滤波代替一个大的滤波 + for _ in range(num_bilateral): + img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7) + # 升采样图片到原始大小 + for _ in range(num_down): + img_color = cv2.pyrUp(img_color) + # 转换为灰度并且使其产生中等的模糊 + img_gray = cv2.cvtColor(img_color, cv2.COLOR_RGB2GRAY) + img_blur = cv2.medianBlur(img_gray, 7) + # 检测到边缘并且增强其效果 + img_edge = cv2.adaptiveThreshold(img_blur, 255, + cv2.ADAPTIVE_THRESH_MEAN_C, + cv2.THRESH_BINARY, + blockSize=9, + C=2) + # 转换回彩色图像 + img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB) + img_cartoon = cv2.bitwise_and(img_color, img_edge) + return img_cartoon + + +if __name__ == '__main__': + from skimage.io import imread, imshow + from skimage.data import chelsea + from matplotlib import pylab as plt + img = chelsea() + out = cartoonise(img) + plt.imshow(out) + plt.show() diff --git a/snowland/image/color/__init__.py b/snowland/image/color/__init__.py new file mode 100644 index 0000000..cdc6606 --- /dev/null +++ b/snowland/image/color/__init__.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author : 河北雪域网络科技有限公司 A.Star +# @contact: astar@snowland.ltd +# @site: +# @file: __init__.py.py +# @time: 2018/7/26 10:31 +# @Software: PyCharm + + +from .color import ycbcr2rgb, rgb2ycbcr \ No newline at end of file diff --git a/snowland/image/color/color.py b/snowland/image/color/color.py new file mode 100644 index 0000000..4b72614 --- /dev/null +++ b/snowland/image/color/color.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author : 河北雪域网络科技有限公司 A.Star +# @contact: astar@snowland.ltd +# @site: +# @file: color.py +# @time: 2018/7/26 0:24 +# @Software: PyCharm + +import numpy as np +npa = np.array + +def rgb2ycbcr(img): + origT = npa([[65.481, 128.553, 24.966], [-37.797, -74.203, 112], [112, -93.786, -18.214]]) + oriOffset = npa([[16], [128], [128]]) + + if img.dtype.name == 'uint8': + t = 1 + offset = 1.0 / 255 + elif img.dtype.name == 'float64': + t = 1.0 / 255 + offset = 1.0 / 255 + elif img.dtype.name == 'uint16': + t = 257.0 / 65535 + offset = 257 + + T = origT * t + Offset = oriOffset * offset + + ycbcr = np.zeros(img.shape, dtype=img.dtype) + for p in range(3): + ycbcr[:, :, p] = T[p, 0] * img[:, :, 0] + T[p, 1] * img[:, :, 1] + T[p, 2] * img[:, :, 2] + Offset[p] + + return ycbcr + + +def ycbcr2rgb(img): + origT = npa([[65.481, 128.553, 24.966], [-37.797, -74.203, 112], [112, -93.786, -18.214]]) + oriOffset = npa([[16], [128], [128]]) + tinv = np.linalg.inv(origT) + if img.dtype.name == 'uint8': + t = 255 + offset = 255 + elif img.dtype.name == 'float64': + t = 255 + offset = 1 + elif img.dtype.name == 'uint16': + t = 65535 / 257.0 + offset = 65535 + + T = tinv * t + Offset = offset * tinv.dot(oriOffset) + + rgb = np.zeros(img.shape, dtype=img.dtype) + + for p in range(3): + rgb[:, :, p] = T[p, 0] * img[:, :, 0] + T[p, 1] * img[:, :, 1] + T[p, 2] * img[:, :, 2] - Offset[p] + + if img.dtype.name == 'float64': + rgb[rgb > 1] = 1 + rgb[rgb < 0] = 0 + return rgb + +if __name__ == '__main__': + from skimage.io import imread + img = imread('1.jpg') + img2 = rgb2ycbcr(img) + img1 = ycbcr2rgb(img2) diff --git a/snowland/test/test.py b/snowland/test/test.py new file mode 100644 index 0000000..c023af4 --- /dev/null +++ b/snowland/test/test.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author : 河北雪域网络科技有限公司 A.Star +# @contact: astar@snowland.ltd +# @site: +# @file: test.py +# @time: 2018/7/26 10:38 +# @Software: PyCharm + + +if __name__ == '__main__': + from snowland.image.api import cartoonise + from skimage.io import imread, imshow + from skimage.data import chelsea + from matplotlib import pylab as plt + img = chelsea() + out = cartoonise(img) + plt.imshow(out) + plt.show()