当前位置: 首页 > wzjs >正文

做机网站北京商场skp

做机网站,北京商场skp,广东网站建设公司哪家好,将wordpress压缩包解压至一个空文件夹_并上传它图像扭曲增强处理流程 ├── 1. 初始化与加载 │ ├── 读取输入图像 │ ├── 检查图像有效性 │ ├── 提取文件名和扩展名 │ └── 获取图像尺寸信息 │ ├── 2. 扭曲变换方法 │ ├── 弹性变换(Elastic Transform) │ │ ├…
图像扭曲增强处理流程
├── 1. 初始化与加载
│   ├── 读取输入图像
│   ├── 检查图像有效性
│   ├── 提取文件名和扩展名
│   └── 获取图像尺寸信息
│
├── 2. 扭曲变换方法
│   ├── 弹性变换(Elastic Transform)
│   │   ├── 生成随机仿射变换矩阵
│   │   ├── 创建复杂位移场(添加随机和周期性扰动)
│   │   └── 应用非线性扭曲
│   │
│   ├── 透视变换(Perspective Transform)
│   │   ├── 随机选择原图四个角点
│   │   ├── 计算透视变换矩阵(增强极端变换概率)
│   │   └── 应用透视扭曲
│   │
│   └── 仿射变换(Affine Transform)
│       ├── 随机缩放(0.4-2.2倍)
│       ├── 随机旋转(-70°至70°)
│       └── 随机剪切(-50°至50°)
│
├── 3. 裁剪与缩放
│   ├── 随机选择缩放因子(1.0-2.5倍原始尺寸)
│   ├── 若图像尺寸不足则直接缩放
│   └── 随机裁剪到目标尺寸
│
├── 4. 随机变换组合
│   ├── 高概率选择弹性变换(50%)
│   ├── 中概率选择透视变换(30%)
│   └── 低概率选择仿射变换(20%)
│
└── 5. 批量生成与保存├── 创建输出目录├── 循环生成指定数量的扭曲图像├── 保存图像到指定路径└── 每100张输出一次进度信息

安装好包,修改好文件路径后,可直接使用的代码展示:

