Classification and recognition of simple KNN under Opencv-python

Source: Internet
Author: User

KNN is a simple algorithm used to classify data mining, which is used to realize the simple recognition of 4 flowers in this cluster.

Environment: PYTHON2.7+OPENCV3.0+WINDOWS10

Principle: After the use of KNN function to extract 4 flower feature points, the need to identify the image to extract the signs points, and the gallery of 4 flowers, compared to the category of the most matching points are considered similar.

Code:

Read in image data:

1 2IMG =cv2.imread (name)3   4Q_img=[1]*105Q_img[0] = Cv2.imread ("images/qiangwei1.jpg")6Q_IMG[1] = Cv2.imread ("images/qiangwei2.jpg")7Q_IMG[2] = Cv2.imread ("images/qiangwei3.jpg")8Q_IMG[3] = Cv2.imread ("images/qiangwei4.jpg")9Q_IMG[4] = Cv2.imread ("images/qiangwei5.jpg")Ten    OneX_img=[1]*10 AX_img[0] = Cv2.imread ("images/xinghua1.jpg") -X_IMG[1] = Cv2.imread ("images/xinghua2.jpg") -X_IMG[2] = Cv2.imread ("images/xinghua3.jpg") theX_IMG[3] = Cv2.imread ("images/xinghua4.jpg") -X_IMG[4] = Cv2.imread ("images/xinghua5.jpg") -  -T_img=[1]*10 +T_img[0] = Cv2.imread ("images/taohua1.jpg") -T_IMG[1] = Cv2.imread ("images/taohua2.jpg") +T_IMG[2] = Cv2.imread ("images/taohua3.jpg") AT_IMG[3] = Cv2.imread ("images/taohua4.jpg") atT_IMG[4] = Cv2.imread ("images/taohua5.jpg") -  -Y_img=[1]*10 -Y_img[0] = Cv2.imread ("images/yinghua1.jpg") -Y_IMG[1] = Cv2.imread ("images/yinghua2.jpg") -Y_IMG[2] = Cv2.imread ("images/yinghua3.jpg") inY_IMG[3] = Cv2.imread ("images/yinghua4.jpg") -Y_IMG[4] = Cv2.imread ("images/yinghua5.jpg")

To get a grayscale image:

Gray =Cv2.cvtcolor (Img,cv2. Color_bgr2gray) Q_gray=[1]*10Q_gray[0]=Cv2.cvtcolor (Q_img[0],cv2. Color_bgr2gray) q_gray[1] = Cv2.cvtcolor (q_img[1],cv2. Color_bgr2gray) q_gray[2] = Cv2.cvtcolor (q_img[2],cv2. Color_bgr2gray) q_gray[3] = Cv2.cvtcolor (q_img[3],cv2. Color_bgr2gray) q_gray[4] = Cv2.cvtcolor (q_img[4],cv2. Color_bgr2gray) X_gray=[1]*10X_gray[0]=Cv2.cvtcolor (X_img[0],cv2. Color_bgr2gray) x_gray[1] = Cv2.cvtcolor (x_img[1],cv2. Color_bgr2gray) x_gray[2] = Cv2.cvtcolor (x_img[2],cv2. Color_bgr2gray) x_gray[3] = Cv2.cvtcolor (x_img[3],cv2. Color_bgr2gray) x_gray[4] = Cv2.cvtcolor (x_img[4],cv2. Color_bgr2gray) T_gray=[1]*10T_gray[0]=Cv2.cvtcolor (T_img[0],cv2. Color_bgr2gray) t_gray[1] = Cv2.cvtcolor (t_img[1],cv2. Color_bgr2gray) t_gray[2] = Cv2.cvtcolor (t_img[2],cv2. Color_bgr2gray) t_gray[3] = Cv2.cvtcolor (t_img[3],cv2. Color_bgr2gray) t_gray[4] = Cv2.cvtcolor (t_img[4],cv2. Color_bgr2gray) Y_gray=[1]*10Y_gray[0]=Cv2.cvtcolor (Y_img[0],cv2. Color_bgr2gray) y_gray[1] = Cv2.cvtcolor (y_img[1],cv2. Color_bgr2gray) y_gray[2] = Cv2.cvtcolor (y_img[2],cv2. Color_bgr2gray) y_gray[3] = Cv2.cvtcolor (y_img[3],cv2. Color_bgr2gray) y_gray[4] = Cv2.cvtcolor (y_img[4],cv2. Color_bgr2gray)

Get Keypoints,descriptor:

