山内セミナー(2019/06/26)

関連サイトと資料

サンプル画像


landscape_1.jpg


landscape_2.jpg

サンプルプログラム(1)

k_means_clustering_data_visualization.py
import numpy as np
from matplotlib import pyplot as plt
	
data = np.float32(np.vstack(
    (np.random.randint(0, 40, (50, 2)), np.random.randint(30, 70, (50, 2)), np.random.randint(60, 100, (50, 2)))))
	
print(data)
fig = plt.figure(figsize=(6, 6))
plt.suptitle("K-means clustering algorithm", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
ax = plt.subplot(1, 1, 1)
plt.scatter(data[:, 0], data[:, 1], c='c')
plt.title("data to be clustered")
plt.show()

サンプルプログラム(2)

k_means_clustering_k_2.py
import numpy as np
import cv2
from matplotlib import pyplot as plt
	
data = np.float32(np.vstack(
    (np.random.randint(0, 40, (50, 2)), np.random.randint(30, 70, (50, 2)), np.random.randint(60, 100, (50, 2)))))
	
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)
ret, label, center = cv2.kmeans(data, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
A = data[label.ravel() == 0]
B = data[label.ravel() == 1]
	
fig = plt.figure(figsize=(12, 6))
plt.suptitle("K-means clustering algorithm", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
ax = plt.subplot(1, 2, 1)
plt.scatter(data[:, 0], data[:, 1], c='c')
plt.title("data")
	
ax = plt.subplot(1, 2, 2)
plt.scatter(A[:, 0], A[:, 1], c='b')
plt.scatter(B[:, 0], B[:, 1], c='g')
plt.scatter(center[:, 0], center[:, 1], s=100, c='m', marker='s')
plt.title("clustered data and centroids (K = 2)")
plt.show()

サンプルプログラム(3)

k_means_clustering_k_3.py
import numpy as np
import cv2
from matplotlib import pyplot as plt
	
data = np.float32(np.vstack(
    (np.random.randint(0, 40, (50, 2)), np.random.randint(30, 70, (50, 2)), np.random.randint(60, 100, (50, 2)))))
	
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)
ret, label, center = cv2.kmeans(data, 3, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
A = data[label.ravel() == 0]
B = data[label.ravel() == 1]
C = data[label.ravel() == 2]
	
fig = plt.figure(figsize=(12, 6))
plt.suptitle("K-means clustering algorithm", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
ax = plt.subplot(1, 2, 1)
plt.scatter(data[:, 0], data[:, 1], c='c')
plt.title("data")
	
ax = plt.subplot(1, 2, 2)
plt.scatter(A[:, 0], A[:, 1], c='b')
plt.scatter(B[:, 0], B[:, 1], c='g')
plt.scatter(C[:, 0], C[:, 1], c='r')
plt.scatter(center[:, 0], center[:, 1], s=100, c='m', marker='s')
plt.title("clustered data and centroids (K = 3)")
plt.show()

サンプルプログラム(4)

k_means_clustering_k_4.py
import numpy as np
import cv2
from matplotlib import pyplot as plt
	
data = np.float32(np.vstack(
    (np.random.randint(0, 40, (50, 2)), np.random.randint(30, 70, (50, 2)), np.random.randint(60, 100, (50, 2)))))
	
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)
ret, label, center = cv2.kmeans(data, 4, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
A = data[label.ravel() == 0]
B = data[label.ravel() == 1]
C = data[label.ravel() == 2]
D = data[label.ravel() == 3]
	
fig = plt.figure(figsize=(12, 6))
plt.suptitle("K-means clustering algorithm", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
ax = plt.subplot(1, 2, 1)
plt.scatter(data[:, 0], data[:, 1], c='c')
plt.title("data")
	
ax = plt.subplot(1, 2, 2)
plt.scatter(A[:, 0], A[:, 1], c='b')
plt.scatter(B[:, 0], B[:, 1], c='g')
plt.scatter(C[:, 0], C[:, 1], c='r')
plt.scatter(D[:, 0], D[:, 1], c='y')
plt.scatter(center[:, 0], center[:, 1], s=100, c='m', marker='s')
plt.title("clustered data and centroids (K = 4)")
plt.show()

サンプルプログラム(5)

data_generator.py
import numpy as np
	
def crateJSON(list1, filename):
    fp = open(filename, 'w')
    fp.write("[\n")
    n = len(list1)
    for i in range(n):
        s = '   { "x": ' + str(list1[i][0]) + ', "y": ' + str(list1[i][1]) + ' }'
        if i < (n-1):
            fp.write(s + ",\n")
        else:
            fp.write(s + "\n")
    fp.write("]\n")
    fp.close()
	
target = 'sample1.json'
data = np.float32(np.vstack(
    (np.random.randint(0, 40, (50, 2)), np.random.randint(30, 70, (50, 2)), np.random.randint(60, 100, (50, 2)))))
crateJSON(data, target)

サンプルプログラム(6)

data_viewer.py
import json
import numpy as np
from matplotlib import pyplot as plt
	
def loadJSON(filename):
    f = open(filename, 'r')
    jsonData = json.load(f)
    data = []
    for item in jsonData:
        x = item['x']
        y = item['y']
        v = [x, y]
        data.append(v)
    return np.float32(np.array(data))
	
target = 'sample1.json'
data = loadJSON(target)
	
fig = plt.figure(figsize=(6, 6))
plt.suptitle("K-means clustering algorithm", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
ax = plt.subplot(1, 1, 1)
plt.scatter(data[:, 0], data[:, 1], c='c')
plt.title("data to be clustered")
plt.show()

サンプルプログラム(7)

clustering_2.py
import numpy as np
import cv2
from matplotlib import pyplot as plt
import json
	
def loadJSON(filename):
    f = open(filename, 'r')
    jsonData = json.load(f)
    data = []
    for item in jsonData:
        x = item['x']
        y = item['y']
        v = [x, y]
        data.append(v)
    return np.float32(np.array(data))
	
target = 'sample1.json'
data = loadJSON(target)
	
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)
ret, label, center = cv2.kmeans(data, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
A = data[label.ravel() == 0]
B = data[label.ravel() == 1]
	
fig = plt.figure(figsize=(12, 6))
plt.suptitle("K-means clustering algorithm", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
ax = plt.subplot(1, 2, 1)
plt.scatter(data[:, 0], data[:, 1], c='c')
plt.title("data")
	
ax = plt.subplot(1, 2, 2)
plt.scatter(A[:, 0], A[:, 1], c='b')
plt.scatter(B[:, 0], B[:, 1], c='g')
plt.scatter(center[:, 0], center[:, 1], s=100, c='m', marker='s')
plt.title("clustered data and centroids (K = 2)")
plt.show()

サンプルプログラム(8)

clustering_3.py
import numpy as np
import cv2
from matplotlib import pyplot as plt
import json
	
def loadJSON(filename):
    f = open(filename, 'r')
    jsonData = json.load(f)
    data = []
    for item in jsonData:
        x = item['x']
        y = item['y']
        v = [x, y]
        data.append(v)
    return np.float32(np.array(data))
	
target = 'sample1.json'
data = loadJSON(target)
	
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)
ret, label, center = cv2.kmeans(data, 3, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
A = data[label.ravel() == 0]
B = data[label.ravel() == 1]
C = data[label.ravel() == 2]
	
fig = plt.figure(figsize=(12, 6))
plt.suptitle("K-means clustering algorithm", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
ax = plt.subplot(1, 2, 1)
plt.scatter(data[:, 0], data[:, 1], c='c')
plt.title("data")
	
ax = plt.subplot(1, 2, 2)
plt.scatter(A[:, 0], A[:, 1], c='b')
plt.scatter(B[:, 0], B[:, 1], c='g')
plt.scatter(C[:, 0], C[:, 1], c='r')
plt.scatter(center[:, 0], center[:, 1], s=100, c='m', marker='s')
plt.title("clustered data and centroids (K = 3)")
plt.show()

サンプルプログラム(9)

clustering_4.py
import numpy as np
import cv2
from matplotlib import pyplot as plt
import json
	
def loadJSON(filename):
    f = open(filename, 'r')
    jsonData = json.load(f)
    data = []
    for item in jsonData:
        x = item['x']
        y = item['y']
        v = [x, y]
        data.append(v)
    return np.float32(np.array(data))
	
target = 'sample1.json'
data = loadJSON(target)
	
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)
ret, label, center = cv2.kmeans(data, 4, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
A = data[label.ravel() == 0]
B = data[label.ravel() == 1]
C = data[label.ravel() == 2]
D = data[label.ravel() == 3]
	
fig = plt.figure(figsize=(12, 6))
plt.suptitle("K-means clustering algorithm", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
ax = plt.subplot(1, 2, 1)
plt.scatter(data[:, 0], data[:, 1], c='c')
plt.title("data")
	
ax = plt.subplot(1, 2, 2)
plt.scatter(A[:, 0], A[:, 1], c='b')
plt.scatter(B[:, 0], B[:, 1], c='g')
plt.scatter(C[:, 0], C[:, 1], c='r')
plt.scatter(D[:, 0], D[:, 1], c='y')
plt.scatter(center[:, 0], center[:, 1], s=100, c='m', marker='s')
plt.title("clustered data and centroids (K = 4)")
plt.show()

サンプルプログラム(10)

k_means_color_quantization.py
import numpy as np
import cv2
from matplotlib import pyplot as plt
	
def show_img_with_matplotlib(color_img, title, pos):
    img_RGB = color_img[:, :, ::-1]
    ax = plt.subplot(2, 3, pos)
    plt.imshow(img_RGB)
    plt.title(title)
    plt.axis('off')
	
def color_quantization(image, k):
    data = np.float32(image).reshape((-1, 3))
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)
    ret, label, center = cv2.kmeans(data, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
    center = np.uint8(center)
    result = center[label.flatten()]
    result = result.reshape(img.shape)
    return result
	
fig = plt.figure(figsize=(16, 8))
plt.suptitle("Color quantization using K-means clustering algorithm", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
img = cv2.imread('landscape_1.jpg')
color_3 = color_quantization(img, 3)
color_5 = color_quantization(img, 5)
color_10 = color_quantization(img, 10)
color_20 = color_quantization(img, 20)
color_40 = color_quantization(img, 40)
	
show_img_with_matplotlib(img, "original image", 1)
show_img_with_matplotlib(color_3, "color quantization (k = 3)", 2)
show_img_with_matplotlib(color_5, "color quantization (k = 5)", 3)
show_img_with_matplotlib(color_10, "color quantization (k = 10)", 4)
show_img_with_matplotlib(color_20, "color quantization (k = 20)", 5)
show_img_with_matplotlib(color_40, "color quantization (k = 40)", 6)
plt.show()

サンプルプログラム(11)

k_means_color_quantization_distribution.py
import numpy as np
import cv2
from matplotlib import pyplot as plt
import collections
	
def show_img_with_matplotlib(color_img, title, pos):
    img_RGB = color_img[:, :, ::-1]
    ax = plt.subplot(2, 3, pos)
    plt.imshow(img_RGB)
    plt.title(title)
    plt.axis('off')
	
def color_quantization(image, k):
    data = np.float32(image).reshape((-1, 3))
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)
    ret, label, center = cv2.kmeans(data, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
    center = np.uint8(center)
    result = center[label.flatten()]
    result = result.reshape(img.shape)
    counter = collections.Counter(label.flatten())
    print(counter)
	
    total = img.shape[0] * img.shape[1]
    desired_width = img.shape[1]
    desired_height = 70
    desired_height_colors = 50
    color_distribution = np.ones((desired_height, desired_width, 3), dtype="uint8") * 255
    start = 0
	
    for key, value in counter.items():
        value_normalized = value / total * desired_width
        end = start + value_normalized
        cv2.rectangle(color_distribution, (int(start), 0), (int(end), desired_height_colors), center[key].tolist(), -1)
        start = end
	
    return np.vstack((color_distribution, result))
	
fig = plt.figure(figsize=(16, 8))
plt.suptitle("Color quantization using K-means clustering algorithm", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
img = cv2.imread('landscape_2.jpg')
color_3 = color_quantization(img, 3)
color_5 = color_quantization(img, 5)
color_10 = color_quantization(img, 10)
color_20 = color_quantization(img, 20)
color_40 = color_quantization(img, 40)
	
show_img_with_matplotlib(img, "original image", 1)
show_img_with_matplotlib(color_3, "color quantization (k = 3)", 2)
show_img_with_matplotlib(color_5, "color quantization (k = 5)", 3)
show_img_with_matplotlib(color_10, "color quantization (k = 10)", 4)
show_img_with_matplotlib(color_20, "color quantization (k = 20)", 5)
show_img_with_matplotlib(color_40, "color quantization (k = 40)", 6)
plt.show()