# Preface

## Experimental environment

This course experiment is based on the image processing of Python language. It uses anaconda to install and manage the python package, and compiles and implements it on pycharm platform.

## Experimental preparation

All the installation packages needed for the experiment need to be added manually. The command code for adding the main library is as follows:

• PCV Library
```>>The file address of setup.py > > Python setup.py install
```

==The following python library installation due to the error "No module named 'pylab'"==

• NumPy library. Provides many advanced mathematical methods for Python.
```conda install numpy
```
• SciPy library. This is a Python Library of algorithms and mathematical tools.
```conda install scipy
```
• matplotlib library. A library for drawing data graphs.
```conda install matplotilb
```

# Image graying

## Basic principle

Grayscale: in RGB model, if R=G=B, color represents a grayscale color. The value of R=G=B is called grayscale value. Therefore, each pixel of grayscale image only needs one byte to store the grayscale value (also called intensity value and brightness value), and the grayscale range is 0-255. Generally, there are four methods to gray color image: the method of maximum value of component, the method of average value, the method of weighted average.
Image grayscale processing can be used as a preprocessing step of image processing to prepare for the following upper operations such as image segmentation, image recognition and image analysis. In the process of digital image processing, the image of various formats is usually transformed into gray-scale image to reduce the calculation of the subsequent image. The description of gray-scale image still reflects the distribution and characteristics of the overall and local chroma and brightness levels of the whole image as well as the color image.

• Main code analysis
• convert() function
It is a method of image instance object, which accepts a mode parameter to specify a color mode
1 ------------ (1-bit pixel, black and white, one pixel per byte)
L -------- (8-bit pixel, black and white)
P ------------ (8-bit pixels, mapped to any other mode using palette)
RGB ------------ (3x8 pixels, true color)

## code implementation

```# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *

from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)
figure()

pil_im = Image.open(r'C:\Users\Lin Li Er\Desktop\chart\bts.jpg')
gray()//Do not use color information
subplot(121)
title(u'Original graph',fontproperties=font)
axis('off')//Do not show coordinates
imshow(pil_im)

pil_im = Image.open(r'C:\Users\Lin Li Er\Desktop\chart\bts.jpg').convert('L')
subplot(122)
title(u'Grayscale',fontproperties=font)
axis('off')
imshow(pil_im)

show()
```

# Draw image outline and image histogram

## Basic principle

• Main code analysis

## code implementation

```# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *

from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)
im = array(Image.open(r'C:\Users\Lin Li Er\Desktop\chart\bts.jpg').convert('L'))  # Open image and convert to grayscale image

figure()
subplot(121)
gray()
contour(im, origin='image')
axis('equal')
axis('off')
title(u'Image contour', fontproperties=font)

subplot(122)
hist(im.flatten(), 128)
title(u'image histogram ', fontproperties=font)
plt.xlim([0,260])
plt.ylim([0,11000])

show()
```

# Gauss filter (Gauss fuzzy processing)

## code implementation

```# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
from scipy.ndimage import filters

from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)

#im = array(Image.open('board.jpeg'))
im = array(Image.open(r'C:\Users\Lin Li Er\Desktop\chart\bts.jpg').convert('L'))

figure()
gray()
axis('off')
subplot(1, 4, 1)
axis('off')
title(u'Original graph', fontproperties=font)
imshow(im)

for bi, blur in enumerate([2, 5, 10]):
im2 = zeros(im.shape)
im2 = filters.gaussian_filter(im, blur)
im2 = np.uint8(im2)
imNum=str(blur)
subplot(1, 4, 2 + bi)
axis('off')
title(u'Standard deviation is'+imNum, fontproperties=font)
imshow(im2)

#If it is a color image, blur the three channels respectively
#for bi, blur in enumerate([2, 5, 10]):
#  im2 = zeros(im.shape)
#  for i in range(3):
#    im2[:, :, i] = filters.gaussian_filter(im[:, :, i], blur)
#  im2 = np.uint8(im2)
#  subplot(1, 4,  2 + bi)
#  axis('off')
#  imshow(im2)

show()
```

# Histogram equalization

## code implementation

``` # -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
from PCV.tools import imtools

from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)

im = array(Image.open(r'C:\Users\Lin Li Er\Desktop\chart\bts.jpg').convert('L'))  # Open image and convert to grayscale image
#im = array(Image.open('../data/AquaTermi_lowcontrast.JPG').convert('L'))
im2, cdf = imtools.histeq(im)

figure()
subplot(2, 2, 1)
axis('off')
gray()
title(u'original image', fontproperties=font)
imshow(im)

subplot(2, 2, 2)
axis('off')
title(u'Image after histogram equalization', fontproperties=font)
imshow(im2)

subplot(2, 2, 3)
axis('off')
title(u'Original histogram', fontproperties=font)
#hist(im.flatten(), 128, cumulative=True, normed=True)
hist(im.flatten(), 128, normed=True)

subplot(2, 2, 4)
axis('off')
title(u'Histogram after equalization', fontproperties=font)
#hist(im2.flatten(), 128, cumulative=True, normed=True)
hist(im2.flatten(), 128, normed=True)

show()
```

## experimental result

Published 7 original articles, won praise 1, visited 239

Posted on Sun, 08 Mar 2020 00:26:54 -0500 by lukatchikm