PYTHON+OPENCV image Processing (i.)

Source: Internet
Author: User
Tags abs scale image

First, what is OPENCV?

Open Source Computer Vision LIBRARY.OPENCV was established by Intel in 1999 and is now supported by Willow garage. OpenCV is a BSD licensing (open source) release of the cross-platform computer Vision library that can run on Linux, Windows, MacOS operating systems. It is lightweight and efficient-consisting of a series of C functions and a small number of C + + classes, and provides interfaces to Python, Ruby, Matlab, and many more general-purpose algorithms for image processing and computer vision. The latest version is 3.1, released January 29, 2016. (Quoted from Baidu Encyclopedia OpenCV)

In short, the OPENCV can realize the editing of computer image and video. Widely used in image recognition, motion tracking, machine vision and other fields.

Second, installation

Install directly with PIP

Pip Install numpy matplotlib #由于opencv依赖numpypip install opencv-python #或者使用国内镜像pip install opencv-python-i  https:/ /pypi.douban.com/simple

After the installation is complete, then the command line interpreter input:

Import Cv2

If no module error is indicated, the installation is successful

Test script:

Import Cv2   #导入模块, OpenCV's Python module is called cv2imgobj = Cv2.imread (' pho.jpg ') #读取图像cv2. Namedwindow ("image") # Create the window and display the image type Cv2.imshow ("image", Imgobj) Cv2.waitkey (0)        #等待事件触发, parameter 0 means to wait forever Cv2.destroyallwindows ()   #释放窗口

Third, the study website:

https://www.cnblogs.com/lclblack/p/6377710.html OPENCV Environment Construction

Http://www.opencv.org.cn Official website Chinese Course

77876080 Python matplotlib Easy Tutorial

http://python.jobbole.com/87471/Python Quick tutorials for deep learning beginners-numpy and Matplotlib

56854985 differences between single and triple channels

Iv. Correlation of image contrast

Related background
To identify two similar images, what is the process of perceptual discussion? First of all, we will distinguish between the two types of photos, such as a landscape or a portrait. Landscape, is the desert or the sea, the figures in the picture, two people are not all the face of the state, or oval face (or inverted oval face ... Ha ha...... )。

So from the machine's point of view, the image is identified and then compared.

Clearly, in the absence of a trained computer (i.e. model building), it is difficult to distinguish between what is the ocean and what is desert. But the computer can easily recognize the pixel values of the image.

Therefore, in the image recognition, the color characteristic is the most commonly used. (other commonly used features are texture features, shape features, spatial relationship characteristics, etc.)

Which is divided into

Histogram
Color Set
Color moment
Aggregation vectors
Related diagrams

1. Histogram

In Python, using the Calchist () method in OpenCV to get its histogram data, the result is a list, using Matplotlib, to draw the histogram data graph of the two graphs