import cv2
import numpy as np
import os
import random
from typing import Tuple, List, Callable, Dict#扭曲更强,图片范围1-2倍
class ImageDistortion:def __init__(self, input_path: str, output_dir: str):"""初始化图像扭曲增强器参数:input_path: 输入图像路径output_dir: 输出目录路径"""self.input_path = input_pathself.output_dir = output_dirself.image = self._load_image()self.filename, self.ext = os.path.splitext(os.path.basename(input_path))self.height, self.width = self.image.shape[:2]def _load_image(self) -> np.ndarray:"""加载输入图像"""image = cv2.imread(self.input_path)if image is None:raise FileNotFoundError(f"无法加载图像: {self.input_path}")return imagedef elastic_transform(self, alpha: float = 200, sigma: float = 8,alpha_affine: float = 20, random_state: np.random.RandomState = None) -> np.ndarray:"""应用增强的弹性变换参数:alpha: 位移场强度sigma: 位移场平滑度alpha_affine: 仿射变换强度random_state: 随机状态返回:变换后的图像"""if random_state is None:random_state = np.random.RandomState(None)shape = self.image.shapeshape_size = shape[:2]# 增强弹性变换效果center_square = np.float32(shape_size) // 2square_size = min(shape_size) // 3# 随机仿射变换pts1 = np.float32([center_square + square_size,[center_square[0] + square_size, center_square[1] - square_size],center_square - square_size])pts2 = pts1 + random_state.uniform(-alpha_affine, alpha_affine, size=pts1.shape).astype(np.float32)M = cv2.getAffineTransform(pts1, pts2)image = cv2.warpAffine(self.image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)# 创建更复杂的位移场dx = cv2.GaussianBlur((random_state.rand(*shape_size) * 2 - 1), (0, 0), sigma) * alphady = cv2.GaussianBlur((random_state.rand(*shape_size) * 2 - 1), (0, 0), sigma) * alpha# 添加周期性扰动增强扭曲效果grid_size = random_state.randint(2, 6)x, y = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))# 添加更强的正弦波扰动wave_strength = random_state.uniform(10, 30)wave_freq = random_state.uniform(0.01, 0.07)dx += np.sin(x * wave_freq) * wave_strengthdy += np.cos(y * wave_freq) * wave_strength# 添加多个频率的波if random.random() < 0.5:wave_strength2 = random_state.uniform(5, 15)wave_freq2 = random_state.uniform(0.03, 0.1)dx += np.sin(y * wave_freq2) * wave_strength2dy += np.cos(x * wave_freq2) * wave_strength2# 创建网格map_x = np.float32(x + dx)map_y = np.float32(y + dy)# 应用变换return cv2.remap(image, map_x, map_y, interpolation=cv2.INTER_LINEAR,borderMode=cv2.BORDER_REFLECT_101)def perspective_transform(self, scale: float = 0.3, tilt_prob: float = 0.7) -> np.ndarray:"""应用增强的透视变换参数:scale: 变换比例tilt_prob: 倾斜概率返回:变换后的图像"""height, width = self.image.shape[:2]# 原图四个角点pts1 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])# 增加极端透视变换的可能性if random.random() < tilt_prob:# 强烈倾斜变换pts2 = np.float32([[random.uniform(0, width * scale), random.uniform(0, height * scale)],[width - random.uniform(0, width * scale), random.uniform(0, height * scale)],[random.uniform(0, width * scale), height - random.uniform(0, height * scale)],[width - random.uniform(0, width * scale), height - random.uniform(0, height * scale)]])else:# 常规透视变换pts2 = np.float32([[random.uniform(0, width * scale * 0.7), random.uniform(0, height * scale * 0.7)],[width - random.uniform(0, width * scale * 0.7), random.uniform(0, height * scale * 0.7)],[random.uniform(0, width * scale * 0.7), height - random.uniform(0, height * scale * 0.7)],[width - random.uniform(0, width * scale * 0.7), height - random.uniform(0, height * scale * 0.7)]])# 计算透视变换矩阵matrix = cv2.getPerspectiveTransform(pts1, pts2)# 应用变换return cv2.warpPerspective(self.image, matrix, (width, height),borderMode=cv2.BORDER_REFLECT_101,flags=cv2.INTER_CUBIC)def affine_transform(self, scale_range: Tuple[float, float] = (0.5, 2.0),rotation_range: Tuple[float, float] = (-60, 60),shear_range: Tuple[float, float] = (-45, 45)) -> np.ndarray:"""应用增强的仿射变换参数:scale_range: 缩放范围rotation_range: 旋转角度范围shear_range: 剪切角度范围返回:变换后的图像"""height, width = self.image.shape[:2]# 随机选择变换参数scale = random.uniform(*scale_range)rotation = random.uniform(*rotation_range)shear_x = random.uniform(*shear_range)shear_y = random.uniform(*shear_range)# 计算旋转矩阵rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), rotation, scale)# 应用旋转变换rotated = cv2.warpAffine(self.image, rotation_matrix, (width, height),borderMode=cv2.BORDER_REFLECT_101,flags=cv2.INTER_CUBIC)# 应用剪切变换shear_matrix = np.float32([[1, shear_x / 100, 0],[shear_y / 100, 1, 0]])return cv2.warpAffine(rotated, shear_matrix, (width, height),borderMode=cv2.BORDER_REFLECT_101,flags=cv2.INTER_CUBIC)def crop_and_resize(self, image: np.ndarray, scale_factor: float = None) -> np.ndarray:"""裁剪并调整图像大小,使最终图像尺寸在原始尺寸的1倍到2.5倍之间参数:image: 输入图像scale_factor: 缩放因子,如果为None则随机选择返回:裁剪并调整大小后的图像"""if scale_factor is None:# 在1.0到2.5倍之间随机选择缩放因子scale_factor = random.uniform(1.0, 2.5)h, w = image.shape[:2]# 计算目标尺寸target_h = int(self.height * scale_factor)target_w = int(self.width * scale_factor)# 如果图像尺寸小于目标尺寸,直接调整大小if h < target_h or w < target_w:return cv2.resize(image, (target_w, target_h), interpolation=cv2.INTER_CUBIC)# 随机裁剪y = random.randint(0, h - target_h)x = random.randint(0, w - target_w)cropped = image[y:y + target_h, x:x + target_w]return croppeddef apply_random_distortion(self) -> np.ndarray:"""应用随机选择的扭曲变换返回:变换后的图像"""# 增加弹性变换的概率,因其效果更丰富distortion_methods = ['elastic'] * 5 + ['perspective'] * 3 + ['affine'] * 2distortion_method = random.choice(distortion_methods)if distortion_method == 'elastic':# 随机调整弹性变换参数alpha = random.uniform(150, 250)sigma = random.uniform(5, 10)alpha_affine = random.uniform(15, 30)distorted = self.elastic_transform(alpha, sigma, alpha_affine)elif distortion_method == 'perspective':# 随机调整透视变换参数scale = random.uniform(0.2, 0.4)tilt_prob = random.uniform(0.6, 0.9)distorted = self.perspective_transform(scale, tilt_prob)else:  # affine# 随机调整仿射变换参数scale_range = (random.uniform(0.4, 0.8), random.uniform(1.5, 2.2))rotation_range = (random.uniform(-70, -30), random.uniform(30, 70))shear_range = (random.uniform(-50, -20), random.uniform(20, 50))distorted = self.affine_transform(scale_range, rotation_range, shear_range)# 应用裁剪和缩放return self.crop_and_resize(distorted)def generate_and_save(self, count: int) -> None:"""生成指定数量的扭曲图像并保存到输出目录参数:count: 要生成的图像数量"""# 确保输出目录存在os.makedirs(self.output_dir, exist_ok=True)print(f"开始生成 {count} 张扭曲图像...")for i in range(count):# 应用随机扭曲distorted_image = self.apply_random_distortion()# 构建输出文件名output_filename = f"{self.filename}_distorted_{i:04d}{self.ext}"output_path = os.path.join(self.output_dir, output_filename)# 保存图像cv2.imwrite(output_path, distorted_image)# 每生成100张图像打印一次进度if (i + 1) % 100 == 0:print(f"已生成 {i + 1}/{count} 张图像")print(f"所有 {count} 张扭曲图像已保存到: {self.output_dir}")def main():# 输入图像路径input_image_path = r"E:\project1\photo01\0_defect_1.bmp"# 确保路径中的目录分隔符正确input_image_path = input_image_path.replace('\\', '/')# 输出目录(与输入图像在同一目录)output_directory = os.path.dirname(input_image_path)# 创建图像扭曲增强器实例enhancer = ImageDistortion(input_image_path, output_directory)# 生成并保存3000张扭曲图像enhancer.generate_and_save(3000)if __name__ == "__main__":main()


