是否可以在 python 中的图像上创建随机形状?

Is it possible to create a random shape on an image in python?

我需要在图像上创建一些斑点。这些斑点是不规则形状的(主要是我试图添加一个大圆圈,然后尝试在大圆圈的边缘添加较小的圆圈,使其成为“不规则”的圆形)。这里我只是在例子中展示了一个圆圈。 因为我有一个充满图像的目录,所以每个图像的大小、位置和点数都需要不同。这里给出了我尝试过的方法:

import glob
import cv2
import numpy as np
import random


count = 0
cv_img = []
for img in glob.glob('Lenna_(test_image).png'):
    n = cv2.imread(img)
    for i in range(random.randint(1, 5)):
        
        c1 = random.randrange(75,200,1)
        c2 = random.randrange(70,350,1)
        r1 = random.randint(8,18)
        
        n1_img = cv2.circle(n,(c1,c2),(r1),(255,255,255),-1, lineType = 4)
        
        
        cv_img.append(n1_img)
        cv2.imwrite('result.png',n1_img)
    count = count+1 

This is the image I get

但我想补充一点。我是用油漆做的。 This is the thing I want to add on the image

这是在 Python/OpenCV/Numpy/Skimage 中生成随机形状的一种方法。

  • 定义随机数生成器的种子值(以更改形状的模式)
  • 定义输出宽度和高度
  • 创建随机噪声图像
  • 模糊噪声图像
  • 将模糊图像拉伸到全动态范围
  • 对拉伸图像设置阈值
  • 应用形态学清理阈值图像
  • 保存结果

import cv2
import skimage.exposure
import numpy as np
from numpy.random import default_rng

# define random seed to change the pattern
seedval = 55
rng = default_rng(seed=seedval)

# define image size
width=500
height=500

# create random noise image
noise = rng.integers(0, 255, (height,width), np.uint8, True)

# blur the noise image to control the size
blur = cv2.GaussianBlur(noise, (0,0), sigmaX=15, sigmaY=15, borderType = cv2.BORDER_DEFAULT)

# stretch the blurred image to full dynamic range
stretch = skimage.exposure.rescale_intensity(blur, in_range='image', out_range=(0,255)).astype(np.uint8)

# threshold stretched image to control the size
thresh = cv2.threshold(stretch, 175, 255, cv2.THRESH_BINARY)[1]

# apply morphology open and close to smooth out shapes
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
result = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
result = cv2.morphologyEx(result, cv2.MORPH_CLOSE, kernel)

# save result
cv2.imwrite('random_blobs.png', result)

# show results
cv2.imshow('noise', noise)
cv2.imshow('blur', blur)
cv2.imshow('stretch', stretch)
cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

您现在可以拍摄此图像并将其添加到背景图像中,以在背景上插入白色斑点。

这是将带有黑色边框的白色斑点添加到 Python/OpenCV 中的某些图像的完整代码。

输入:

import cv2
import skimage.exposure
import numpy as np
from numpy.random import default_rng

# read input image
img = cv2.imread('lena.jpg')
height, width = img.shape[:2]

# define random seed to change the pattern
seedval = 75
rng = default_rng(seed=seedval)

# create random noise image
noise = rng.integers(0, 255, (height,width), np.uint8, True)

# blur the noise image to control the size
blur = cv2.GaussianBlur(noise, (0,0), sigmaX=15, sigmaY=15, borderType = cv2.BORDER_DEFAULT)

# stretch the blurred image to full dynamic range
stretch = skimage.exposure.rescale_intensity(blur, in_range='image', out_range=(0,255)).astype(np.uint8)

# threshold stretched image to control the size
thresh = cv2.threshold(stretch, 175, 255, cv2.THRESH_BINARY)[1]

# apply morphology open and close to smooth out and make 3 channels
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
mask = cv2.merge([mask,mask,mask])

# add mask to input
result1 = cv2.add(img, mask)

# use canny edge detection on mask
edges = cv2.Canny(mask,50,255)

# thicken edges and make 3 channel
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
edges = cv2.morphologyEx(edges, cv2.MORPH_DILATE, kernel)
edges = cv2.merge([edges,edges,edges])

# merge edges with result1 (make black in result where edges are white)
result2 = result1.copy()
result2[np.where((edges == [255,255,255]).all(axis=2))] = [0,0,0]

# add noise to result where mask is white
noise = cv2.merge([noise,noise,noise])
result3 = result2.copy()
result3 = np.where(mask==(255,255,255), noise, result3)

# save result
cv2.imwrite('lena_random_blobs1.jpg', result1)
cv2.imwrite('lena_random_blobs2.jpg', result2)
cv2.imwrite('lena_random_blobs3.jpg', result3)

# show results
cv2.imshow('noise', noise)
cv2.imshow('blur', blur)
cv2.imshow('stretch', stretch)
cv2.imshow('thresh', thresh)
cv2.imshow('mask', mask)
cv2.imshow('edges', edges)
cv2.imshow('result1', result1)
cv2.imshow('result2', result2)
cv2.imshow('result3', result3)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果 1(白色斑点):

结果 2(带黑色边框的白色斑点):

结果 3(带有黑色边框的噪声斑点):