├── Images ├── Psnr.png ├── Bird_sample.png ├── sheep_sample.png ├── Sunset_sample.png └── universal_sample.png ├── README.md └── noise_reduction.py /Images/Psnr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LaveenaParwani/Video-and-Image-Noise-Reduction/HEAD/Images/Psnr.png -------------------------------------------------------------------------------- /Images/Bird_sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LaveenaParwani/Video-and-Image-Noise-Reduction/HEAD/Images/Bird_sample.png -------------------------------------------------------------------------------- /Images/sheep_sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LaveenaParwani/Video-and-Image-Noise-Reduction/HEAD/Images/sheep_sample.png -------------------------------------------------------------------------------- /Images/Sunset_sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LaveenaParwani/Video-and-Image-Noise-Reduction/HEAD/Images/Sunset_sample.png -------------------------------------------------------------------------------- /Images/universal_sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LaveenaParwani/Video-and-Image-Noise-Reduction/HEAD/Images/universal_sample.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Video and Image-Noise-Reduction 2 | 3 | Performing Video and Image Noise Reduction using OpenCV. 4 | 5 | In this project, we are denoising the Video clip or Image using OpenCV. 6 | 7 | The Noised image and Denoised image after applying the reduction are: 8 | 9 | ![](Images/universal_sample.png) 10 | 11 | Denoising of an image refers to the process of reconstruction of a signal from noisy images. Denoising is done to remove unwanted noise from image to analyze it in better form. 12 | 13 | The final PSNR values(Peak signal-to-noise ratio) to frames output are: 14 | 15 | ![](Images/Psnr.png) 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /noise_reduction.py: -------------------------------------------------------------------------------- 1 | #complete code for working video 2 | import cv2 #for image operations 3 | import numpy as np #for image stored in array 4 | from matplotlib import pyplot as plt #for plotting the graph 5 | import math #for math iopoerations 6 | import glob #to access the files from folder 7 | import os #for file or folder handling 8 | import shutil #to remove the folder with files 9 | 10 | 11 | 12 | videopath=input("Enter any Path====") 13 | print("Your Path is===",videopath) 14 | #break video into frames with noisy frames and creating folder 15 | path1="C:\\PSNR\\frame_with_noise" 16 | if os.path.exists(path1) and os.path.isdir(path1): 17 | shutil.rmtree(path1) 18 | print("Delete") 19 | if not os.path.isfile(path1): 20 | os.makedirs(path1) 21 | print("Create") 22 | 23 | vidcap1 = cv2.VideoCapture(videopath) #capturing video 24 | success,image1 = vidcap1.read()#Read the video 25 | # Capture frame-by-frame 26 | count = 0 27 | success = True 28 | while success: 29 | 30 | cv2.imwrite("%s\\imgN%d.jpg" %(path1,count), image1) # save frame as JPEG file 31 | vidcap1.set(cv2.CAP_PROP_POS_MSEC,(count)) #used to hold speed of frame generation 32 | #Current position of the video file in milliseconds 33 | 34 | success,image1 = vidcap1.read() 35 | print ('Read a new frame: ', success) 36 | count += 1 37 | if(count==200): 38 | break 39 | print("the total frames=",count) 40 | cv2.waitKey(0) 41 | 42 | #------------------------------without noise frames conversion---------------- 43 | 44 | #break video into frames with noise removal with filter 45 | path2="C:\\PSNR\\frame_without_noise" 46 | if os.path.exists(path2) and os.path.isdir(path2): 47 | shutil.rmtree(path2) 48 | print("Delete") 49 | if not os.path.isfile(path2): 50 | os.makedirs(path2) 51 | print("Create") 52 | 53 | 54 | 55 | vidcap = cv2.VideoCapture(videopath) 56 | success,image = vidcap.read()#READ THE VIDEO 57 | count1 = 0 58 | success = True 59 | while success: 60 | blur = cv2.bilateralFilter(image,9,75,75) #filter 61 | opr=cv2.fastNlMeansDenoisingColored(blur,None,10,10,7,21) #remove noise 62 | cv2.imwrite("%s\\imgWN%d.jpg" %(path2,count1),opr) # save frame as JPEG file 63 | vidcap.set(cv2.CAP_PROP_POS_MSEC,(count1)) #used to hold speed of frane generation 64 | success,image = vidcap.read() 65 | print ('Read a new frame: ', success) 66 | count1 += 1 67 | if(count1==200): 68 | break 69 | print("count 2====",count1) 70 | cv2.waitKey(0) 71 | 72 | 73 | #-----------------psnr of all frames----------------------------------- 74 | psnr1=[] #empty list to store psnr values 75 | 76 | f1 = [cv2.imread(file) for file in glob.glob("%s\\*.jpg"%path1)] #frames with noise 77 | f2 = [cv2.imread(file) for file in glob.glob("%s\\*.jpg"%path2)] #frame witthout noise 78 | i=0 #for checking loop working 79 | 80 | #psnr of all frames and average of all frames 81 | 82 | 83 | def psnr(a,b): # Calculate psnr for two images 84 | mse = np.mean( (a -b) ** 2 ) 85 | if mse == 0: 86 | return 100 87 | PIXEL_MAX = 255.0 88 | return 20 * math.log10(PIXEL_MAX / math.sqrt(mse)) 89 | 90 | 91 | for x in range(count): #take no. of frames from loop count 92 | 93 | plt.subplot(121),plt.imshow(f1[x]),plt.title('Noised image') #images with noise 94 | plt.xticks([]), plt.yticks([]) 95 | plt.subplot(122),plt.imshow(f2[x]),plt.title('Denoised image') #imges without noise 96 | plt.xticks([]), plt.yticks([]) 97 | d=psnr(f1[x],f2[x]) #calling psnr calculation function 98 | print("psnr",d) #print psnr of every two frams i.e original and blured 99 | psnr1.append(d) #store all value of psnr 100 | 101 | plt.show() 102 | i+=1 103 | print("the value==",i) 104 | cv2.waitKey(0) 105 | 106 | 107 | print("complete") 108 | #print(psnr1) 109 | print("The average of all frames psnr==",sum(psnr1)/count) #find average of all psnr values 110 | cv2.waitKey(0) 111 | 112 | 113 | 114 | #---------------------graph---------- 115 | x1 = np.arange(count) 116 | # setting the corresponding y - coordinates 117 | y = psnr1 118 | 119 | plt.xlabel('Frames') 120 | plt.ylabel('Psnr values') 121 | 122 | # potting the points 123 | plt.plot(x1, y) 124 | 125 | # function to show the plot 126 | plt.show() 127 | --------------------------------------------------------------------------------