cv2.kmeans usage in Python

前端 未结 2 1733
萌比男神i
萌比男神i 2021-02-06 04:03

I am considering to use OpenCV\'s Kmeans implementation since it says to be faster...

Now I am using package cv2 and function kmeans,

I can not understand the pa

2条回答
  •  逝去的感伤
    2021-02-06 04:39

    the documentation on this function is almost impossible to find. I wrote the following Python code in a bit of a hurry, but it works on my machine. It generates two multi-variate Gaussian Distributions with different means and then classifies them using cv2.kmeans(). You may refer to this blog post to get some idea of the parameters.

    Handle imports:

    import cv
    import cv2
    import numpy as np
    import numpy.random as r
    

    Generate some random points and shape them appropriately:

    samples = cv.CreateMat(50, 2, cv.CV_32FC1)
    random_points = r.multivariate_normal((100,100), np.array([[150,400],[150,150]]), size=(25))
    random_points_2 = r.multivariate_normal((300,300), np.array([[150,400],[150,150]]), size=(25))   
    samples_list = np.append(random_points, random_points_2).reshape(50,2)  
    random_points_list = np.array(samples_list, np.float32) 
    samples = cv.fromarray(random_points_list)
    

    Plot the points before and after classification:

    blank_image = np.zeros((400,400,3))
    blank_image_classified = np.zeros((400,400,3))
    
    for point in random_points_list:
        cv2.circle(blank_image, (int(point[0]),int(point[1])), 1, (0,255,0),-1)
    
    temp, classified_points, means = cv2.kmeans(data=np.asarray(samples), K=2, bestLabels=None,
    criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 1, 10), attempts=1, 
    flags=cv2.KMEANS_RANDOM_CENTERS)   #Let OpenCV choose random centers for the clusters
    
    for point, allocation in zip(random_points_list, classified_points):
        if allocation == 0:
            color = (255,0,0)
        elif allocation == 1:
            color = (0,0,255)
        cv2.circle(blank_image_classified, (int(point[0]),int(point[1])), 1, color,-1)
    
    cv2.imshow("Points", blank_image)
    cv2.imshow("Points Classified", blank_image_classified)
    cv2.waitKey()
    

    Here you can see the original points:

    Points before classification

    Here are the points after they have been classified: Points after classification

    I hope that this answer may help you, it is not a complete guide to k-means, but it will at least show you how to pass the parameters to OpenCV.

提交回复
热议问题