Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add Gaussian noise or Salt and Pepper noise to the Image by Python

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "Python how to achieve the image to add Gaussian noise or salt and pepper noise", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn about "Python how to add Gaussian noise or salt and pepper noise to the image" this article.

Code with noise (Gaussian noise, salt and pepper noise)

Add_noise.py

# noisef in the code is the signal level. For example, I need 0.7 noise. I passed the parameter 1-0.7from PIL import Imageimport numpy as npimport randomimport torchvision.transforms as transformsnorm_mean = (0.5,0.5,0.5) norm_std = (0.5,0.5,0.5) class AddPepperNoise (object): "" increase the salt and pepper noise Args: snr (float): Signal Noise Rate p (float): probability value Perform the operation according to probability "" def _ init__ (self, snr, pause 0.9): assert isinstance (snr, float) and (isinstance (p, float)) # 2020 07 26 or-> and self.snr = snr self.p = p def _ call__ (self) Img): "" Args: img (PIL Image): PIL Image Returns: PIL Image: PIL image. " If random.uniform (0,1) < self.p: img_ = np.array (img). Copy () h, w, c = img_.shape signal_pct = self.snr noise_pct = (1-self.snr) mask = np.random.choice ((0,1,2), size= (h, w, 1), p = [signal _ pct, noise_pct/2., noise_pct/2.]) Mask = np.repeat (mask, c Axis=2) img_ [mask = = 1] = 255# salt noise img_ [mask = = 2] = 0 # pepper noise return Image.fromarray (img_.astype ('uint8')). Convert (' RGB') else: return imgclass Gaussian_noise (object): "increase Gaussian noise this function adds the resulting Gaussian noise Input: img: original image mean: mean sigma: standard deviation return: gaussian_out: noise processed image "" def _ _ init__ (self) Mean, sigma): self.mean = mean self.sigma = sigma def _ _ call__ (self, img): "" Args: img (PIL Image): PIL Image Returns: PIL Image: PIL image. "" # standardize the grayscale of the picture img_ = np.array (img). Copy () img_ = img_ / 255.0 # generate Gaussian noise noise = np.random.normal (self.mean, self.sigma, img_.shape) # overlay noise and picture gaussian_out = img_ + noise # set more than 1 Set 0 gaussian_out = np.clip (gaussian_out, 0) below 0 1) # restore the gray range of the image to 0255 gaussian_out = np.uint8 (gaussian_out*255) # make the noise range 0255 # noise = np.uint8 (noise*255) return Image.fromarray (gaussian_out) .convert ('RGB') def image_transform (noisef): "pre-convert the images in the training set and the test set Train_transform: noisy image _ train_transform: original image (no noise) test_transform: test pattern (no noise) "" train_transform = transforms.Compose ([transforms.Resize ((256) # resize # transforms.RandomCrop (32), AddPepperNoise (noisef, pendant 0.9), # add salt and pepper noise # Gaussian_noise (0, noisef), # add Gaussian noise transforms.ToTensor (), # convert to tensor # transforms.Normalize (norm_mean,norm_std) ]) _ train_transform = transforms.Compose ([transforms.Resize ((256,256)), # transforms.RandomCrop (32 transforms.Normalize paddingroom4), transforms.ToTensor (), # transforms.Normalize (norm_mean,norm_std),]) test_transform = transforms.Compose ([transforms.Resize ((256,256)), # transforms.RandomCrop (32paddingroom4), transforms.ToTensor () # transforms.Normalize (norm_mean,norm_std),]) how to use return train_transform, _ train_transform, test_transform in pytorch to transform and noise train_transform into noisy images _ train_transform is the original image and test_transform is the test chart noisef. The noise level train_transform,_train_transform,test_transform = image_transform (noisef) training_data=FabricDataset_file (data_dir=train_dir,transform=train_transform) _ training_data=FabricDataset_file (data_dir=_train_dir,transform=_train_transform) testing_data=FabricDataset_file (data_dir=test_dir,transform=test_transform) is all the contents of the article "how to add Gaussian noise or salt and pepper noise to an image by Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report