gpt4 book ai didi

python - 如何使用 OpenCV 生成类似纸张的背景

转载 作者:太空狗 更新时间:2023-10-30 01:18:50 25 4
gpt4 key购买 nike

我正在尝试使用 OpenCV 实现一个随机的类似纸的背景。附件示例展示了真实的纸质文件伪影(请忽略上面的手写)。 enter image description here

简单的生纸效果,只要加噪点就可以轻松实现

import cv2
import numpy as np

BG_COLOR = 209

def blank_image(width=1024, height=1024):
img = np.full((height, width, 1), BG_COLOR, np.uint8)
return img

def noisy(image):
row, col, ch = image.shape
mean = 0
sigma = 10
gauss = np.random.normal(mean, sigma, (row, col, ch))
gauss = gauss.reshape(row, col, ch)
noisy = gauss + image
return noisy

if __name__ == '__main__':
img = blank_image()
cv2.imwrite('out.jpg', noisy(img))

但上面没有人工制品,它看起来太统一了: enter image description here

我想知道从第一张图片生成这种随机结构的最佳方法是什么。

最佳答案

受BoboDarph提供的解决方案的启发,我创建了一个类似纸的纹理。现在我需要从上面的真实照片中添加人工制品。代码如下:

import cv2
import numpy as np


BG_COLOR = 209
BG_SIGMA = 5
MONOCHROME = 1


def blank_image(width=1024, height=1024, background=BG_COLOR):
"""
It creates a blank image of the given background color
"""
img = np.full((height, width, MONOCHROME), background, np.uint8)
return img


def add_noise(img, sigma=BG_SIGMA):
"""
Adds noise to the existing image
"""
width, height, ch = img.shape
n = noise(width, height, sigma=sigma)
img = img + n
return img.clip(0, 255)


def noise(width, height, ratio=1, sigma=BG_SIGMA):
"""
The function generates an image, filled with gaussian nose. If ratio parameter is specified,
noise will be generated for a lesser image and then it will be upscaled to the original size.
In that case noise will generate larger square patterns. To avoid multiple lines, the upscale
uses interpolation.

:param ratio: the size of generated noise "pixels"
:param sigma: defines bounds of noise fluctuations
"""
mean = 0
assert width % ratio == 0, "Can't scale image with of size {} and ratio {}".format(width, ratio)
assert height % ratio == 0, "Can't scale image with of size {} and ratio {}".format(height, ratio)

h = int(height / ratio)
w = int(width / ratio)

result = np.random.normal(mean, sigma, (w, h, MONOCHROME))
if ratio > 1:
result = cv2.resize(result, dsize=(width, height), interpolation=cv2.INTER_LINEAR)
return result.reshape((width, height, MONOCHROME))


def texture(image, sigma=BG_SIGMA, turbulence=2):
"""
Consequently applies noise patterns to the original image from big to small.

sigma: defines bounds of noise fluctuations
turbulence: defines how quickly big patterns will be replaced with the small ones. The lower
value - the more iterations will be performed during texture generation.
"""
result = image.astype(float)
cols, rows, ch = image.shape
ratio = cols
while not ratio == 1:
result += noise(cols, rows, ratio, sigma=sigma)
ratio = (ratio // turbulence) or 1
cut = np.clip(result, 0, 255)
return cut.astype(np.uint8)


if __name__ == '__main__':
cv2.imwrite('texture.jpg', texture(blank_image(background=230), sigma=4, turbulence=4))
cv2.imwrite('texture-and-noise.jpg', add_noise(texture(blank_image(background=230), sigma=4), sigma=10))

cv2.imwrite('noise.jpg', add_noise(blank_image(1024, 1024), sigma=10))

结果图片: enter image description here

关于python - 如何使用 OpenCV 生成类似纸张的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51646185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com