How to add stripe noise to an image? (Matlab, Python, C++)
I want to put artificial noise in the image, what should I do? You want to put stripes at regular pixel 开发者_如何学Gointervals, as shown in the figure below.
I wrote the code below to put black stripes on one line for every three lines
for i = 1:512
counter = counter + 1;
if counter > 3
counter = 0;
end
for j = 1:512
if counter < 3
img{i,j} = 255;
elseif counter == 3
img{i,j} = 0;
end
end
end
fr = imread("image.bmp")
sum = fr + img;
I thought I could simply add the stripe arrangement and image made like this. However, the pixel that should be zero value was not created because the pixel value of the original image was included. The result was added only white stripes with a value of 0.
Please let me know if there is a function or code to put stripe noise in the mat wrap.
What you can do is simply create stripes -PIL and then Alpha composting.
so the way of approch here is
- first create stripes
- Then overlay images
I think I seen functions to remove stripe noises in images...But, Never any functionality approch to add stripe noise.
Python
opencv - 4.5.5
Like so.
#create stripes
from PIL import Image, ImageDraw
img = Image.new('RGB', (100, 100), (255, 255, 255))
draw = ImageDraw.Draw(img)
for y in range(10, 91, 20):
draw.line((100, y, 0, y), (0, 0, 0), 10)
img.save('stripes.png')
####Overlay
import cv2
background = cv2.imread(r"bird.png", cv2.IMREAD_UNCHANGED)
foreground = cv2.imread(r"stripes.png", cv2.IMREAD_UNCHANGED)
height, width, channels = background.shape
foreground = cv2.resize(foreground, (width, height))
# normalize alpha channels from 0-255 to 0-1
alpha_background = background[:,:,2] / 255.0
alpha_foreground = foreground[:,:,2] / 255.0
# set adjusted colors
for color in range(0, 3):
background[:,:,color] = alpha_foreground * foreground[:,:,color] + \
alpha_background * background[:,:,color] * (1 - alpha_foreground)
# set adjusted alpha and denormalize back to 0-255
background[:,:,2] = (1 - (1 - alpha_foreground) * (1 - alpha_background)) * 255
# display the image
cv2.imshow("Composited image", background)
cv2.waitKey(0)
some sample insights.
Background image
After noise
精彩评论