opencv norm, histogram equalization

norm() is used to calculate the L1, L2, infinite norm and Hamming norm of the image. OpenCV provides two interface forms. The first interface only requires one input image, and the second interface has two input images.

cv2.norm(src1[, normType[, mask]]) ->retval

src1: Input image, if it is multi-channel, channel calculation will not be distinguished; when finding Hamming norm, it must be a single-channel 8-bit data type;
normType: norm type;
mask: mask;
retval: returned norm value, floating point type ;

cv2.norm(src1, src2[, normType[, mask]]) ->retval

The input images src1 and src2 can be single-channel or multi-channel images. When using multi-channel images, the channels are not calculated separately. This is different from many functions. If you want to calculate the norms of individual channels, you need to separate the channels first. In addition, when calculating the Hamming norm, the input image is limited to the CV_8U type.

The optional types of normType are:

nomrType	名称	输入图像只有src1	输入图像有src1和src2
NORM_L1 	L1范数	所有元素的绝对值的和	src1-src2的所有元素的绝对值的和

NORM_L2 	L2范数	所有元素平方和的开平方	src1-src2的所有元素平方和的开平方

NORM_L2SQR 	L2范数平方	所有元素平方和	src1-src2的所有元素平方和

NORM_INF 	无穷范数	元素中绝对值最大的数值	src1-src2的元素中绝对值最大的数值

NORM_HAMMING 	汉明范数	计算src1和0的汉明距离,也即所有元素中1的位数的总和	src1和src2异或后元素中1的位数的总和

NORM_HAMMING2 	汉明范数2	单个元素从右到左相邻的两个bit,如果不全0记为一个1,然后统计新的1的位数	src1和src2异或后元素按照左侧的方法计算1的位数
val = cv2.norm(img_src,cv2.NORM_L2)
print('lena图像L2范数:',val )
val = cv2.norm(img_src,cv2.NORM_L2SQR)
print('lena图像L2范数平方:',val )
val = cv2.norm(img_src,cv2.NORM_L1)
print('lena图像L1范数:',val )
val = cv2.norm(img_src,cv2.NORM_INF)
print('lena图像无穷范数:',val )

The L1 norm of the image is the sum of the absolute values ​​of all elements, the L2 norm is the square sum of the square roots of all elements, and the infinity norm is the absolute value of the element with the largest absolute value; the Hamming norm is constrained to a single-channel 8-bit image Type, NORM_HAMMING counts the total number of bits of all elements that are 1, and the norm represented by NORM_HAMMING2 is the total number of adjacent 2 bits that are not 0.

In the norm calculation of the second interface form, L1, L2, and infinite norms can be regarded as the norm of the image obtained by subtracting two images using absdiff() and then calculated using the first interface form, Hamming norm. It can be regarded as the norm of the image obtained after XORing two images with bitwise_xor() and then using the first interface form to calculate.

matplotlib hist() draws histogram

hist(x, bins=None, range=None,......)

x: input sequence, if it is a two-dimensional image, it needs to be expanded into a one-dimensional array;
bins: the number of pillars, if it is a CV_8U type image, set to 256, indicating that each pixel value is an interval;
range: the threshold range of the pixel value , it will be calculated automatically if not set;

The example reads in the lena image, and then draws a histogram of its BGR channel respectively. When drawing the histogram, the input parameter x is required to be a one-dimensional array, so the ravel() method is used to expand the image:

img_src = cv2.imread('..\\lena.jpg')
b,g,r = cv2.split(img_src)  

#显示图像
fig,ax = plt.subplots(2,2)
ax[0,0].set_title('b hist')
ax[0,0].hist(b.ravel(),bins=256)  
ax[0,1].set_title('g hist')
ax[0,1].hist(g.ravel(),bins=256)
ax[1,0].set_title('r hist')
ax[1,0].hist(r.ravel(),bins=256)
ax[1,1].set_title('src') 
ax[1,1].imshow(cv2.cvtColor(img_src,cv2.COLOR_BGR2RGB))  
#ax[0,0].axis('off');ax[0,1].axis('off');ax[1,0].axis('off');
ax[1,1].axis('off')#关闭坐标轴显示
plt.show() 

Calculate histogram calcHist()

cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) ->hist

images: Input image is a collection of images, which can be a list or tuple containing multi-channel color images, or a list or tuple composed of multiple grayscale images; input in the form of list or tuple; channels: determined according to images,
specified Which channel number in images to use is determined according to the form of images; input in the form of list or tuple;
mask: mask;
histSize: the size of the histogram, which is actually the equal division of the element values; input in the form of list or tuple ;
ranges: the range of image element values; input in the form of list or tuple;
accumulate: if True represents the number of pixel values ​​accumulated when multiple images are used;
hist: the returned histogram data is a two-dimensional array, the shape of the array is (the number of rows determined by histSize, 1);