Import cv2import numpyfrom matplotlib import pyplotif __name__ = = ' __main__ ':    imgobj1 = Cv2.imread (' pho.jpg ')    Imgobj2 = Cv2.imread (' ph1.jpg ')    hist1 = Cv2.calchist ([imgobj1], [0], None, [[[]], [0.0, 255.0])    hist2 = Cv2.calch IST ([imgobj2], [0], None, [up], [0.0, 255.0])    Pyplot.plot (range (a), Hist1, ' R ')    Pyplot.plot (range (256), Hist2, ' B ')    pyplot.show ()    cv2.imshow (' img1 ', imgobj1)    cv2.imshow (' Img2 ', imgobj2)    cv2.waitkey (0)

  

2. What is the effect of gray scale diagram?

A grayscale chart is a picture that contains only black and white colors, and 0~255 brightness levels. Gray-scale image has a small storage, its luminance value is 256 color palette index number, from the whole image of the overall and local color and brightness level distribution characteristics, gray-scale image description and Color map description is consistent characteristics. So a lot of real color image analysis, the first step is to convert to grayscale, and then analysis. True Color, because it is 24 bits, 2 (^8) * 2 (^8) * 2 (^8) = 16777216 colors, need palette 16777216 * 4byte bytes of space is 64MB palette space, so true color is not a palette. For example, for video target tracking and recognition, the first step is to convert to a grayscale image. The existing mature analysis algorithm is based on gray image, gray-scale image synthesis of true color bitmap RGB channel information.

(a): Single channel diagram,

Commonly known as grayscale, each pixel can only have a value to represent the color, its pixel value between 0 to 255, 0 is black, 255 is white, the median is some different levels of gray. (There are also 3-channel grayscale, 3-channel grayscale, only one channel has a value, the other two channels have a value of 0).

(b): three-channel graph, each pixel point has 3 values, so is 3 channels. There are also 4-channel graphs. For example, the RGB image is a three-channel picture, RGB color mode is a color standard industry, is through the red (R), Green (G), Blue (B) three color channels and their superposition to get a variety of colors, RGB is the red, green, blue three channels of color, This standard almost includes all the colors that human vision can perceive, and is one of the most widely used color systems. In summary, each point is represented by a value of three.

3, the use of the histogram to determine whether two pictures are similar to the method is to calculate the degree of coincidence of its histogram can be.
The calculation method is as follows:

Where GI and Si are respectively referred to as the first point of the two curves.

The result of the final calculation is the similarity.

However, there is an obvious weakness in this approach, that is, he is based on the global distribution of color, can not describe the color of the local distribution and color in place.

That is, if a picture is dominated by blue, the content is a blue sky, and the other picture is blue, but the content is the girl wearing a blue skirt, then this algorithm is likely to think of the two pictures similar.

One way to mitigate this weakness is to use the image's crop method to divide the image evenly, then calculate its similarity separately, and then consider it synthetically.

Image fingerprint and Hamming distance
Before introducing the other methods to discriminate the similarity, some concepts should be added. The first one is the image fingerprint.

Image fingerprint and human fingerprint, is the identity of the symbol, and the image fingerprint simple point of view, is the image according to a certain hash algorithm, after the operation of a set of binary numbers.

In this case, the concept of Hamming distance can be drawn in passing.

If a set of binary data is 101, the other group is 111, then it is obvious that the first group of the second data 0 to 1 can be converted into a second set of data 111, so the Hamming distance of the two sets of data is 1

To put it simply, Hamming distance is the number of steps required to turn a set of binary data into another set of data, obviously, this value can measure the difference between two pictures, the smaller the Hamming distance, the higher the similarity. Hamming distance is 0, which means two pictures exactly the same.

How to calculate the Hamming distance, see the following three hashing algorithms

Average hash method (Ahash)
This algorithm is based on the comparison of each pixel and average value of the grayscale graph.

General steps:

1. Zoom the picture, the general size is 8*8,64 pixel value.
2. Convert to Grayscale
3. Calculate average: Calculates the average of all pixels of a picture after grayscale processing, and calculates it directly using the mean () in NumPy.
4. Compare pixel grayscale values: traverse the grayscale image each pixel, if the greater than average record is 1, otherwise 0.
5. Get information fingerprint: Combine 64 bit bits, order to maintain consistency.
Finally, compared to the fingerprints of two pictures, the Hamming distance can be obtained.

Perceptual hashing Algorithm (Phash)
The average hashing algorithm is too strict, imprecise, and more suitable for searching thumbnails, in order to obtain more accurate results can choose the perceptual hashing algorithm, it uses DCT (discrete cosine transform) to reduce the frequency of the method

General steps:

Zoom Out: 32 * 32 is a better size, so convenient for DCT computing
Convert to Grayscale Map
Compute DCT: Using the DCT () method provided in the OPENCV, note that the input image must be a 32-bit floating point type, so the conversion is first made with the float32 in NumPy
Reduced DCT:DCT computed Matrix is 32 * 32, leaving the upper left corner of 8 * 8, these represent the lowest frequency of the picture
Calculate average: Calculates the average of all pixels after the narrowing of the DCT.
Further reduce the DCT: the greater than the average record is 1, whereas the record is 0.
Get information fingerprint: Combine 64 information bits, order to maintain consistency.
Finally, compared to the fingerprints of two pictures, the Hamming distance can be obtained.

Dhash algorithm
Compared to the Phash,dhash speed is much faster, compared to ahash,dhash in the efficiency of almost the same situation, the effect is better, it is based on the gradient implementation.

Steps:

Shrink Picture: Shrinks to 9*8 size so that it has a 72 pixel dot
Convert to Grayscale Map
Calculate Variance Value: The Dhash algorithm works between neighboring pixels, resulting in 8 different differences between 9 pixels per line, with a total of 8 rows, resulting in 64 differential values
Get fingerprints: If the left pixel is brighter than the right, the record is 1, otherwise 0.
Finally, compared to the fingerprints of two pictures, the Hamming distance can be obtained.

Import Cv2import NumPy as Npfrom matplotlib import pyplot# The simplest implementation of the Huidu histogram as a similar comparison def classify_gray_hist (Image1,image2, Size = (256,256)): #先调整大小 image1 = cv2.resize (image1,size) image2 = Cv2.resize (image2,size) Hist1 = Cv2.calchi St ([image1],[0],none,[256],[0.0,255.0]) Hist2 = Cv2.calchist ([image2],[0],none,[256],[0.0,255.0]) #比较直方图 PYPLOT.P Lot (range, hist1, ' R ') Pyplot.plot (range (0), hist2, ' y ') pyplot.show () #计算直方图的重合度 degree = for i in RA Nge (Len (hist1)): if hist1[i]! = Hist2[i]: Degree = degree + (n (ABS (Hist1[i]-hist2[i))/max (hist1[i],hist 2[i])) Else:degree = degree + 1 degree = Degree/len (hist1) return degreedef classify_hist_with_s Plit (image1,image2,size = (256,256)): #将图像resize后, classified as three channels, and then calculated similar values for each channel Image1 = cv2.resize (image1,size) Image2 = C V2.resize (image2,size) sub_image1 = Cv2.split (image1) sub_image2 = Cv2.split (image2) sub_data = 0 for im1,im2 In Zip (sub_image1,sub_image2): Sub_data + = calculate (im1,im2) Sub_data = SUB_DATA/3 return sub_datadef calculate (im1,im2): Hist1 = Cv2. Calchist ([im1],[0],none,[256],[0.0,255.0]) Hist2 = Cv2.calchist ([im2],[0],none,[256],[0.0,255.0]) Pyplot.plot (        Range (0), Hist1, ' R ') Pyplot.plot (range, hist2, ' y ') pyplot.show () degree = for i in range (len (hist1)):        If hist1[i]! = Hist2[i]: Degree = degree + (1-(ABS (Hist1[i]-hist2[i])/MAX (Hist1[i], hist2[i]))) Else:degree = degree + 1 degree = Degree/len (hist1) return degree# average hash algorithm calculates def classify_ahash (imag E1,IMAGE2): Image1 = Cv2.resize (Image1, (8,8)) Image2 = Cv2.resize (Image2, (8,8)) Gray1 = Cv2.cvtcolor (image1,cv2.c Olor_bgr2gray) #切换至灰度图 gray2 = Cv2.cvtcolor (image2,cv2. Color_bgr2gray) Hash1 = Gethash (gray1) Hash2 = Gethash (gray2) return Hamming_distance (HASH1,HASH2) def classify_ph Ash (image1,image2): Image1 = Cv2.resize (Image1, (32,32)) Image2 = Cv2.resize (Image2, (32,) Gray1 = Cv2.cvtcolor (image1,cv2. Color_bgr2gray) #切换至灰度图 gray2 = Cv2.cvtcolor (image2,cv2. Color_bgr2gray) # Convert grayscale to floating-point, then DCT transform Dct1 = CV2.DCT (Np.float32 (gray1)) Dct2 = CV2.DCT (Np.float32 (Gray2)) # take top left Corner of the 8*8, these represent the lowest frequency of the picture # This operation is equivalent to the mask operation implemented by OPENCV in C + + mask operation # in Python, you can directly remove a part of the image matrix Dct1_roi = Dct1[0:8,0:8] DCT 2_roi = Dct2[0:8,0:8] Print (dct1) Hash1 = Gethash (dct1_roi) Hash2 = Gethash (Dct2_roi) return hamming_distance ( HASH1,HASH2) # Enter a grayscale image, return hashdef gethash (image): Avreage = Np.mean (image) #计算像素平均值 hash = [] for I in range (IMAGE.S            Hape[0]): for j in Range (Image.shape[1]): If Image[i, J] > Avreage:hash.append (1) Else:hash.append (0) return hash# calculate Hamming distance def hamming_distance (Hash1, hash2): num = 0 for in Dex in range (len (HASH1)): if hash1[index]! = Hash2[index]: num + = 1 return num# difference-aware algorithm def dhash (image1 , image2): Image1 = Cv2.resize (Image1, (9,8)) Image2 = Cv2.resize (Image2, (9,8)) Gray1 = Cv2.cvtcolor (image1,cv2. Color_bgr2gray) #切换至灰度图 gray2 = Cv2.cvtcolor (image2,cv2. Color_bgr2gray) Hash1 = Dhashcaulate (gray1) Hash2 = Dhashcaulate (gray2) return Hamming_distance (HASH1,HASH2) def D Hashcaulate (Gray): Hash_str = "For I in Range (8): for J in Range (8): If Gray[i, j] > Gray[i, J + 1]: Hash_str = hash_str + ' 1 ' else:hash_str = hash_str + ' 0 ' return hash_ Strif __name__ = = ' __main__ ': imgobj1 = Cv2.imread (' one.jpg ') imgobj2 = Cv2.imread (' three.jpg ') cv2.imshow (' Img1 ') , imgobj1) cv2.imshow (' Img2 ', imgobj2) #degree = Classify_gray_hist (imgobj1,imgobj2) #单通道直方图 #degree = Classify_h Ist_with_split (imgobj1,imgobj2) #三通道直方图 #degree = Classify_ahash (imgobj1,imgobj2) #平均哈希法 #degree = Classif Y_phash (imgobj1,imgobj2) #感知哈希法 degree = Dhash (imgobj1,imgobj2) #差值感知哈希法 print (degree) cv2.wait Key (0)

How the variance is calculated:

import cv2import Matplotlib.pyplot as plt# calculates the variance def getss (list): #计算平均值 avg=sum (list)/len (list) #定义方差变量ss, initial value is 0 ss=0 #计算方差 for L in list:ss+= (L-AVG) * (L-AVG)/len (list) #返回方差 ret Urn ss# gets the pixel mean per line def getdiff (IMG): #定义边长 sidelength=30 #缩放图像 img=cv2.resize (IMG, (sidelength,sidelength), Interpo Lation=cv2. Inter_cubic) #灰度处理 Gray=cv2.cvtcolor (Img,cv2. Color_bgr2gray) #avglist列表保存每行像素平均值 avglist=[] #计算每行均值, save to Avglist list for I in range (sidelength): Avg=sum (Gray[i])/len (Gray[i]) avglist.append (avg) #返回avglist平均值 return avglist# read test picture img1=cv2.imread ("one.jpg") diff1 =getdiff (IMG1) print (' Img1: ', GETSS (diff1)) #读取测试图片img11 =cv2.imread ("Two.jpg") Diff11=getdiff (img11) print (' img11: ') , GETSS (DIFF11)) X=range (() plt.figure ("avg") Plt.plot (x,diff1,marker= "*", label= "$walk 01$") Plt.plot (X,DIFF11, Marker= "*", label= "$walk 03$") plt.title ("avg") Plt.legend () plt.show () cv2.waitkey (0) cv2.destroyallwindows () 

  

PYTHON+OPENCV image Processing (i.)

Related Article

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.