Python中用什么方法实现图像的颜色迁移效果
Admin 2022-06-27 群英技术资讯 338 次浏览
reinhard算法:Color Transfer between Images,作者Erik Reinhard
welsh算法:Transferring Color to Greyscale Images,作者Tomihisa Welsh
人像图换肤色,风景图颜色迁移
def color_trans_reinhard(in_img, ref_img, in_mask_lists=[None], ref_mask_lists=[None]): ref_img_lab = cv2.cvtColor(ref_img, cv2.COLOR_BGR2LAB) in_img_lab = cv2.cvtColor(in_img, cv2.COLOR_BGR2LAB) in_avg = np.ones(in_img.shape, np.float32) in_std = np.ones(in_img.shape, np.float32) ref_avg = np.ones(in_img.shape, np.float32) ref_std = np.ones(in_img.shape, np.float32) mask_all = np.zeros(in_img.shape, np.float32) for in_mask, ref_mask in zip(in_mask_lists, ref_mask_lists): #mask,取值为 0, 255, shape[height,width] in_avg_tmp, in_std_tmp = cv2.meanStdDev(in_img_lab, mask=in_mask) np.copyto(in_avg, in_avg_tmp.reshape(1,1,-1), where=np.expand_dims(in_mask,2)!=0) #numpy.copyto(destination, source) np.copyto(in_std, in_std_tmp.reshape(1,1,-1), where=np.expand_dims(in_mask,2)!=0) ref_avg_tmp, ref_std_tmp = cv2.meanStdDev(ref_img_lab, mask=ref_mask) np.copyto(ref_avg, ref_avg_tmp.reshape(1,1,-1), where=np.expand_dims(in_mask,2)!=0) #numpy.copyto(destination, source) np.copyto(ref_std, ref_std_tmp.reshape(1,1,-1), where=np.expand_dims(in_mask,2)!=0) #mask mask_all[in_mask!=0] = 1 in_std[in_std==0] =1 #避免除数为0的情况 transfered_lab = (in_img_lab - in_avg)/(in_std) *ref_std + ref_avg transfered_lab[transfered_lab<0] = 0 transfered_lab[transfered_lab>255] = 255 out_img = cv2.cvtColor(transfered_lab.astype(np.uint8), cv2.COLOR_LAB2BGR) if in_mask_lists[0] is not None and ref_mask_lists[0] is not None: np.copyto(out_img, in_img, where=mask_all==0) return out_img """ #img1 = cv2.imread("imgs/1.png") #img2 = cv2.imread("imgs/2.png") #img1 = cv2.imread("welsh22/1.png", 1) #img2 = cv2.imread("welsh22/2.png", 1) img1 = cv2.imread("welsh22/gray.jpg", 1) img2 = cv2.imread("welsh22/consult.jpg", 1) cv2.imwrite("out.jpg", color_trans_reinhard(img1, img2, [np.ones(img1.shape[:-1],np.uint8)*255], [np.ones(img2.shape[:-1],np.uint8)*255])) """ img1 = cv2.imread("ab.jpeg") img2 = cv2.imread("hsy.jpeg") mask1 = cv2.imread("ab_parsing.jpg", 0) mask1[mask1<128]=0 mask1[mask1>=128]=255 mask2 = cv2.imread("hsy_parsing.jpg", 0) mask2[mask2<128]=0 mask2[mask2>=128]=255 cv2.imwrite("out.jpg", color_trans_reinhard(img1, img2, [mask1], [mask2]))
改进点
def get_domain_std(img_l, pixel, height, width, window_size): window_left = max(pixel[1] - window_size, 0) window_right = min(pixel[1] + window_size + 1, width) window_top = max(pixel[0] - window_size, 0) window_bottom = min(pixel[0] + window_size + 1, height) window_slice = img_l[window_top: window_bottom, window_left: window_right] return np.std(window_slice) def get_weight_pixel(ref_img_l, ref_img_a, ref_img_b, ref_img_height, ref_img_width, segment, window_size, ratio, ref_mask_lists=[None]): weight_list = [] pixel_a_list = [] pixel_b_list = [] ref_img_mask = np.ones((ref_img_height, ref_img_width), np.uint8) if ref_mask_lists[0] is not None: for x in ref_mask_lists: ref_img_mask = np.bitwise_or(x, ref_img_mask) ref_img_l_mean = cv2.blur(ref_img_l, (window_size, window_size)) ref_img_l_std = np.sqrt(cv2.blur(np.power((ref_img_l - ref_img_l_mean), 2), (window_size, window_size))) for _ in range(segment): height_index = np.random.randint(ref_img_height) width_index = np.random.randint(ref_img_width) pixel = [height_index, width_index] #[x,y] if ref_img_mask[pixel[0], pixel[1]] == 0: continue pixel_light = ref_img_l[pixel[0], pixel[1]] pixel_a = ref_img_a[pixel[0], pixel[1]] pixel_b = ref_img_b[pixel[0], pixel[1]] #pixel_std = get_domain_std(ref_img_l, pixel, ref_img_height, ref_img_width, window_size) pixel_std = ref_img_l_std[height_index, width_index] weight_value = int(pixel_light * ratio + pixel_std * (1 - ratio)) if weight_value not in weight_list: weight_list.append(weight_value) pixel_a_list.append(pixel_a) pixel_b_list.append(pixel_b) return np.array(weight_list), np.array(pixel_a_list), np.array(pixel_b_list) def color_trans_welsh(in_img, ref_img, in_mask_lists=[None], ref_mask_lists=[None]): start = time.time() #参考图 ref_img_height, ref_img_width, ref_img_channel = ref_img.shape window_size=5 #窗口大小 segment= 10000#随机点个数 ratio=0.5 #求weight的比例系数 ref_img_lab = cv2.cvtColor(ref_img, cv2.COLOR_BGR2Lab) ref_img_l, ref_img_a, ref_img_b = cv2.split(ref_img_lab) #计算参考图weight ref_img_weight_array, ref_img_pixel_a_array, ref_img_pixel_b_array = get_weight_pixel(ref_img_l, ref_img_a, ref_img_b, ref_img_height, ref_img_width, segment, window_size, ratio, ref_mask_lists) ref_img_max_pixel, ref_img_min_pixel = np.max(ref_img_l), np.min(ref_img_l) #输入图 in_img_height, in_img_width, in_img_channel = in_img.shape in_img_lab = cv2.cvtColor(in_img, cv2.COLOR_BGR2LAB) # 获取灰度图像的亮度信息; in_img_l, in_img_a, in_img_b = cv2.split(in_img_lab) in_img_max_pixel, in_img_min_pixel = np.max(in_img_l), np.min(in_img_l) pixel_ratio = (ref_img_max_pixel - ref_img_min_pixel) / (in_img_max_pixel - in_img_min_pixel) # 把输入图像的亮度值映射到参考图像范围内; in_img_l = ref_img_min_pixel + (in_img_l - in_img_min_pixel) * pixel_ratio in_img_l = in_img_l.astype(np.uint8) in_img_l_mean = cv2.blur(in_img_l, (window_size, window_size)) in_img_l_std = np.sqrt(cv2.blur(np.power((in_img_l - in_img_l_mean), 2), (window_size, window_size))) in_img_weight_pixel = ratio * in_img_l + (1 - ratio) * in_img_l_std nearest_pixel_index = np.argmin(np.abs(ref_img_weight_array.reshape(1,1,-1) - np.expand_dims(in_img_weight_pixel, 2)), axis=2).astype(np.float32) in_img_a = cv2.remap(ref_img_pixel_a_array.reshape(1, -1), nearest_pixel_index, np.zeros_like(nearest_pixel_index, np.float32), interpolation=cv2.INTER_LINEAR) in_img_b = cv2.remap(ref_img_pixel_b_array.reshape(1, -1), nearest_pixel_index, np.zeros_like(nearest_pixel_index, np.float32), interpolation=cv2.INTER_LINEAR) merge_img = cv2.merge([in_img_l, in_img_a, in_img_b]) bgr_img = cv2.cvtColor(merge_img, cv2.COLOR_LAB2BGR) mask_all = np.zeros(in_img.shape[:-1], np.int32) if in_mask_lists[0] is not None and ref_mask_lists[0] is not None: for x in in_mask_lists: mask_all = np.bitwise_or(x, mask_all) mask_all = cv2.merge([mask_all, mask_all, mask_all]) np.copyto(bgr_img, in_img, where=mask_all==0) end = time.time() print("time", end-start) return bgr_img if __name__ == '__main__': # 创建参考图像的分析类; #ref_img = cv2.imread("consult.jpg") #ref_img = cv2.imread("2.png") ref_img = cv2.imread("../imgs/2.png") # 读取灰度图像;opencv默认读取的是3通道的,不需要我们扩展通道; #in_img = cv2.imread("gray.jpg") #in_img = cv2.imread("1.png") in_img = cv2.imread("../imgs/1.png") bgr_img = color_trans_welsh(in_img, ref_img) cv2.imwrite("out_ren.jpg", bgr_img) """ ref_img = cv2.imread("../hsy.jpeg") ref_mask = cv2.imread("../hsy_parsing.jpg", 0) ref_mask[ref_mask<128] = 0 ref_mask[ref_mask>=128] = 255 in_img = cv2.imread("../ab.jpeg") in_mask = cv2.imread("../ab_parsing.jpg", 0) in_mask[in_mask<128] = 0 in_mask[in_mask>=128] = 255 bgr_img = color_trans_welsh(in_img, ref_img, in_mask_lists=[in_mask], ref_mask_lists=[ref_mask]) cv2.imwrite("bgr.jpg", bgr_img) """
从左到右,分别为原图,参考图,reinhard效果,welsh效果
从左到右,分别为原图,原图皮肤mask,参考图,参考图皮肤mask,reinhard效果,welsh效果
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
pathlib 是Python内置库,Python 文档给它的定义是 Object-oriented filesystem paths(面向对象的文件系统路径)。pathlib 提供表示文件系统路径的类,其语义适用于不同的操作系统。
这篇文章主要介绍了Python中schedule模块定时任务的使用方法,文章基于上一篇文章的内容展开的后续,需要的朋友可以参考一下
所谓定时器,是指间隔特定时间执行特定任务的机制。几乎所有的编程语言,都有定时器的实现。这篇文章主要介绍的是通过Python实现的定时器,感兴趣的可以跟随小编学习一下
写了个python opencv的小demo,可以通过键盘按下字母s进行采集图像。
python open()方法用于打开一个文件,并返回文件对象,在对文件处理的过程中都需要用到这个函数,如果文件无法打开,会抛出OSError。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008