histSize = 256
histRange = (0, histSize)  #统计的范围和histSize保持一致时可覆盖所有取值
b_hist = cv2.calcHist([img_src], [0], None, [histSize], histRange) 
g_hist = cv2.calcHist([img_src], [1], None, [histSize], histRange)
r_hist = cv2.calcHist([img_src], [2], None, [histSize], histRange)

b,g,r = cv2.split(img_src)  
b_hist2 = cv2.calcHist([b], [0], None, [histSize], histRange) 
g_hist2 = cv2.calcHist([g], [0], None, [histSize], histRange)
r_hist2 = cv2.calcHist([r], [0], None, [histSize], histRange) 

calcHist() calculates matplotlib plot() display

b,g,r = cv2.split(img_src)  
histSize = 256
histRange = (0, histSize) 统计的范围和histSize保持一致时可覆盖所有取值
b_hist = cv2.calcHist([b], [0], None, [histSize], histRange) 
g_hist = cv2.calcHist([g], [0], None, [histSize], histRange) 
r_hist = cv2.calcHist([r], [0], None, [histSize], histRange) 

opencv plot showing histogram
2D ​​histogram

Histogram equalization

equalizeHist() can achieve histogram equalization of the image. It is a global histogram equalization that considers the entire image.

cv2.equalizeHist(src[, dst]) ->dst

src: input image, 8-bit single channel;
dst: equalized output image, the same type as src;

Adaptive Histogram Equalization createCLAHE
CLAHE is the abbreviation of Contrast Limited Adaptive Histogram Equalization (Contrast Limited Adaptive Histogram Equalization). Different from ordinary histogram equalization, it is a local histogram equalization method.
Calling the interface is divided into two steps. First create an instance and then call the apply() method:

cv2.createCLAHE([, clipLimit[, tileGridSize]]) ->retval
dst=retval.apply(src)

clipLimit: comparison limit threshold, the default is 40;
tileGridSize: the grid size for histogram equalization, the input image will be divided according to this size for local histogram equalization, the default is 8×8 size;
src: input image, 8bit single Channel;
dst: equalized output image, the same type as src;

#普通直方图均衡
img_dst = cv2.equalizeHist(img_src)
#自适应直方图均衡 
clahe = cv2.createCLAHE(clipLimit=5.0, tileGridSize=(8,8))
img_dst_clahe = clahe.apply(img_src)

histSize = 256
histRange = (0, 256) 
hist_src = cv2.calcHist([img_src], [0], None, [histSize], histRange) 
hist_dst = cv2.calcHist([img_dst], [0], None, [histSize], histRange)  
hist_dst_clahe = cv2.calcHist([img_dst_clahe], [0], None, [histSize], histRange)  

Histogram comparison compareHIST
uses compareHist() to compare the correlation of two images from the perspective of histogram. The comparison object can be a 1D or 2D histogram.

cv2.compareHist(H1, H2, method) ->retval

H1: input image histogram;
H2: input image histogram, the same size as H1;
method: comparison method; contains 6 methods, values ​​0~5
enum cv::HistCompMethods { cv::HISTCMP_CORREL = 0, cv:: HISTCMP_CHISQR = 1, cv::HISTCMP_INTERSECT = 2, cv::HISTCMP_BHATTACHARYYA = 3, cv::HISTCMP_HELLINGER = HISTCMP_BHATTACHARYYA, cv::HISTCMP_CHISQR_ALT = 4, cv::HISTCMP_KL_DIV = 5 }







Histogram backprojection calcBackProject
backprojection is used to calculate the degree of matching between the pixels of the source image and the pixel distribution in the feature image histogram. It returns an image with the same dimensions as the input image, where each pixel corresponds to the probability that the pixel belongs to the feature image. The higher the probability, the closer it is to the object to be found, which can be used for image segmentation or finding areas of interest.

cv2.calcBackProject(images,channels,hist,ranges,scale[,dst])->dst

images: Input image is a collection of images, which can be a list or tuple containing multi-channel color images, or a list or tuple composed of multiple grayscale images; input channels in the form of list or tuple: determined according to images, specify the
desired Which channel number in images is used is determined according to the form of images; input in list or tuple form;
hist: input histogram;
ranges: range of image element values; list or tuple containing 2 elements;
scale: scaling ratio;
dst: target image, single channel, the same size and depth as images[0]; the
input parameters images, channels, and ranges are used the same as calcHist().

The input parameter hist is the histogram of the feature image, which is used to find the feature in the source image.

Guess you like

Origin blog.csdn.net/aqiangdeba/article/details/129779545