Detect = Cv2.xfeatures2d.SIFT_create (800) Kp,des=Detect.detectandcompute (gray,none) Q_KP=[1]*10Q_des=[1]*10Q_kp[0],q_des[0]=Detect.detectandcompute (q_gray[0],none) q_kp[1],Q_DES[1] = Detect.detectandcompute (q_gray[1],none) q_kp[2],Q_DES[2] = Detect.detectandcompute (q_gray[2],none) q_kp[3],Q_DES[3] = Detect.detectandcompute (q_gray[3],none) q_kp[4],Q_DES[4] = Detect.detectandcompute (q_gray[4],none) X_KP=[1]*10X_des=[1]*10X_kp[0],x_des[0]=Detect.detectandcompute (x_gray[0],none) x_kp[1],X_DES[1] = Detect.detectandcompute (x_gray[1],none) x_kp[2],X_DES[2] = Detect.detectandcompute (x_gray[2],none) x_kp[3],X_DES[3] = Detect.detectandcompute (x_gray[3],none) x_kp[4],X_DES[4] = Detect.detectandcompute (x_gray[4],none) T_KP=[1]*10T_des=[1]*10T_kp[0],t_des[0]=Detect.detectandcompute (t_gray[0],none) t_kp[1],T_DES[1] = Detect.detectandcompute (t_gray[1],none) t_kp[2],T_DES[2] = Detect.detectandcompute (t_gray[2],none) t_kp[3],T_DES[3] = Detect.detectandcompute (t_gray[3],none) t_kp[4],T_DES[4] = Detect.detectandcompute (t_gray[4],none) Y_KP=[1]*10Y_des=[1]*10Y_kp[0],y_des[0]=Detect.detectandcompute (y_gray[0],none) y_kp[1],Y_DES[1] = Detect.detectandcompute (y_gray[1],none) y_kp[2],Y_DES[2] = Detect.detectandcompute (y_gray[2],none) y_kp[3],Y_DES[3] = Detect.detectandcompute (y_gray[3],none) y_kp[3],Y_DES[4] = Detect.detectandcompute (Y_gray[4],none)

To match using the KNN match class:

BF =Cv2. Bfmatcher () q_matches=[1]*10Q_matches[0]= Bf.knnmatch (des,q_des[0],k=2) q_matches[1]= Bf.knnmatch (des,q_des[1],k=2) q_matches[2]= Bf.knnmatch (des,q_des[2],k=2) q_matches[3]= Bf.knnmatch (des,q_des[3],k=2) q_matches[4]= Bf.knnmatch (des,q_des[4],k=2) X_matches=[1]*10X_matches[0]= Bf.knnmatch (des,x_des[0],k=2) x_matches[1]= Bf.knnmatch (des,x_des[1],k=2) x_matches[2]= Bf.knnmatch (des,x_des[2],k=2) x_matches[3]= Bf.knnmatch (des,x_des[3],k=2) x_matches[4]= Bf.knnmatch (des,x_des[4],k=2) T_matches=[1]*10T_matches[0]= Bf.knnmatch (des,t_des[0],k=2) t_matches[1]= Bf.knnmatch (des,t_des[1],k=2) t_matches[2]= Bf.knnmatch (des,t_des[2],k=2) t_matches[3]= Bf.knnmatch (des,t_des[3],k=2) t_matches[4]= Bf.knnmatch (des,t_des[4],k=2) Y_matches=[1]*10Y_matches[0]= Bf.knnmatch (des,y_des[0],k=2) y_matches[1]= Bf.knnmatch (des,y_des[1],k=2) y_matches[2]= Bf.knnmatch (des,y_des[2],k=2) y_matches[3]= Bf.knnmatch (des,y_des[3],k=2) y_matches[4]= Bf.knnmatch (des,y_des[4],k=2)  

Record and filter the matching points:

sum1=0 sum2=0 sum3=0 sum4=0 forIinchRange (5):       forM,ninchQ_matches[i]:ifM.distance < 0.55*N.distance:sum1=sum1+1 forIinchRange (5):       forM,ninchX_matches[i]:ifM.distance < 0.55*n.distance:sum2=sum2+1 forIinchRange (5):       forM,ninchT_matches[i]:ifM.distance < 0.55*n.distance:sum3=sum3+1 forIinchRange (5):       forM,ninchY_matches[i]:ifM.distance < 0.55*n.distance:sum4=sum4+1

return Result:

ifMax (SUM1,SUM2,SUM3,SUM4) = =sum1:return "Rose"      ifMax (SUM1,SUM2,SUM3,SUM4) = =sum2:return "Apricot Blossom"      ifMax (SUM1,SUM2,SUM3,SUM4) = =sum3:return "Peach Blossom"      ifMax (SUM1,SUM2,SUM3,SUM4) = =SUM4:return "Sakura"

GUI uses a simple page developed using Wxformbuilder+wxpython

Final file:

As follows:


The recognition rate is not very high because the picture gallery is small and the algorithm is simple.

Classification and recognition of simple KNN under Opencv-python

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.