Introduction to the statistical data of the ImageStat module of Python image processing library PIL

Source: Internet
Author: User
Tags image processing library

Introduction to the statistical data of the ImageStat module of Python image processing library PIL

The ImageStat module is used to calculate the statistical data of an entire image or an image area.

1. Functions of the ImageStat Module

1. Stat

Definition 1: ImageStat. Stat (image )? Stat instance

ImageStat. Stat (image, mask )? Stat instance

Meaning 1: Calculate the statistical value of a given image. If the variable mask is assigned a value, statistics will only be made for the region defined by the variable mask.

Example 1:

>>> from PIL importImage, ImageStat>>> im01 =Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")>>> im02 =Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>> r,g,b =im02.split()>>> stat =ImageStat.Stat(im01)>>> stat.sum[120170597.0, 151378139.0,151481196.0]>>> stat.count[786432, 786432, 786432]>>> stat_r =ImageStat.Stat(im01,r)>>> stat_r.sum[120170597.0, 151378139.0,151481196.0]>>> stat_r.count[786432, 786432, 786432]>>> stat_g =ImageStat.Stat(im01,g)>>> stat_g.sum[116891840.0, 146593055.0,145616479.0]>>> stat_g.count[760083, 760083, 760083]>>> stat_b =ImageStat.Stat(im01,b)>>> stat_b.sum[111057281.0, 140047475.0,139208738.0]>>> stat_b.count[729161, 729161, 729161]


Definition 2: ImageStat. Stat (list )? Stat instance

Meaning 2: it is the same as Definition 1, but it only calculates the statistical value of the previous histogram.

Example 2:

>>> from PIL importImage, ImageStat>>> im01 =Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")>>> im02 =Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>> stat = ImageStat.Stat(im01)>>> stat_l =ImageStat.Stat([0,0,100,100])>>> stat_l.count[200]>>> stat_l.sum Traceback (most recent calllast):  File "
 
  ", line 1,in 
  
       stat_l.sum  File"C:\Python27\lib\site-packages\PIL\ImageStat.py", line 48, in__getattr__    v = getattr(self, "_get" + id)()  File"C:\Python27\lib\site-packages\PIL\ImageStat.py", line 84, in _getsum    layerSum += j * self.h[i + j]IndexError: list index out ofrange>>> stat_l.mean[]>>> stat_l.bands[]>>> stat_l.h[0, 0, 100, 100]
  
 


The prompt in the python editor shows that the object stat_l has only four attributes, namely count, h, mean, and bands, but does not have the sum attribute.

2. attributes of the ImageStat Module

The following attributes have a sequence containing only one element for each channel of the image. These attributes are calculated only when called. Otherwise, they are not calculated.

1. Extrema

Definition: stat. extrema

Meaning: obtain the maximum and minimum values of each channel in the image.

Example:

>>> from PIL importImage, ImageStat>>> im02 =Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>> stat =ImageStat.Stat(im02)>>> stat.extrema[(2, 255), (0, 255), (0, 255)]

In im02, the minimum value of the red channel is 2 and the maximum value is 255. the minimum value of the green and blue channels is 0, and the maximum value is 255.

2. Count

Definition: stat. count

Meaning: obtains the number of pixels for each channel in an image.

Example:

>>> from PIL importImage, ImageStat>>> im02 =Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>> im02.mode'RGB'>>> im02.size(1024, 768)>>> stat =ImageStat.Stat(im02)>>>stat.count[786432, 786432,786432]

The im02 mode is "RGB", the size is 1024x768, and the number of pixels is 786432. Therefore, the statistical result of the attribute count is 786432 for all three channels.

3. Sum

Definition: stat. sum

Meaning: obtains the sum of pixel values of each channel in an image.

Example:

>>> from PIL import Image, ImageStat>>> im02 =Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>> stat = ImageStat.Stat(im02)>>> stat.sum[90912466.0,75765120.0, 59027727.0]

The pixel values of the three channels of the image im02 are accumulated as 90912466.0, 75765120.0, and 59027727.0 respectively.

4. Sum2

Definition: stat. sum2

Meaning: obtains the sum of the pixel values of each channel in an image.

Example:

>>>from PIL import Image, ImageStat>>>im02 = Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>>stat = ImageStat.Stat(im02)>>>stat.sum[90912466.0,75765120.0, 59027727.0]>>>stat.sum2[14449895138.0,12289898764.0, 9141884969.0]

The property sum2 calculates the sum of the pixel values of each channel, not the sum of the square.

5. Mean

Definition: stat. mean

Meaning: Get the average value of the pixel value of each channel in the image.

Example:

>>>from PIL import Image, ImageStat>>>im02 = Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>>stat = ImageStat.Stat(im02)>>>stat.sum[90912466.0,75765120.0, 59027727.0]>>>stat.count[786432, 786432,786432]>>>stat.mean[115.60117848714192,96.34033203125, 75.05763626098633]

From the instance, we can see that the attribute mean is the sum of the pixel values of each channel divided by the number of pixels, accurate to 14 digits after the decimal point.

6. Median

Definition: stat. mean

Meaning: obtain the value of pixels in each channel in the image.

Example:

>>> from PIL import Image, ImageStat>>> im02 =Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>> stat = ImageStat.Stat(im02)>>> stat.extrema[(2, 255), (0,255), (0, 255)]>>>stat.median[119, 80, 40]

The median attribute obtains the pixel value of each channel.

7. Rms

Definition: stat. rms

Meaning: Get the root mean square value of the pixel value of each channel in the image. Root value, also known as square root value or valid value. The calculation method is square first, then average, and then square. That is, divide the sum of squares of N items by the square result after N, that is, the result of Root Mean. The formula is as follows:

Example:

>>>from PIL import Image, ImageStat>>>im02 = Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>>stat = ImageStat.Stat(im02)>>>stat.rms[135.55069835243268,125.00965724006934, 107.81701101697355]

8. Var

Definition: stat. var

Meaning: obtains the difference between the pixel values of each channel in the image. Variance is a measure of the degree of discretization when probability theory and statistical variance are used to measure random variables or a set of data. In probability theory, variance is used to measure the deviation between a random variable and its mathematical expectation (that is, the mean. The variance (sample variance) in the statistics is the sum of the square that shows the difference between each data and its mean.

Example:

>>>from PIL import Image, ImageStat>>>im02 = Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>>stat = ImageStat.Stat(im02)>>>stat.var[5010.359356216148,6345.954827388127, 5990.859103547667]

9. Stddev

Definition: stat. stddev

Meaning: gets the standard difference of pixel values for each channel in the image. The Standard Deviation is also known as the Standard Deviation. The Standard Deviation (Standard Deviation) describes the average distance (Deviation) between the mean of each data Deviation. It is the squared variance and the percentile after the mean, which is represented by σ. Standard deviation is the arithmetic square root of variance. The standard deviation reflects the degree of discretization of a dataset. The smaller the standard deviation, the less the deviation between these values from the average value, and vice versa. The size of the standard deviation can be measured by the rate relationship between the standard deviation and the average value. The formula for standard deviation is as follows:

Example:

>>>from PIL import Image, ImageStat>>>im02 = Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>>stat = ImageStat.Stat(im02)>>>stat.stddev[70.78389192617306,79.66150153862358, 77.40064020114863]

The example shows that the pixel value of the red channel in the image im02 is closest to the average value.

 

Contact Us

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.