Python Computer Vision-Image Processing Fundamentals Chapter 1 Histogram, Gaussian Filtering, Histogram Equalization


Preface

Before I put the learning blog in the blog garden, the link is as follows:
blog garden link . I
just encountered the system failure of the blog garden and the reason of low page views, so I decided to update it together with CSDN.

In the digital image course last semester, we have initially touched the course of digital image processing. The difference is that last semester we used matlab to realize the basic image processing, while in this course we use python+opencv to realize the image deal with.
I use Pycharm to program.
Reference book materials: Python computer vision programming


1. Gray Scale Image?

The logarithmic relationship between white and black is divided into several levels, called grayscale. The gray scale is divided into 256 levels.
An image expressed in grayscale is called a grayscale image.
Any color is composed of the three primary colors (RGB) of red, green, and blue, and the grayscale image has only one channel with 256 gray levels, 255 represents all white, and 0 represents all black.
In addition to common satellite images and aerial photos, many geophysical observation data are also expressed in grayscale.

Principle introduction:

The convert() method is used to realize the color conversion of the image. The
principle of the convert('L') function in PIL:

  • img = img.convert()
    PIL has nine different modes: 1, L, P, RGB, RGBA, CMYK, YCbCr, I, F.

  • img.convert('1')
    is a binary image, either black or white. Each pixel is represented by 8 bits, 0 means black, and 255 means white.

  • img.convert('L') #The code used in the following code is
    a grayscale image. Each pixel is represented by 8 bits, 0 represents black, 255 represents white, and other numbers represent different gray levels.
    Conversion formula: L = R * 299/1000 + G * 587/1000 + B * 114/1000.

Code display:

# -*- codeing =utf-8 -*-
# @Time : 2021/3/7 15:16
# @Author : ArLin
# @File : demo1灰度图.py
# @Software: PyCharm
# -*- 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('data/001.jpg')
gray()# 不使用颜色信息
subplot(121)
title(u'原图',fontproperties=font)
axis('off')
imshow(pil_im)


pil_im = Image.open('data/001.jpg').convert('L')
subplot(122)
title(u'灰度图',fontproperties=font)
axis('off')
imshow(pil_im)


show()

Result display:

Insert picture description here

Image contour and histogram

Image outline:

An outline is a curve composed of a series of connected points, which represents the basic shape of an object. Compared with the edge, the outline is continuous, and the edges are not all continuous.
The operation of finding contours is generally used for binarized images, so threshold segmentation or Canny edge detection is usually used to obtain the binarized images first

Histogram:

The histogram is an illustration that can give an overall understanding of the grayscale distribution of the entire image. Through the histogram, we can have an intuitive understanding of the image's contrast, brightness, and grayscale distribution.
The histogram of the image is used to characterize the distribution of pixel values ​​of the image. A certain number of bins are used to specify the range of characterizing pixel values, and each bin will get the number of pixels that fall into the range of the bin.

Principle introduction:

1. Draw contour:
Because drawing contour requires setting a threshold for the pixel value of each coordinate [x, y], the image must be grayed first.

2. Draw a histogram:
use the drawing tool plt.hist() that comes with Matplotlib to draw.

Code display:

# -*- codeing =utf-8 -*-
# @Time : 2021/3/8 15:09
# @Author : ArLin
# @File : demo4图像轮廓和直方图.py
# @Software: PyCharm
# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *


# 添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"d:\JsVison\font\SimSun.ttc", size=14)
im = array(Image.open('data/001.jpg').convert('L'))  # 打开图像,并转成灰度图像,读取图像到数组中

#图像轮廓
figure()
subplot(121)
gray()# 不使用颜色信息
contour(im, origin='image')# 在原点的左上角显示轮廓图像
axis('equal')
axis('off')
title(u'图像轮廓', fontproperties=font)

#图像直方图
subplot(122)
hist(im.flatten(), 128)
title(u'图像直方图', fontproperties=font)
plt.xlim([0,260])
plt.ylim([0,11000])


show()

Result demonstration:

Insert picture description here

2. Histogram equalization

A good image is usually distributed evenly on the histogram, and histogram equalization is used to improve the overall brightness and contrast of the image.

Histogram equalization refers to flattening the grayscale histogram of an image so that the distribution probability of each grayscale value in the transformed image is the same. Before further processing the image, histogram equalization is usually a very good way to normalize the gray value of the image, and it can enhance the contrast of the image.

The transformation function of histogram equalization is the cumulative distribution function of the pixel values ​​in the image (cumulative distribution function, abbreviated as cdf, a normalization operation that maps the range of pixel values ​​to the target range).

The concrete realization of histogram equalization (function in imtools):

There are two input parameters in this function, one is the gray image, and the other is the number of cells used in the histogram.
The function returns the histogram equalized image and the cumulative distribution function used for pixel value mapping.
Note that the last element of the cumulative distribution function (subscript -1) is used in the function to normalize it to the range of 0...1.

def histeq(im,nbr_bins=256):
""" 对一幅灰度图像进行直方图均衡化"""
# 计算图像的直方图
imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)
cdf = imhist.cumsum() # cumulative distribution function
cdf = 255 * cdf / cdf[-1] # 归一化
# 使用累积分布函数的线性插值,计算新的像素值
im2 = interp(im.flatten(),bins[:-1],cdf)
return im2.reshape(im.shape), cdf

