This is the 25th day of my participation in the August Genwen Challenge.More challenges in August

Python OpenCV 365 day learning plan, enter the graphics realm with eraser.

Foundation of basic knowledge

In previous blogs, we used to obtain image histograms by obtaining one-dimensional histograms, which simply means obtaining the features of only one channel, such as gray scale, B channel, and R channel.

The first thing we’re going to look at today is a two-dimensional histogram, also known as a 2D histogram, which deals with two features, one of which is the hue of the pixel and the other is saturation.

With these two values, as you can probably guess, you need to convert the image to HSV format ahead of time.

Cv2. CalcHist function

Calculate the 2D histogram using the same cv2.calcHist function as you learned before.

To obtain color histograms, you need to convert BGR to HSV in advance.

The function prototype

cv2.calcHist(images, channels, mask, histSize,ranges[, hist[,accumulate]])
Copy the code

Parameter Description:

  • Images: The original image (image format uint8 or float32), which should be in brackets when passing in a function[]For example:[img];
  • Channels:[0, 1]H and S channels need to be processed simultaneously;
  • Bins:[180256]The Hue channel is 180, and the S channel is 256.
  • The range:[0,180,0,256]Hue ranges from 0 to 180, and saturation S ranges from 0 to 256.

The test code is as follows:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

def create_2d_hist(image) :
    hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
    hist = cv.calcHist([hsv], [0.1].None[180.256],  [0.180.0.256])
    return hist

img = cv.imread('2.jpg')
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
plt.plot(create_2d_hist(img))

plt.show()
Copy the code

The running effect is as shown below, and you can see the 2D histogram.

In the retrieval of information, also learned the following way to display the histogram, but the effect is not as good as the line.

plt.imshow(hist,interpolation = 'nearest')
Copy the code

2D histogram in NUMpy

The NUMpy library also provides a function for 2D histograms, the Np.histogram2d function.

The function prototype

As you have previously studied the Np. histogram function, understanding this 2D histogram function should be easier.

histogram2d(x, y, bins=10.range=None, normed=None, weights=None, density=None)
Copy the code

Parameter Description:

  • X, Y: H channel and S channel;
  • Bins: Number of bins;
  • Range: range of H and S.

A more detailed description of the parameters can be found by using help(np.histogram2d).

The test code is as follows:

After running, the result is shown in the figure below.

The red line in the table above is the content of eraser marks. The two tables are corresponding. H=25,S=20 and H=100-150,S=120-100, we can get the high values of yellow, blue and purple regions, and the corresponding original graph, the conclusion is similar.

The original image as follows

The official manual can be read: click to jump

Eraser section

I hope you got something out of your hour today, and we’ll see you in our next blog

reading


If you have ideas or techniques you’d like to share, feel free to leave them in the comments section.


Blogger ID: Dream eraser, hope you like, comment, favorites.