[PYTHON] [Machine learning] I tried to do something like passing an image

Hello. This is Yuki Ando (@ holiday1173).

In this article, using a type of clustering method called the K-means method introduced in the previous article, [pathization (vectorization)](https: // school) of images, which is often used in Illustrator, etc. .dhw.co.jp/word/design/vector.html) I tried something like that.

Overview

Here's what I did in the last article:

  1. Clustering the pixel value of each pixel of the image using K-means

  2. Assign the average pixel value in the cluster for each cluster, reduce the number of colors and draw

Previous article → [Machine learning] Explaining color quantization

Here is the result of the actual processing. (Left: Before processing, Right: After processing) Parrots_with_k9.png

The image on the right shows the result of K-means processing with 9 clusters. Since the number of clusters = the number of colors, the processed image is drawn in only 9 colors. The image looks like a slightly flat coloring book.

In the previous article, we used only pixel values as input for K-means, so if the colors were similar, pixels in a slightly distant area of the image would be divided into the same cluster.

So, I thought that if I clustered the pixel coordinate information in addition to the pixel value, I could get a path-like vector-like image, so I tried it.

result

Here is the code I used.

import os
import argparse
import numpy as np
import cv2


if __name__ == "__main__":
    #Specify the path as an argument
    parser = argparse.ArgumentParser()
    parser.add_argument('--img', '-i', help='The path of the image you want to process')
    parser.add_argument('--output', '-o', help='Directory to output the result image')
    args = parser.parse_args()

    #Loading images
    img = cv2.imread(args.img)

    #Image as it is k-Since it cannot be applied to means, shape(Number of pixels, 3(BGR))Conversion to
    Z1 = img.reshape((-1, 3))
    
    """
Get xy coordinates for each pixel
    """
    #Vertical and horizontal width of the image
    h, w = img.shape[:-1]
    #Two-dimensional list of xy coordinates of images
    Z2 = [[i, j] for i in range(h) for j in range(w)]
    
    # RGB +Concatenate xy coordinates
    Z = np.hstack((Z1, Z2))
    # np.Convert to float32 type
    Z = np.float32(Z)

    # k-means termination condition
    #Use default value
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    #Number of groups after split
    for K in range(5, 21, 3):
        # K = 10
        # k-means processing
        _, label, center = cv2.kmeans(
            Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

        # np.Convert to uint8 type
        center = np.uint8(center)
        #Assign intra-group mean for each group
        res = center[label.flatten()]
        res = res[:, :-2]
        #Restore the original image size
        res2 = res.reshape((img.shape))

        output_path = os.path.join(args.output, 'Parrots_K{:02}.png'.format(K))
        #Save image
        cv2.imwrite(output_path, res2)

Here are the main processes added to the previous code.

"""
Get xy coordinates for each pixel
"""
#Vertical and horizontal width of the image
h, w = img.shape[:-1]
#Two-dimensional list of xy coordinates of images
Z2 = [[i, j] for i in range(h) for j in range(w)]

This part of the process gets the xy coordinates for each pixel. By combining the xy coordinates obtained here with the list of pixel values, a list that concatenates RGB + xy coordinates is generated. First, get the vertical width (h) and horizontal width (w) of img. The xy coordinate list is created with the line Z2 = ~ as [[0, 0], [0, 1], [0, 2] ...].

# RGB +Concatenate xy coordinates
Z = np.hstack((Z1, Z2))

This line combines the list of pixel values and xy coordinates, and the data to be input to K-means is completed.

Below are the results. The original image Parrots.png

K = 5 Parrots_K05.png

K = 8 Parrots_K08.png

K = 11 Parrots_K11.png

K = 14 Parrots_K14.png

K = 17 Parrots_K17.png

K = 20 Parrots_K20.png

Not only the pixel value but also the position is taken into consideration when dividing into clusters, so it looks like a path!

reference

- Image </ b>
The image data used this time can be downloaded from the link below.
http://www.ess.ic.kanagawa-it.ac.jp/app_images_j.html

Recommended Posts

[Machine learning] I tried to do something like passing an image
I tried to compress the image using machine learning
I wanted to do something like an Elixir pipe in Python
I tried image processing like an event camera
I tried to get an image by scraping
I tried to move machine learning (ObjectDetection) with TouchDesigner
An introduction to machine learning
I tried to extract a line art from an image with Deep Learning
I tried to process and transform the image and expand the data for machine learning
I tried to build an environment for machine learning with Python (Mac OS X)
I want to do something like sort uniq in Python
I tried machine learning to convert sentences into XX style
I tried machine learning with liblinear
I wanted to do it like running an AtCoder test case.
I tried to make an image similarity function with Python + OpenCV
I tried to understand supervised learning of machine learning in an easy-to-understand manner even for server engineers 1
An introduction to OpenCV for machine learning
I tried HR Tech to develop an expert search engine by machine learning in-house meeting information
An introduction to Python for machine learning
I tried to understand supervised learning of machine learning in an easy-to-understand manner even for server engineers 2
#I tried something like Vlookup with Python # 2
I tried to classify guitar chords in real time using machine learning
(Machine learning) I tried to understand Bayesian linear regression carefully with implementation.
I tried to visualize the model with the low-code machine learning library "PyCaret"
I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas
I tried to correct the keystone of the image
I tried to detect an object with M2Det!
Mayungo's Python Learning Episode 5: I tried to do four arithmetic operations with numbers
I made an image classification model and tried to move it on mobile
I tried to predict the presence or absence of snow by machine learning.
I tried to predict the change in snowfall for 2 years by machine learning
I tried to implement various methods for machine learning (prediction model) using scikit-learn.
I want to do machine learning even without a server --Time Series Edition -
[Keras] I tried to solve a donut-type region classification problem by machine learning [Study]
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried to make a real-time sound source separation mock with Python machine learning
I tried to implement an artificial perceptron with python
I tried deep learning
I tried to debug.
I tried to get an AMI using AWS Lambda
I tried to become an Ann Man using OpenCV
I tried to paste
I tried to make an OCR application with PySimpleGUI
Alcoholic rattling! Reincarnate an important key to another world! Did I do something again?
I tried to find an alternating series with tensorflow
I want to do something in Python when I finish
Introduction to machine learning
An introduction to machine learning from a simple perceptron
Everything for beginners to be able to do machine learning
Matching app I tried to take statistics of strong people & tried to create a machine learning model
[Deep Learning from scratch] I tried to explain the gradient confirmation in an easy-to-understand manner.
I tried to verify the yin and yang classification of Hololive members by machine learning
I tried to summarize useful knowledge when developing / operating machine learning services [Python x Azure]
Uncle SE with hardened brain tried to study machine learning
I tried using Tensorboard, a visualization tool for machine learning
Mayungo's Python Learning Episode 3: I tried to print numbers with print
I tried to implement ListNet of rank learning with Chainer
I tried to implement Perceptron Part 1 [Deep Learning from scratch]
I tried to divide with a deep learning language model
I tried to create an article in Wiki.js with SQLAlchemy
I tried AutoGluon's Image Classification