opencv-python下简单KNN分类识别

KNN是数据挖掘中一种简单算法常用来分类,此次用来聚类实现对4种花的简单识别。

环境:python2.7+opencv3.0+windows10

原理:在使用KNN函数提取出4种花特征点以后,对需要辨认的图片提取体征点,与图库中4类花进行比较,匹配点最多的一类即视为同类。

代码:

读入图像数据:

 1 
 2     img =cv2.imread(name)
 3   
 4     q_img=[1]*10
 5     q_img[0] = cv2.imread("images/qiangwei1.jpg")
 6     q_img[1] = cv2.imread("images/qiangwei2.jpg")
 7     q_img[2] = cv2.imread("images/qiangwei3.jpg")
 8     q_img[3] = cv2.imread("images/qiangwei4.jpg")
 9     q_img[4] = cv2.imread("images/qiangwei5.jpg")
10   
11     x_img=[1]*10
12     x_img[0] = cv2.imread("images/xinghua1.jpg")
13     x_img[1] = cv2.imread("images/xinghua2.jpg")
14     x_img[2] = cv2.imread("images/xinghua3.jpg")
15     x_img[3] = cv2.imread("images/xinghua4.jpg")
16     x_img[4] = cv2.imread("images/xinghua5.jpg")
17 
18     t_img=[1]*10
19     t_img[0] = cv2.imread("images/taohua1.jpg")
20     t_img[1] = cv2.imread("images/taohua2.jpg")
21     t_img[2] = cv2.imread("images/taohua3.jpg")
22     t_img[3] = cv2.imread("images/taohua4.jpg")
23     t_img[4] = cv2.imread("images/taohua5.jpg")
24 
25     y_img=[1]*10
26     y_img[0] = cv2.imread("images/yinghua1.jpg")
27     y_img[1] = cv2.imread("images/yinghua2.jpg")
28     y_img[2] = cv2.imread("images/yinghua3.jpg")
29     y_img[3] = cv2.imread("images/yinghua4.jpg")
30     y_img[4] = cv2.imread("images/yinghua5.jpg")

 

获取灰度图:

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  
    q_gray=[1]*10
    q_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]*10
    x_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]*10
    t_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]*10
    y_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)

 

获取keypoints,descriptor:

    detect = cv2.xfeatures2d.SIFT_create(800)

    kp,des = detect.detectAndCompute(gray,None)
    
    
    q_kp=[1]*10
    q_des=[1]*10
    q_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]*10
    x_des=[1]*10
    x_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]*10
    t_des=[1]*10
    t_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]*10
    y_des=[1]*10
    y_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)

 

使用Knn匹配类进行匹配:

  bf = cv2.BFMatcher()
    q_matches=[1]*10
    q_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]*10
    x_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]*10
    t_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]*10
    y_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)
  

 

记录并对匹配点进行筛选:

sum1=0
    sum2=0
    sum3=0
    sum4=0
    for i in range(5):
      for m,n in q_matches[i]:
        if m.distance < 0.55*n.distance:
          sum1=sum1+1
  
    for i in range(5):
      for m,n in x_matches[i]:
        if m.distance < 0.55*n.distance:
          sum2=sum2+1
          
    for i in range(5):
      for m,n in t_matches[i]:
        if m.distance < 0.55*n.distance:
          sum3=sum3+1
  
    for i in range(5):
      for m,n in y_matches[i]:
        if m.distance < 0.55*n.distance:
          sum4=sum4+1

 

返回结果:

if max(sum1,sum2,sum3,sum4)==sum1:
      return "蔷薇"
  
    if max(sum1,sum2,sum3,sum4)==sum2:
      return "杏花"
  
    if max(sum1,sum2,sum3,sum4)==sum3:
      return "桃花"
  
    if max(sum1,sum2,sum3,sum4)==sum4:
      return "樱花"

 

gui使用利用wxformbuilder+wxpython开发的简单页面

最终文件:

opencv-python下简单KNN分类识别_第1张图片

效果图如下:

opencv-python下简单KNN分类识别_第2张图片opencv-python下简单KNN分类识别_第3张图片

由于图库图片较少且算法较为简单,识别率不会很高。

 

转载于:https://www.cnblogs.com/Aiden-/p/8871954.html

你可能感兴趣的:(opencv-python下简单KNN分类识别)