文章转载自:

http://yhMK62Py.xfLwq.cn
http://W2jtUUTJ.xfLwq.cn
http://WEwLfzQD.xfLwq.cn
http://7dc6Dfbe.xfLwq.cn
http://CpHon2Th.xfLwq.cn
http://MNicUpt1.xfLwq.cn
http://4PIhQKbF.xfLwq.cn
http://FFrVKZ3X.xfLwq.cn
http://CBnk9ZEC.xfLwq.cn
http://n0XSoHTe.xfLwq.cn
http://vFe8PDHq.xfLwq.cn
http://XRrEc0k5.xfLwq.cn
http://T8oPr4dj.xfLwq.cn
http://t1o1YF15.xfLwq.cn
http://w3YIzxCc.xfLwq.cn
http://wWd5ZBRN.xfLwq.cn
http://xVMnNOC6.xfLwq.cn
http://MLTba7gv.xfLwq.cn
http://Wh8MYt1i.xfLwq.cn
http://V04cDTXz.xfLwq.cn
http://Vm3MGKfd.xfLwq.cn
http://r9vMAWXD.xfLwq.cn
http://oN2VIQ6K.xfLwq.cn
http://nbUNKq28.xfLwq.cn
http://ZrOnZccb.xfLwq.cn
http://J0TQeIO8.xfLwq.cn
http://po2ePWGK.xfLwq.cn
http://8Xx7OfAQ.xfLwq.cn
http://NE3iNWZx.xfLwq.cn
http://J7T0LaiK.xfLwq.cn
http://www.dtcms.com/wzjs/743421.html

相关文章:

  • 攀枝花住房和城乡建设厅官方网站emlog文章转wordpress
  • 怎么在网站后台做图片新闻网站设计的概述
  • 网站设计公司哪家好h5页面制作网站
  • 网站运营工作具体做啥深圳网站建设提供服务公司
  • 网站开发业务好做吗深圳市建设工程造价信息
  • 丰台网站建设联系方式如何上传织梦做的网站
  • 网站建设的条件分析企业文化经典句子
  • 平台网站开发是什么意思东莞百姓网免费发布信息网
  • 广州天河区做网站的公司郑州网站建设白杨网络
  • 中山网站建设哪家强2345网址导航官网
  • 杭州临安网站建设公司管理系统数据库
  • 接网站建设 网站设计黄山建设网站公司电话
  • 织梦网站图标怎么开设网站 优帮云
  • 第三方网站系统建设WordPress登录效果
  • 信誉好的大良网站建设小程序模板下载了怎么用
  • 电子商务的网站设计龙华网站建设的公司
  • 公司的网站建设注意点wordpress模板文件介绍
  • 曲阜住房和城乡建设局网站有没有可以做app的网站
  • 网站域名注册哪个好小游戏网站怎么做
  • 国外做健康的网站小困网络科技泰安有限公司
  • 免费行情软件网站下载ww开发移动网站
  • 东莞网站优化电话上传网站教程
  • 旅游项目网站开发网站内容建设包括什么
  • 备案 网站错了宠物用品wordpress模板
  • 网站备案帐号无法安装wordpress
  • 网站开发收费标准网站安全 代码
  • 湛江模板建站定制网站海淀网站建设联系方式
  • 企业做网站要注意些什么问题电脑版浏览器在线使用
  • 亚马逊站外deal网站开发平台为用户提供了哪四类应用开发
  • 沧州网站建设费用网站文件下载系统