Code display:

# -*- codeing =utf-8 -*-
# @Time : 2021/3/8 15:17
# @Author : ArLin
# @File : demo8直方图均衡化.py
# @Software: PyCharm
# -*- 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"d:\JsVison\font\SimSun.ttc", size=14)


im = array(Image.open('data/001.jpg').convert('L'))  # 打开图像,并转成灰度图像
im2, cdf = imtools.histeq(im) #上述详解imtools.histeq函数


figure()
subplot(2, 2, 1)
axis('off')
gray()
title(u'原始图像', fontproperties=font)
imshow(im)


subplot(2, 2, 2)
axis('off')
title(u'直方图均衡化后的图像', fontproperties=font)
imshow(im2)


subplot(2, 2, 3)
axis('off')
title(u'原始直方图', fontproperties=font)
hist(im.flatten(), 128, density=True)


subplot(2, 2, 4)
axis('off')
title(u'均衡化后的直方图', fontproperties=font)
hist(im2.flatten(), 128, density=True)


show()

Result display:

As shown in the figure, the contrast of the image is enhanced after the histogram equalization, and the details of the gray area of ​​the original image become clear.
Insert picture description here

Problems encountered:

There is an error in the source code shown in the course of the book using the learning Python computer vision programming.
VisibleDeprecationWarning: Passing normed=Trueon non-uniform bins has always been broken, and computes neither the probability density function nor the probability mass function. The result is only correct if the bins are uniform, when density=True will produce the same result anyway. The argument will be removed in a future version of numpy.
hist, _ = np.histogram(lbp, normed=True, bins=max_bins, range=(0, max_bins))

Solution: replace normed with density

The keyword normed is deprecated in Numpy 1.6 because it can cause confusion/bug behavior. It will be removed in Numpy 2.0. Instead, use the density keyword to replace.

Three, Gaussian filtering

Gaussian filtering is a linear smoothing filter, which is suitable for eliminating Gaussian noise and is widely used in the denoising process of image processing.
In layman's terms, Gaussian filtering is the process of weighted averaging the entire image. The value of each pixel is obtained by weighted averaging of itself and other pixel values ​​in the neighborhood.

Principle introduction:

The specific operation of Gaussian filtering is to scan each pixel in the image with a template (or convolution, mask), and use the weighted average gray value of the pixels in the neighborhood determined by the template to replace the value of the center pixel of the template.
Insert picture description here
Insert picture description here

Filtering is a process of convolution processing on the input signal. It is written in the form of a function like this:
Filter = Convolution (input signal, convolution template) The difference of the convolution template/mask determines the different filtering methods. Therefore, basic filtering methods such as high-pass, low-pass, band-pass, and band-stop are produced.

For low-pass filtering, the low-frequency part of the signal is reserved and the high-frequency part is suppressed. To achieve this goal, you can use the mean mask, Gaussian mask, etc. to process the input signal.

The filtering method that uses the mean mask to convolve the input signal is called mean filtering; the filtering method that
uses the Gaussian mask to convolve the input signal is called the Gaussian filtering;

Gaussian filtering and Gaussian blur:

same. Without the qualifier "Gauss", the scope of filtering is much larger than blur, and the latter is only a subset of the former. When the "Gauss" limit is added, the convolution operation referred to is the same operation, because according to the equation filtering = convolution (input signal, convolution template), when the input signal and convolution template of the two operations are both At the same time, these two operations are one operation.
That is because the filtering effect looks like blurring the image, so it is called "Gaussian Blur" again.

SciPy (http://scipy.org/) is an open source toolkit for numerical operations based on NumPy. SciPy provides many efficient operations that can realize numerical integration, optimization, statistics, signal processing, and the most important image processing functions for us. Next, this section will introduce a large number of useful modules in SciPy. SciPy is an open source toolkit, which can be downloaded from http://scipy.org/Download.
Insert picture description here

Code display:

# -*- codeing =utf-8 -*-
# @Time : 2021/3/8 15:29
# @Author : ArLin
# @File : demo10高斯模糊.py
# @Software: PyCharm
# -*- 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"d:\JsVison\font\SimSun.ttc", size=14)


#im = array(Image.open('board.jpeg'))
im = array(Image.open('data/004.jpg').convert('L'))


figure()
gray()
axis('off')
subplot(1, 4, 1)
axis('off')
title(u'原图', 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'标准差为'+imNum, fontproperties=font)
  imshow(im2)


#如果是彩色图像,则分别对三个通道进行模糊
#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()

Result display:

The image shows the degree to which an image is blurred as σ increases. The larger σ is, the more details of the processed image will be lost.
Insert picture description here


Four, summary

The above is the content to be talked about today. This article only briefly introduces the use of histogram, Gaussian filtering, and histogram equalization. In fact, this article is also an introductory course for the author's computer vision course. I hope I can get more growth and harvest in the following study.

Guess you like

Origin blog.csdn.net/qq_43741419/article/details/115279917