OpenCV common basic processing functions (7) and the image pyramid histograms

Gaussian pyramid

Gaussian pyramid is the top by the bottom image in successive rows and columns removal obtained. Each pixel value is equal to the top of the image layer of the image Gaussian weighted average of five pixels.

This operation once a MxN picture image becomes a M / 2xN / 2 in. So this piece of area of ​​the image becomes a quarter of the original image area.

You can get a resolution declining image pyramid. We can use the function cv2.pyrDown () and cv2.pyrUp () to construct an image pyramid.

Image outline:

The profile can be simply considered as discrete points (boundaries attached) together curve having the same color or gray scale;

Finding the contour of a binary image:
  function cv2.findContours () has three parameters, a first input image, the outline of the second search mode, the third contour approximation method. There are three return values, it is a first image, a second profile, and the third is chromatographed structure (profile).

 

Histogram

A histogram is a grayscale image drawing instead of the color map

OpenCV using an image histogram statistics

Parameters:
cv2.calcHist (Images, channels, mask, HISTSIZE, Ranges [, a hist [, the accumulate]])
  1. Images: the original image (or image format uint8 float32). When passed to the function to be [] enclosed in square brackets, for example: [img].
  2. channels: also need to be enclosed in brackets, and it will tell histogram function we want to photograph the image. If the input image is a grayscale image, its value is [0]; if it is a color image, the parameters passed may be [0], [1], [2] which correspond to the channels B, G, R.
  3. mask: the mask image. To histogram of the entire image to put it to None. But if you want a certain part of the statistical histogram of the image, you will need to make a mask image, and use it. (There are examples of back)
  4. HISTSIZE: the BIN number. Should also be enclosed in square brackets, for example: [256].
  5. ranges: pixel value range, usually [0,256]

 

Also can do, Matplotlib Matplotlib have to draw a histogram function: matplotlib.pyplot.hist ()
which can directly draw a histogram and statistics

Possible to use only the drawing function matplotlib, which while drawing a multi-channel (BGR) histogram

CV2 Import 
Import numpy AS NP
 from matplotlib Import pyplot AS PLT 
IMG = cv2.imread ( ' home.jpg ' ) 
Color = ( ' B ' , ' G ' , ' R & lt ' ) 
# of a list or array to traverse the index and both to traverse the elements, using the enumerate 
for I, COL in the enumerate (Color): 
histr = cv2.calcHist ([IMG], [I], None, [ 256 ], [ 0 , 256 ]) 
plt.plot (histr, Color = COL) 
plt.xlim ([0,256])
plt.show()

Histogram equalization

One-dimensional histogram

The resulting histogram may direct some of the pixel values ​​in one place, the pixel values ​​of a high-quality image should be widely distributed.

So you should put it to become evenly distributed histogram, which is the histogram equalization. This operation will normally improve the contrast of the image .

Here directly introduce adaptive histogram equalization function can be effectively retained some details of the image;

In this case, the entire image is divided into many smaller pieces is called "tiles" (i.e. the second parameter is a function of the 8x8 by default), then a small piece of each histogram equalization, respectively of

numpy Import AS NP 
Import CV2 
IMG = cv2.imread ( ' tsukuba_l.png ' , 0 ) 
# Create a CLAHE objects
CLAHE = cv2.createCLAHE (clipLimit = 2.0 , tileGridSize = ( . 8 , . 8 )) CLl = clahe.apply (IMG ) cv2.imwrite ( ' clahe_2.jpg ' , CLl)

 

2D histogram

Consider a one-dimensional histogram of gray values ​​of this factor only, 2d histogram and color saturation to consider two factors; (the first two is the color saturation and space hsv)


2D histogram is calculated cv2.calcHist (), to modify the parameters:
  • channels = [0,. 1] S and H simultaneously process two channels.
  • bins = [180, 256] H is a channel 180, S 256 channels.
  • range = [0, 180, 0, 256] H in the range of 0 to 180, S in the range 0-256.
The last line is drawn using the drawing function of plt color histogram, interpolation function with the nearest.
img = cv2.imread('home.jpg')
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

hist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
plt.imshow(hist,interpolation = 'nearest')

 

Histogram back projection

The purpose is to find some or other action we are interested in the image;

Projection and outputs the input image (to be searched for) the same size of the image, wherein each pixel value represents the probability that the corresponding point on the input image belongs to the target object. ,

More simple words to explain, the higher the output image pixel values ​​(more white) represents the target point, the more likely we are to be searched (the position of the input image is located).

OpenCV in using the function cv2.calcBackProject () to do the histogram back projection. Its parameters with substantially the same function cv2.calcHist parameters. One argument is that we want to find the target histogram. The third parameter is targeted histogram

Prior to re-use the same target histogram back projection we should do to make their normalization process. Returned result is a probability that the image, we then checked using a disc-shaped convolution operations which do volume, the last used threshold binarization

import cv2
import numpy as np
roi = cv2.imread('tar.jpg')
hsv = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
target = cv2.imread('roi.jpg')
hsvt = cv2.cvtColor(target,cv2.COLOR_BGR2HSV)
# calculating object histogram
roihist = cv2.calcHist([hsv],[0, 1], None, [180, 256], [0, 180, 0, 256] )
Apply backprojection and the normalize Histogram # 
# normalized: the original image, the image a result, the resulting image is mapped to the minimum value, maximum value, a normalized type 
# cv2.NORM_MINMAX array for all values were transformed to a linear mapping so that they between the minimum and maximum 
histogram after normalizing # ease of illustration, it would be after normalization 0 to 255 number between a. 
cv2.normalize (roihist, roihist, 0 , 255 , cv2.NORM_MINMAX) 
DST = cv2.calcBackProject ([hsvt], [ 0 , . 1 ], roihist, [ 0 , 180 [ , 0 , 256 ], . 1 ) 
# Now with Convolute Disc Circular 
# convolution herein can be scattered points together 
Disc = cv2.getStructuringElement (cv2.MORPH_ELLIPSE, ( . 5 ,. 5 )) 
DST = cv2.filter2D (DST, - . 1 , Disc) 
# binary threshold and the AND 
RET, Thresh = cv2.threshold (DST, 50 , 255 , 0 ) 
# Remember is a three-channel image, hence the use of merge into 3 channels 
Thresh = cv2.merge ((Thresh, Thresh, Thresh)) 
# bitwise operations 
RES = cv2.bitwise_and (target, Thresh) 
RES = np.hstack ((target, Thresh, RES)) 
cv2.imwrite ( ' res.jpg ' , RES) 
# display image 
cv2.imshow ( ' . 1 ' , RES) 
cv2.waitKey ( 0)

The results are shown below:

 

 参考自:https://www.cnblogs.com/Undo-self-blog/p/8439149.html

Guess you like

Origin www.cnblogs.com/ywheunji/p/10990311.html