Python homework: histogram, equalization, Gaussian filtering

coldplay.xixi
Release: 2021-03-10 10:41:31
forward
2472 people have browsed it

Python homework: histogram, equalization, Gaussian filtering

Python histogram, equalization, Gaussian filtering

  • Test original image
  • Histogram
    • Basic principle
    • matplotlib library draws histogram
    • RGB three-channel histogram
  • Histogram equalization
    • Basic principle
    • PCV library completes histogram equalization
  • Gaussian filter
    • Basic principles
    • Opencv Gaussian filter implementation

##(Free learning recommendation: python video tutorial

Test original image

Python homework: histogram, equalization, Gaussian filtering

Python homework: histogram, equalization, Gaussian filtering

Histogram

Basic Principles

What is a histogram: Histogram of an image Describes the relationship between the gray level of an image and the number of times (frequency) that the corresponding gray level appears in the image. Through the histogram, image segmentation, retrieval, classification and other operations can be performed.

hist function of the matplotlib library : The hist function can help draw histograms. It has many parameters, the first two parameters are used here: x, bins. The x parameter represents a one-dimensional array of pixels. If the array is more than one dimension, it can be flattened into one dimension using the flatten method. Generally speaking, reading a picture is a two-dimensional matrix, which requires flattening. The bins parameter indicates the number of columns to display the histogram

Assume there is a two-dimensional array img=[[159,120,130],[100,84,92],[168,150,212]]. The number represents the pixel value of the image. After flattening, img=[159,120,130,100,84,92,168,150,212]. The histogram drawn using the hist function is as shown below. The horizontal axis represents the pixel value, and the vertical axis represents the frequency of occurrence of the pixel value


Python homework: histogram, equalization, Gaussian filtering
cv2.calcHist() provided by opencv draws a histogram: The calcHist function needs to pass in the read Picture image; the channels of the image, if it is a grayscale image channels=0, if it is the r, g, and b channels respectively, then 0, 1, and 2 are passed in.

matplotlib library for drawing histograms

Textbook code

from PIL import Imagefrom pylab import *# 解决中文乱码plt.rcParams['font.sans-serif'] = 'SimHei'plt.rcParams['axes.unicode_minus'] = False#im = array(Image.open('headimage.jpeg').convert('L'))  # 打开图像,并转成灰度图像print(im)figure()subplot(121)gray()contour(im, origin='image')  #画图axis('equal')  # 自动调整比例axis('off')  # 去除x  y轴上的刻度title(u'图像轮廓')subplot(122)# flatten()函数可以执行展平操作,返回一个一维数组hist(im.flatten(), 128)print(im.flatten())title(u'图像直方图')plt.xlim([0,260])plt.ylim([0,11000])show()
Copy after login
Running results


Python homework: histogram, equalization, Gaussian filtering

RGB Three-channel histogram

Code implementation

import cv2from matplotlib import pyplot as plt
img = cv2.imread('headimage.jpeg',1)color = ('b','g','r')for i,col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])plt.show()
Copy after login
Running results


Python homework: histogram, equalization, Gaussian filtering

Histogram equalization

Basic Principle

What is histogram equalization: Histogram equalization uses the histogram of the image to adjust the contrast, which is a type of image enhancement. method. Intuitively, from the picture, the equalized picture has stronger contrast, is clearer, and has more obvious features; from the histogram, the frequency of the histogram gray value of the equalized picture is more uniform.

How to equalize the histogram

    Histogram equalization must first read a picture img and calculate the histogram value of the picture imhist (You can use the histogram function).
  • After getting the value of the histogram, you need to calculate the cumulative histogram cdf of the histogram (cdf[i] is equal to the sum of imhist[0] to imhist[i], which can be obtained directly using the cumsum function).
  • The last step is to equalize the histogram. For the pixel value img[i, j] in the
  • i row and j# column of the picture, use the formula img[i, j] = cdf[ img[i,j] ] / (m*n)*255 is calculated to obtain the equalized pixel value, and then the histogram of the equalized picture is calculated to obtain the equalized histogram. Figure

Use the histeq function of the PCV library to equalize : Pass in the image im and return the equalized histogram and cumulative histogram cdf.

PCV library completes histogram equalization

Textbook code

# -*- coding: utf-8 -*-from PIL import Imagefrom 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('Python homework: histogram, equalization, Gaussian filtering').convert('L'))  # 打开图像,并转成灰度图像im2, cdf = imtools.histeq(im)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()
Copy after login
Running results


Python homework: histogram, equalization, Gaussian filtering can be obtained by running the results , because the original image is overall darker (black), so the histogram of the original image appears more frequently in low pixels and less frequently in high pixels. After histogram equalization, the image as a whole becomes brighter. Observing the histogram, it is found that the frequency of low pixels has decreased, while the frequency of high pixels has increased, giving the image a more obvious contrast

Gaussian Filtering

##Basic Principles

What is Gaussian filtering

: Gaussian filtering is a linear smoothing filter that uses normal distribution for Image processing is suitable for eliminating Gaussian noise, and can blur the image to smooth the image and produce a blurred effect on the image.

高斯滤波原理:高斯滤波是用户指定一个模板,然后通过这个模板对图像进行卷积,所进行的卷积操作就是将模板中心周围的像素值进行加权平均后替换模板中心的像素值
Python homework: histogram, equalization, Gaussian filtering

opencv高斯滤波实现

代码实现

import cv2import matplotlib.pyplot as plt

im=cv2.imread("Python homework: histogram, equalization, Gaussian filtering")# 高斯滤波img_Guassian = cv2.GaussianBlur(im,(5,5),0)plt.subplot(121)plt.imshow(im)plt.subplot(122)plt.imshow(img_Guassian)plt.show()
Copy after login

Python homework: histogram, equalization, Gaussian filtering
Python homework: histogram, equalization, Gaussian filtering
从Python homework: histogram, equalization, Gaussian filtering中可以看出,经过高斯滤波后的图像变得模糊了,边缘变得没有那么明显,图像变得平滑

相关免费学习推荐:python教程(视频)

The above is the detailed content of Python homework: histogram, equalization, Gaussian filtering. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!