# Python Digital Image processing (v) degradation and restoration of images

Source: Internet
Author: User

import cv2importas npimportas pltimport scipyimport scipy.stats%matplotlib inline

Read in the image we need

= cv2.imread("apple.jpg"= cv2.resize(cv2.cvtColor(apple,cv2.COLOR_BGR2RGB),(200,200))plt.imshow(apple)plt.axis("off")plt.show()

Introduction to Noise Gaussian noise

Gaussian noise refers to a class of noises whose probability density function obeys a Gaussian distribution (i.e. normal distribution).

Similar to salt and pepper noise (salt and Pepper Noise), Gaussian noise (Gauss Noise) is also a common noise in digital images.

Salt and pepper noise is the noise which appears in random position, the noise depth is basically fixed, the Gaussian noise is opposite, it is noise at almost every point, noisy depth random noise.

As the above introduction we only need to implement a random matrix, the matrix values in general conform to the Gaussian distribution, and the original image to add, you can implement the Gaussian noise, python in the random provides a method of generating Gaussian random number, but NumPy provides a direct generation of random Gaussian matrix method.

We can use numpy here

= np.random.normal(mean,sigma,(row,col,ch))

So we can come up with a way to generate Gaussian noise.

def GaussieNoisy(image,sigma):    row,col,ch= image.shape    =0    = np.random.normal(mean,sigma,(row,col,ch))    = gauss.reshape(row,col,ch)    =+ gauss    return noisy.astype(np.uint8)
plt.imshow(GaussieNoisy(apple,25))plt.show()

For the effect of applying the Gaussian noise of Sigma 25

Salt and pepper noise

Compared to Gaussian noise, the concept of salt and pepper noise is very simple, that is, a random point in the image to make it 0 or 255

defSpnoisy (image,s_vs_p= 0.5, amount= 0.004): Row,col,ch=Image.shape out=Np.copy (image) Num_salt=Np.ceil (Amount*Image.size*s_vs_p) coords=[Np.random.randint (0, I- 1,int(Num_salt)) forIinchImage.shape] Out[coords]= 1Num_pepper=Np.ceil (Amount*Image.size*(1.-s_vs_p)) coords=[Np.random.randint (0+ N- 1,int(Num_pepper)) forIinchImage.shape] Out[coords]= 0    returnOut
plt.imshow(spNoisy(apple))plt.show()

Filtering arithmetic mean value filter

The arithmetic mean filter is to find the mean value of an image in a range, instead of the value of the center point of the range, which has been implemented before.

defArithmeticmeanoperator (ROI):returnNp.mean (ROI)defArithmeticmeanalogrithm (image): New_image=Np.zeros (image.shape) image=Cv2.copymakeborder (Image,1,1,1,1, Cv2. Border_default) forIinch Range(1, image.shape[0]-1): forJinch Range(1, image.shape[1]-1): New_image[i-1J-1]=Arithmeticmeanoperator (image[i-1: I+2J-1: j+2]) New_image=(New_image-Np.min(image))*(255/Np.Max(image))returnNew_image.astype (Np.uint8)
def rgbArithmeticMean(image):    = cv2.split(image)    = ArithmeticMeanAlogrithm(r)    = ArithmeticMeanAlogrithm(g)    = ArithmeticMeanAlogrithm(b)    return cv2.merge([r,g,b])plt.imshow(rgbArithmeticMean(apple))plt.show()

Geometric mean-value filtering

The geometric mean formula is as follows
$f (x, y) = [\prod_{(s,t) \in s_{x,y}}{g (s,t)}]^{\frac 1{mn}}$

defGeometricmeanoperator (ROI): ROI=Roi.astype (Np.float64) p=Np.prod (ROI)returnP**(1/(roi.shape[0]*roi.shape[1]))defGeometricmeanalogrithm (image): New_image=Np.zeros (image.shape) image=Cv2.copymakeborder (Image,1,1,1,1, Cv2. Border_default) forIinch Range(1, image.shape[0]-1): forJinch Range(1, image.shape[1]-1): New_image[i-1J-1]=Geometricmeanoperator (image[i-1: I+2J-1: j+2]) New_image=(New_image-Np.min(image))*(255/Np.Max(image))returnNew_image.astype (Np.uint8)
def rgbGemotriccMean(image):    = cv2.split(image)    = GeometricMeanAlogrithm(r)    = GeometricMeanAlogrithm(g)    = GeometricMeanAlogrithm(b)    return cv2.merge([r,g,b])plt.imshow(rgbGemotriccMean(apple))plt.show()

Harmonic mean value

The harmonic mean formula is defined as follows
$H = \frac{n} {\frac{1}{x_1}+\frac{1}{x_2}+\frac{1}{x_3}\ldots \frac{1}{x_n}}$

It should be noted here that the number of harmonic mean processing must be greater than 0, when x exists as the number of 0 is, approaching infinity, then h=0
So here we are, when there is a number greater than 0, it returns 0.

defHmeanoperator (ROI): ROI=Roi.astype (Np.float64)if 0 inchRoi:roi= 0    Else: ROI=Scipy.stats.hmean (Roi.reshape (-1))returnRoidefHmeanalogrithm (image): New_image=Np.zeros (image.shape) image=Cv2.copymakeborder (Image,1,1,1,1, Cv2. Border_default) forIinch Range(1, image.shape[0]-1): forJinch Range(1, image.shape[1]-1): New_image[i-1J-1]=Hmeanoperator (image[i-1: I+2J-1: j+2]) New_image=(New_image-Np.min(image))*(255/Np.Max(image))returnNew_image.astype (Np.uint8)defRgbhmean (image): R,g,b=Cv2.split (image) R=Hmeanalogrithm (R) G=Hmeanalogrithm (g) b=Hmeanalogrithm (b)returnCv2.merge ([r,g,b]) plt.imshow (Rgbhmean (apple)) Plt.show ()

Mean value of inverse harmonics

The formula is as follows
$f (x, y) = \frac{\sum_{(s,t) \in s_{xy}}{g (s,t) ^{q+1}}} {\sum_{(s,t) \in s_{xy}}{g (s,t) ^{q}}}$
So use Python to implement the following

defIhmeanoperator (roi,q): ROI=Roi.astype (Np.float64)returnNp.mean (ROI)**(q+1))/Np.mean (ROI)**(q))defIhmeanalogrithm (image,q): New_image=Np.zeros (image.shape) image=Cv2.copymakeborder (Image,1,1,1,1, Cv2. Border_default) forIinch Range(1, image.shape[0]-1): forJinch Range(1, image.shape[1]-1): New_image[i-1J-1]=Ihmeanoperator (image[i-1: I+2J-1: j+2],Q) New_image=(New_image-Np.min(image))*(255/Np.Max(image))returnNew_image.astype (Np.uint8)defRgbihmean (image,q): r,g,b=Cv2.split (image) R=Ihmeanalogrithm (R,Q) G=Ihmeanalogrithm (g,q) b=Ihmeanalogrithm (B,Q)returnCv2.merge ([r,g,b]) plt.imshow (Rgbihmean (Apple,2)) Plt.show ()

