opencv2-- histogram 5

(A) Histogram

Constituting an image is composed of pixels, each pixel value represents the color of the point (grayscale or color image). The so-called histogram is to the values ​​of these pixels in the image statistics, get a unified whole concept of gray. The benefits of the histogram is that you can clearly understand the overall distribution of gray image, which is critical for image processing based on the histogram for the latter.

Are generally gray-scale image histogram, histogram of the x-axis is the gradation value (generally 0 ~ 255), y-axis the number of gray level corresponding to each pixel of the image is.

So how do you get a histogram of the image? First, let's understand some of the amount needed to draw a histogram: gray scale is 0-255 total of 256 gray levels under normal circumstances, up from darkest to brightest (white) (statistics may also be a part of one of the gray-scale range ), then each number corresponds to a gray level to store the number of dots corresponding to the gradation. That histogram is actually a 1 * m (gray scale) of an array only. But sometimes we do not want a gray level of increase, for example, now I want 15 gray together as a gray-level histogram to spend, this time we may only need 1 * (m / 15) on such an array enough. So 15 is the width of the interval histogram here.

Opencv function provides us with a cv2.calcHist (), this function has five parameters:

image input image, an incoming should be enclosed in brackets [] enclosed
channels :: incoming channel image, if the image is gray, it goes without saying that only one channel, a value of 0, if it is a color image (with 3 channels), then the value of 0,1,2, a selected, corresponding to the respective channels BGR. This value have to use [] passed.
mask: the mask image. If the statistics the whole image, it is none. If the main part of the figure to histogram, must form the corresponding mask calculated inflammation.
histSize: the number of gray levels, requires brackets, such as [256]
Ranges: range of pixel values, typically [0,256], 0-256 conventional image if not, you say that various changes back and forth resulting in a negative pixel value great, you need to adjust before they can.
In addition, there are strong numpy functions for statistical histogram, a generic function np.histogram, there is a function np.bincount () (for that histogram, faster). These three parameters passed embodiment is substantially similar, except that the need for carrying opencv in parentheses.
For histogram display is relatively simple, direct plt.plot () on it. One example is as follows:

. 1  Import CV2
 2  Import numpy AS NP
 . 3  Import matplotlib.pyplot AS PLT
 . 4 IMG = cv2.imread ( ' test.jpg ' , 0)   # direct reading of a grayscale image 
. 5  
. 6  # OpenCV method of reading -cv2.calcHist (speed The fastest) 
7  # image channel [0] - grayscale mask - no gray level, a pixel range 
. 8 hist_cv cv2.calcHist = ([IMG], [0], None, [256], [0 , 256 ])
 . 9  
10  # numpy method of reading -np.histogram () 
. 11 hist_np, bins = np.histogram (img.ravel (), 256, [0, 256 ])
 12 is  
13 is  #Another method of reading numpy -np.bincount () (= 10 times the speed of Method 2) 
14 hist_np2 = np.bincount (img.ravel (), minLength = 256 )
 15  
16 plt.subplot (221), PLT. imshow (IMG, ' Gray ' )
 . 17 plt.subplot (222 ), plt.plot (hist_cv)
 18 is plt.subplot (223 ), plt.plot (hist_np)
 . 19 plt.subplot (224 ), plt.plot (hist_np2)
 20 plt.show ()

 

Results of the:

 

 

Now consider using the histogram function opencv in the mask, the mask is a region the size of, that your next histogram is the pixel count of the region. An example follows:

. 1  Import CV2
 2  Import numpy AS NP
 . 3  Import matplotlib.pyplot AS PLT
 . 4  
. 5 IMG = cv2.imread ( ' test.jpg ' , 0)   # direct reading of a grayscale image 
. 6 mask = np.zeros (img.shape [: 2 ], np.uint8)
 . 7 mask [100: 200 is, 100: 200 is] = 255
 . 8 masked_img = cv2.bitwise_and (IMG, IMG, mask = mask)
 . 9  
10  # OpenCV method of reading -cv2.calcHist (fastest ) 
11  # image channel [0] - grayscale mask - no gray level, the range of the pixel 
12 is hist_full cv2.calcHist = ([IMG], [0], None, [256], [0, 256 ])
 13 
14 hist_mask = cv2.calcHist([img], [0], mask, [256], [0, 256])
15 
16 plt.subplot(221), plt.imshow(img, 'gray')
17 plt.subplot(222), plt.imshow(mask, 'gray')
18 plt.subplot(223), plt.imshow(masked_img, 'gray')
19 plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask)
20 plt.show()

 

Results of the:

 

 

(B) histogram equalization

Is a histogram of an image of processing on the image contrast effect, so that the entire image is intended to effect uniform, a point between the respective pixel level between the black and white bit more uniform.
Histogram equalization as long comprising three steps:

1, the number of each gray level histogram statistics appear;
2, calculating the normalized cumulative histogram;
3, pixel values of the pixels recalculated;
detailed section on the principle of a reference to:

Histogram equalization principle

Baidu Encyclopedia explanation is great

http://baike.baidu.com/link?url=RUjahehgkTMDGKwAEyMsHyeMyWWTw9a0KUx2CzLbXtxdZyoF6zqDbJsfEffUQYAwvr7kD9p6cbOxJGYGk1nkZq

 

In a special function opencv histogram equalization function is used cv2.equalizeHist () as an example:

 

Import CV2
 Import matplotlib.pyplot AS PLT 

IMG = cv2.imread ( ' the flower.jpg ' , 0) # read directly grayscale image 
RES = cv2.equalizeHist (IMG) 

plt.subplot ( 121), plt.imshow (IMG, ' Gray ' ) 
plt.subplot ( 122), plt.imshow (RES, ' Gray ' )

 

Above histogram equalization may be possible to equalize on a global sense, but sometimes this operation is not very good, some adjustment should not be part of will to adjust. There is also a Opencv histogram equalization, which is a local equalization to partial, that is to say the entire image is divided into a plurality of small blocks (such as by a small piece of 10 * 10), then for each small block equalizing. This method is mainly for image histogram is not so single (such as the presence of multimodal case) image is more practical. This method is called in the Opencv CLAHE, the function is used to cv2.createCLAHE (), as an example:

 

Import CV2
 Import matplotlib.pyplot AS PLT 

IMG = cv2.imread ( ' the flower.jpg ' , 0) # read directly grayscale image 
CLAHE = cv2.createCLAHE (clipLimit = 2, tileGridSize = (10,10 )) 
CLl = CLAHE .apply (IMG) 

plt.subplot ( 121), plt.imshow (IMG, ' Gray ' ) 
plt.subplot ( 122), plt.imshow (CLl, ' Gray ' )

 



It can be seen relative to the global histogram equalization, the local equalization seem to get a little more natural effect.
----------------
Disclaimer: This article is the original article CSDN bloggers "on2way", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/on2way/article/details/46881599

 

 

 

 

 

 

 

 

 

1

Guess you like

Origin www.cnblogs.com/weststar/p/11512991.html