图像弹性变换算法的实现

编程入门 行业动态 更新时间:2024-10-09 16:24:48

图像弹性变换<a href=https://www.elefans.com/category/jswz/34/1770096.html style=算法的实现"/>

图像弹性变换算法的实现

原文: 

 

我只是实现了Python版本的该算法

from PIL import Image, ImageFilter
import numpy as np
import math
import osdef augment(old_file, new_file):# 打开图片# image = Image.open(r'F:\TJ AI-Food\test.png')image = Image.open(old_file)# 创建一个相等大小的空白图作为结果result = Image.new('RGB', (image.width, image.height), (255, 255, 255))# 创建一个跟图片大小相等的空白图片(1通道)arr = np.random.rand(image.height, image.width) * 200delta_x = Image.fromarray(arr, mode='RGB')arr = np.random.rand(image.height, image.width) * 200delta_y = Image.fromarray(arr, mode='RGB')# 对delta_x和delta_y进行高斯模糊delta_x = delta_x.filter(ImageFilter.GaussianBlur(radius=4))delta_y = delta_y.filter(ImageFilter.GaussianBlur(radius=4))# 像素平移for i in range(image.width):for j in range(image.height):x_low = i + math.floor((delta_x.getpixel((i, j))[0] / 256 * 2 - 1) * 20)x_high = i + math.ceil((delta_x.getpixel((i, j))[0] / 256 * 2 - 1) * 20)y_low = j + math.floor((delta_y.getpixel((i, j))[0] / 256 * 2 - 1) * 20)y_high = j + math.ceil((delta_y.getpixel((i, j))[0] / 256 * 2 - 1) * 20)def bound(value, max_value):if value < 0: return 0if value >= max_value: return max_value - 1return valuex_low = bound(x_low, image.width)x_high = bound(x_high, image.width)y_low = bound(y_low, image.height)y_high = bound(y_high, image.height)new_pixel = (int((image.getpixel((x_low, y_low))[0] + image.getpixel((x_low, y_high))[0] + image.getpixel((x_high, y_low))[0] + image.getpixel((x_high, y_high))[0]) / 4),int((image.getpixel((x_low, y_low))[1] + image.getpixel((x_low, y_high))[1] + image.getpixel((x_high, y_low))[1] + image.getpixel((x_high, y_high))[1]) / 4),int((image.getpixel((x_low, y_low))[2] + image.getpixel((x_low, y_high))[2] + image.getpixel((x_high, y_low))[2] + image.getpixel((x_high, y_high))[2]) / 4))sum_val = (bound(new_pixel[0], 256), bound(new_pixel[1], 256), bound(new_pixel[2], 256))# put pixel at (i, j)result.putpixel((i, j), sum_val)result.save(new_file)

 

更多推荐

图像弹性变换算法的实现

本文发布于:2024-02-07 03:09:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753052.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   弹性   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!