Restoration of images

Below we will try to restore images with Gaussian noise and salt and pepper noise.

= spNoisy(apple,0.5,0.1= GaussieNoisy(apple,25)plt.subplot(121)plt.title("Salt And peper Image")plt.imshow(spApple)plt.axis("off")plt.subplot(122)plt.imshow(gaussApple)plt.axis("off")plt.title("Gauss noise Image")plt.show()

== rgbGemotriccMean(spApple)plt.subplot(121)plt.title("Arithmatic to spImage")plt.imshow(arith_sp_apple)plt.axis("off")plt.subplot(122)plt.imshow(gemo_sp_apple)plt.axis("off")plt.title("Geomotric to spImage")plt.show()

== rgbGemotriccMean(gaussApple)plt.subplot(121)plt.title("Arithmatic to gsImage")plt.imshow(arith_gs_apple)plt.axis("off")plt.subplot(122)plt.imshow(gemo_gs_apple)plt.axis("off")plt.title("Geomotric to gsImage")plt.show()

The arithmetic mean can slightly remove the points produced by the salt and pepper noise, but the geometric mean effect is somewhat strange.

For Gaussian noise, both have very weak effects.

== rgbIHMean(spApple,3)plt.subplot(121)plt.title("H Mean to spImage")plt.imshow(arith_sp_apple)plt.axis("off")plt.subplot(122)plt.imshow(gemo_sp_apple)plt.axis("off")plt.title("IH mean to spImage")plt.show()

== rgbIHMean(gaussApple,3)plt.subplot(121)plt.title("HMean to gsImage")plt.imshow(arith_gs_apple)plt.axis("off")plt.subplot(122)plt.imshow(gemo_gs_apple)plt.axis("off")plt.title("IHMean to gsImage")plt.show()

, Ihmean effect is much better than Hmean, even Gauss God can achieve good denoising effect

Python Digital Image processing (v) degradation and restoration of images

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.