Input : Input image Is; Image size W and H; Defect area ratio range sl and sh; Defect bright range bl and bh; shape ratio r1; blur ratio r2. Output: Defective image Ig. 1: while True do 2: xr←Rand(sl, sh)×W, yr←Rand (sl, sh)×H; 3: θ ←Uniform_sample(0, 2×pi); 4: cx←Rand(max(xr, yr), W-max(xr, yr)); 5: cy←Rand(max(xr, yr), H-max(xr, yr)); 6: contour←Gentratete_ellipse(cy, cx, yr, xr, θ); 7: if r1 < 0 then 8: Imask← Gentratete_mask(Rand(bl, bh), r2, Is, contour); 9: else 10: P1←Sample N points from contour; 11: P2←Adjust_contour_shape(P1, r1); 12: new_contour←Interpolate(P2); 13: Imask← Gentratete_mask(Rand(bl, bh), r2, Is, new_contour); 14: end 15: Ig← Add_img( Is, Imask); 16: return Ig. 17: end上面论文的算法流程转换成python代码
时间: 2023-08-18 18:05:06 浏览: 87
以下是将论文中的算法流程转换成 Python 代码的实现:
```python
import cv2
import numpy as np
import random
from math import pi
from scipy.interpolate import interp1d
def generate_defective_image(image, defect_area_range, defect_bright_range, shape_ratio, blur_ratio):
h, w = image.shape[:2]
while True:
xr = random.uniform(defect_area_range[0], defect_area_range[1]) * w
yr = random.uniform(defect_area_range[0], defect_area_range[1]) * h
theta = random.uniform(0, 2*pi)
cx = random.uniform(max(xr, yr), w - max(xr, yr))
cy = random.uniform(max(xr, yr), h - max(xr, yr))
contour = generate_ellipse_contour(cy, cx, yr, xr, theta)
if shape_ratio < 0:
mask = generate_mask(random.uniform(defect_bright_range[0], defect_bright_range[1]), blur_ratio, image, contour)
else:
p1 = sample_n_points(contour, len(contour)//4)
p2 = adjust_contour_shape(p1, shape_ratio)
new_contour = interpolate(p2)
mask = generate_mask(random.uniform(defect_bright_range[0], defect_bright_range[1]), blur_ratio, image, new_contour)
defective_image = add_image(image, mask)
return defective_image
def generate_ellipse_contour(cy, cx, yr, xr, theta):
cos_theta = np.cos(theta)
sin_theta = np.sin(theta)
t = np.linspace(0, 2*pi, 100)
x = cx + xr*np.cos(t)*cos_theta - yr*np.sin(t)*sin_theta
y = cy + xr*np.cos(t)*sin_theta + yr*np.sin(t)*cos_theta
contour = np.stack([x, y], axis=1).astype(np.int32)
return contour
def generate_mask(brightness, blur_ratio, image, contour):
mask = np.zeros(image.shape[:2], dtype=np.uint8)
cv2.drawContours(mask, [contour], -1, 255, -1)
mask = np.float32(mask) / 255
mask = cv2.GaussianBlur(mask, (0, 0), blur_ratio*image.shape[1])
mask = np.clip(mask * brightness, 0, 255)
mask = np.uint8(mask)
return mask
def add_image(image, mask):
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
image = image.astype(np.float32)
mask = mask.astype(np.float32) / 255
added = cv2.addWeighted(image, 1, mask, 0.8, 0)
return np.clip(added, 0, 255).astype(np.uint8)
def sample_n_points(contour, n):
indices = np.arange(0, len(contour))
np.random.shuffle(indices)
indices = indices[:n]
indices = sorted(indices)
return contour[indices]
def adjust_contour_shape(p1, shape_ratio):
p2 = p1.copy()
for i in range(1, len(p2)):
vec = p2[i] - p2[i-1]
vec *= shape_ratio
p2[i] += vec
return p2
def interpolate(points):
t = np.arange(len(points))
interp_func = interp1d(t, points, axis=0, kind='cubic')
t_interp = np.linspace(0, len(points) - 1, len(points) * 10)
return interp_func(t_interp).astype(np.int32)
```
其中,`generate_defective_image` 函数是整个算法的入口,它接受一个原始图像 `image`,以及一些参数,返回一个缺陷图像。具体参数如下:
- `defect_area_range`:缺陷面积占比范围,是一个二元组,表示缺陷面积与原图面积的比例,取值范围为 [0, 1]。
- `defect_bright_range`:缺陷亮度范围,是一个二元组,表示缺陷亮度的最小值和最大值,取值范围为 [0, 255]。
- `shape_ratio`:缺陷形状比例,表示缺陷的形状与原始椭圆形状的差异程度,取值范围为 [-1, 1]。当取值为负数时,表示不需要调整形状。
- `blur_ratio`:模糊程度,表示高斯模糊核大小与原图宽度的比例,取值范围为 [0, 1]。
除此之外,还有一些辅助函数:
- `generate_ellipse_contour`:生成椭圆轮廓。
- `generate_mask`:根据轮廓生成掩码,控制缺陷的亮度和模糊程度。
- `add_image`:将原始图像和缺陷图像加权相加,得到最终的缺陷图像。
- `sample_n_points`:从轮廓中随机采样 n 个点。
- `adjust_contour_shape`:根据 `shape_ratio` 调整轮廓形状。
- `interpolate`:对调整后的轮廓进行插值,得到更平滑的轮廓。
阅读全文