Python OpenCV learning notes implement two-dimensional histogram, pythonopencv

Source: Internet
Author: User

Python OpenCV learning notes implement two-dimensional histogram, pythonopencv

This article describes how to implement a two-dimensional histogram using python OpenCV learning notes. The details are as follows:

Documents-https://docs.opencv.org/3.4.0/dd/d0d/tutorial_py_2d_histogram.html

In the previous article, we calculated and drew a one-dimensional histogram. It is called one-dimensional, because we only consider one feature, that is, the gray intensity value of the pixel. However, in a two-dimensional histogram, you can consider two features. It is usually used to find the color histogram, where two features are the tone and saturation values of each pixel.
There is a python example (samples/python/color_histogram.py) used to find the color histogram. We will try to understand how to create such a color histogram, which will help to understand more in-depth theme like histogram reverse projection.

Two-dimensional histogram in OpenCV

It is very simple and uses the same function cv. calcHist () for calculation. For color histograms, we need to convert the image from BGR to HSV. (Remember, we convert a one-dimensional histogram from BGR to grayscale ). For a 2D histogram, its parameters are modified as follows:

Channels = [0, 1]: because we need to process H (Hue) and S (Saturation) simultaneously ).

Bins = [180,256]: 180 corresponds to H, 256 corresponds to S.

Range = [0,180, 0,256]: The tone value ranges from 0 to 180, and the saturation value ranges from 0 to 256.

import numpy as npimport cv2 as cvimg = cv.imread('home.jpg')hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)hist = cv.calcHist([hsv], [0,1], None, [180,256], [0,180,0,256])

Two-dimensional histogram in Numpy

Numpy also provides a unique function, np. histogram2d () (Remember, for one-dimensional histograms, use the function np. histogram ())

import numpy as npimport cv2 as cvfrom matplotlib import pyplot as pltimg = cv.imread('home.jpg')hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)hist, xbins, ybins = np.histogram2d(h.ravel(), s.ravel(), [180,256], [[0,180], [0,256]])

The first parameter is the H plane, the second is the S plane, the third is the number of bins each, and the fourth is their range.

Draw a Two-Dimensional Histogram

Method 1: Use cv. imshow ()

The result is that the size of a two-dimensional array is 180x256. Therefore, we can use the cv. imshow () function as usual to display them. It will be a grayscale image, and it will not tell you what colors unless you know different colors.

Method 2: Use Matplotlib

We can use the matplotlib. pyplot. imshow () function to plot 2D histograms with different color mappings. It provides us with a better idea about different pixel density. But this does not let us know what color we see at the first glance, unless you know the different colors. This is simple and even better.

import numpy as npimport cv2 as cvfrom matplotlib import pyplot as pltimg = cv.imread('home.jpg')hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)hist = cv.calcHist([hsv], [0,1], None, [180,256], [0,180,0,256])plt.imshow(hist, interpolation='nearest')plt.show()

The following figure shows the input image and its color histogram. The X axis represents the S value (saturation), and the Y axis represents the tone.


In the histogram, you can see some high values near H = 100 and S = 200. It corresponds to the blue sky. Similarly, another peak value can be seen near H = 25 and S = 100. It corresponds to the yellow of the palace. You can use image editing tools like GIMP to verify it.

Method 3: OpenCV sample style

There is a sample code (samples/python/color_histogram.py) for the color histogram in the Opencv-Python2 sample ). If you run the code, you can see the corresponding color of the histogram. Or simply output a color-encoded histogram. The result is very good (although you need to add some additional rows ).
In this Code, the author creates a color map in HSV. Then convert it to BGR. The generated histogram image is multiplied by the color graph. He also uses preprocessing steps to remove small isolated pixels to form a good histogram.


The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.