Note

Find Color 본문

Deep Learning/Computer Vision

Find Color

알 수 없는 사용자 2022. 5. 16. 20:04
728x90
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import PIL
%matplotlib inline
from sklearn.cluster import KMeans

clt = KMeans(n_clusters=5)
clt.fit(img.reshape(-1,3))

def show_img_compar(img_1, img_2 ):
    f, ax = plt.subplots(1, 2, figsize=(10,10))
    ax[0].imshow(img_1)
    ax[1].imshow(img_2)
    ax[0].axis('off') #hide the axis
    ax[1].axis('off')
    f.tight_layout()
    

img = cv.imread("test1.jpg")
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img_2 = cv.imread("test2.jpg")
img_2 = cv.cvtColor(img_2, cv.COLOR_BGR2RGB)
 
dim = (500, 300)
# resize image
img = cv.resize(img, dim, interpolation = cv.INTER_AREA)
img_2 = cv.resize(img_2, dim, interpolation = cv.INTER_AREA)
 
show_img_compar(img, img_2)

# 평균 픽셀 값

img_temp = img.copy()
img_temp[:,:,0], img_temp[:,:,1], img_temp[:,:,2] = np.average(img, axis=(0,1))
 
img_temp_2 = img_2.copy()
img_temp_2[:,:,0], img_temp_2[:,:,1], img_temp_2[:,:,2] = np.average(img_2, axis=(0,1))
 
show_img_compar(img, img_temp)
show_img_compar(img_2, img_temp_2)

# 가장 많은 빈도 픽셀 값

img_temp = img.copy()
unique, counts = np.unique(img_temp.reshape(-1, 3), axis=0, return_counts=True)
img_temp[:,:,0], img_temp[:,:,1], img_temp[:,:,2] = unique[np.argmax(counts)]
 
img_temp_2 = img_2.copy()
unique, counts = np.unique(img_temp_2.reshape(-1, 3), axis=0, return_counts=True)
img_temp_2[:,:,0], img_temp_2[:,:,1], img_temp_2[:,:,2] = unique[np.argmax(counts)]
 
show_img_compar(img, img_temp)
show_img_compar(img_2, img_temp_2)

# kmeans 활용한 픽셀 값

def palette(clusters):
    width=300
    palette = np.zeros((50, width, 3), np.uint8)
    steps = width/clusters.cluster_centers_.shape[0]
    for idx, centers in enumerate(clusters.cluster_centers_): 
        palette[:, int(idx*steps):(int((idx+1)*steps)), :] = centers
    return palette
 
  

clt_1 = clt.fit(img.reshape(-1, 3))
show_img_compar(img, palette(clt_1))
 
clt_2 = clt.fit(img_2.reshape(-1, 3))
show_img_compar(img_2, palette(clt_2))

# kmeans + 픽셀 비율

from collections import Counter
 
def palette_perc(k_cluster):
    width = 300
    palette = np.zeros((50, width, 3), np.uint8)
    
    n_pixels = len(k_cluster.labels_)
    counter = Counter(k_cluster.labels_) # count how many pixels per cluster
    perc = {}
    for i in counter:
        perc[i] = np.round(counter[i]/n_pixels, 2)
    perc = dict(sorted(perc.items()))
    
    #for logging purposes
    print(perc)
    print(k_cluster.cluster_centers_)
    
    step = 0
    
    for idx, centers in enumerate(k_cluster.cluster_centers_): 
        palette[:, step:int(step + perc[idx]*width+1), :] = centers
        step += int(perc[idx]*width+1)
        
    return palette
    
clt_1 = clt.fit(img.reshape(-1, 3))
show_img_compar(img, palette_perc(clt_1))
 
clt_2 = clt.fit(img_2.reshape(-1, 3))
show_img_compar(img_2, palette_perc(clt_2))

 

평균 픽셀

가장 많은 빈도 픽셀 값

kmeans = 5 픽셀 값

kmeans + 픽셀 비율

 

'Deep Learning > Computer Vision' 카테고리의 다른 글

OpenCV (3) - 이미지 위치 변형  (0) 2022.05.28
OpenCV (2) - 이미지 크기 변형  (0) 2022.05.27
OpenCV (1) - 튜토리얼  (0) 2022.05.26
easyocr 텍스트 추출  (0) 2022.05.12
pytesseract 텍스트 추출  (0) 2022.05.10
Comments