PS 中 图像->黑白 算法实现(python版)

PS 中 图像->黑白 算法实现(python版)


详细内容就不解释了,代码有注释, 另外在网上看了很多代码,很多版本,就不做评价了。给自己留个记录的位置和有需要的有缘人。

示例图:

在这里插入图片描述

#!/usr/bin/python3
# coding: utf-8
# @Time: 2021/1/15 13:11
# @Project: ObjectDetection
# @File: BlackWhite.py
# @Author: Lpliner
# @Description:import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import ioclass BlackWhite(object):def __init__(self, filename, red=.4, yellow=.6, green=.4, cyan=.6, blue=.2, magenta=.8):"""ps中图像->黑白: python版, 及各种颜色在灰度图片中的占比红色, 黄色, 绿色, 青色, 蓝色, 洋红Args:filename: 修改的文件路径,abspathred:  红色占比,参数范围[-2.0, 3.0]yellow: 黄红色占比,参数范围[-2.0, 3.0]green: 绿红色占比,参数范围[-2.0, 3.0]cyan: 青红色占比,参数范围[-2.0, 3.0]blue: 蓝色占比,参数范围[-2.0, 3.0]magenta: 洋红色占比,参数范围[-2.0, 3.0]"""self.filename = filenameself.red = redself.yellow = yellowself.green = greenself.cyan = cyanself.blue = blueself.magenta = magentadef __image_read(self, cv=True):"""read image fileReturns:"""if cv:# opencvimg = cv2.imread(self.filename)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)else:# scikit-imageimg = io.imread(self.filename)return imgdef __transform(self):"""调整图像数据黑白度Returns:"""image = self.__image_read()image = image * 1.0  # 将int数据类型转换成floatcolor_ratio = np.array([self.red, self.yellow, self.green, self.cyan, self.blue, self.magenta])# 读取到的image时三维数据,所以取值为axis=2image_max_value = image.max(axis=2)image_min_value = image.min(axis=2)image_sum_value = image.sum(axis=2)image_mid_value = image_sum_value - image_max_value - image_min_value# 添加蒙版ratio_max_midmask_red = 1 - ((image[:, :, 0] - image_max_value + 0.01) < 0)mask_green = 1 - ((image[:, :, 1] - image_max_value + 0.01) < 0)mask_blue = 1 - ((image[:, :, 2] - image_max_value + 0.01) < 0)ratio_max_mid = mask_red * color_ratio[3] + mask_green * color_ratio[5] + mask_blue * color_ratio[1]# 添加蒙版ratio_maxmask_red = 1 - ((image[:, :, 0] - image_min_value - 0.01) > 0)mask_green = 1 - ((image[:, :, 1] - image_min_value - 0.01) > 0)mask_blue = 1 - ((image[:, :, 2] - image_min_value - 0.01) > 0)ratio_max = mask_red * color_ratio[4] + mask_green * color_ratio[0] + mask_blue * color_ratio[2]# output = image_max_value * 1.0# gray algorithm# gray = (max - mid) * ratio_max + (mid - min) * ratio_max_mid + minoutput = (image_max_value - image_mid_value) * ratio_max + (image_mid_value - image_min_value) * ratio_max_mid + image_min_valuereturn outputdef save(self, image, filename):"""将图片数据另存为目标地址Args:image: 图片数据np.ndarrayfilename: 存为文件位置Returns:"""cv2.imwrite(filename, image)def show(self, image):"""显示图片, 此处使用plt绘图,可以更换opencv展示Args:image: 图片数据Returns:"""plt.figure(2)plt.imshow(image / 255.0, plt.cm.gray)plt.axis('off')plt.show()def __call__(self, *args, **kwargs):image = self.__transform()# self.save(image, kwargs.get("saveDir"))self.show(image)if __name__ == '__main__':filename = "./test1.jpg"BlackWhite(filename,red=2.,yellow=-1.,green=-1.,cyan=-1.,blue=-1.,magenta=1.)()


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部