Page 456 - MDP2020-1
P. 456

|    인천전자마이스터고등학교  ·············································································································
            450

            image  =  cv2.imread("/home/pi/mdp/Image/rainbow.png")


            //  이미지파일의  색상  비율  연산
            image  =  cv2.cvtColor(image,  cv2.COLOR_BGR2RGB)

            image  =  image.reshape((image.shape[0]  *  image.shape[1],  3))
            k  =  2
            clt  =  KMeans(n_clusters  =  k)
            clt.fit(image)

            for  center  in  clt.cluster_centers_:
                    print(center)
            def  centroid_histogram(clt):
                    numLabels  =  np.arange(0,  len(np.unique(clt.labels_))  +  1)
                    (hist,  _)  =  np.histogram(clt.labels_,  bins=numLabels)
                    hist  =  hist.astype("float")

                    hist  /=  hist.sum()
                    return  hist


            hist  =  centroid_histogram(clt)

            print(clt)
            print(hist)


            //  색상  비율을  그래프로  출력
            def  plot_colors(hist,  centroids):
                    bar  =  np.zeros((50,  300,  3),  dtype="uint8")

                    startX  =  0
                    for  (percent,  color)  in  zip(hist,  centroids):
                    endX  =  startX  +  (percent  *  300)
                    cv2.rectangle(bar,  (int(startX),  0),  (int(endX),  50),  color.astype("uint8").tolist(),  -1)
                    startX  =  endX

                    return  bar
            bar  =  plot_colors(hist,  clt.cluster_centers_)


            plt.figure()
            plt.axis("off")

            plt.imshow(bar)
            plt.show()


            cv2.waitKey(0)
   451   452   453   454   455   456   457   458   459   460   461