Most dominant color in RGB image - OpenCV / NumPy / Python

后端 未结 3 1386
广开言路
广开言路 2020-12-14 04:05

I have a python image processing function, that uses tries to get the dominant color of an image. I make use of a function I found here https://github.com/tarikd/python-kmea

3条回答
  •  遥遥无期
    2020-12-14 04:39

    @Divakar has given a great answer. But if you want to port your own code to OpenCV, then:

        img = cv2.imread('image.jpg',cv2.IMREAD_UNCHANGED)
    
        data = np.reshape(img, (-1,3))
        print(data.shape)
        data = np.float32(data)
    
        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
        flags = cv2.KMEANS_RANDOM_CENTERS
        compactness,labels,centers = cv2.kmeans(data,1,None,criteria,10,flags)
    
        print('Dominant color is: bgr({})'.format(centers[0].astype(np.int32)))
    

    Result for your image:

    Dominant color is: bgr([41 31 23])

    Time it took: 0.10798478126525879 secs

提交回复
热议问题