I tried simple image processing with Google Colaboratory.

Introduction

This article is a thin article written by a rare technical college student (transfer student) who has never even touched a PC, let alone python. There are quite a lot of points that I refer to the articles of other people. I hope you can see it with warm eyes.

Required environment

Upload and view images

This time, I will upload and process any image so that it is easy to use. You can upload it by using files.upload (). When you execute the command, the upload form will be displayed, so please upload it. After that, get the file name.

Upload


from google.colab import files
uploaded_file = files.upload()
img = next(iter(uploaded_file))

Image display

Original display

The image is read using OpenCV, but since the color of the image in OpenCV is handled by BGR instead of RGB, it is converted to general RGB. Then use matplotlib to display the image.

Image display


import cv2
from matplotlib import pyplot as plt

img2 = cv2.imread(img)
#RGB conversion
src = cv2.cvtColor(img2,cv2.COLOR_BGR2RGB)
#Image display
plt.imshow(src)
#Hide value
plt.axis('off')

This will display the original image. スクリーンショット (3).png

Color inversion

Inverting colors is easy, just put src = 255 --src. This takes advantage of the fact that the value is inverted by subtracting the original color value from white (255).

Color inversion


import cv2
from matplotlib import pyplot as plt

img2 = cv2.imread(img)
src = cv2.cvtColor(img2,cv2.COLOR_BGR2RGB)
#Inversion
src = 255 - src
plt.imshow(src)
plt.axis('off')

Execution result スクリーンショット (14).png

Convert and display to grayscale

Conversion to grayscale can be done with cv2.cvtColor (pixels, cv2.COLOR_BGR2GR). That's the only difference from the original.

grayscale


import numpy as np
pixels = np.array(src)
import matplotlib.pyplot as plt
#Convert to grayscale
img_gray = cv2.cvtColor(pixels, cv2.COLOR_BGR2GRAY)

plt.imshow(img_gray)
plt.axis('off')

Execution result スクリーンショット (7).png

Binarization

Finally, it is binarization. Binarization is done by first converting to grayscale and then binarizing with cv2.threshold (img_gray, 128, 255, cv2.THRESH_BINARY. The 128 part is called ** threshold **, and here Values ​​brighter than the value of are white (255) and darker values ​​are black (0). This is important for binarizing the threshold value.

Binarization


import numpy as np
pixels = np.array(src)

import matplotlib.pyplot as plt]
#Convert to grayscale
img_gray = cv2.cvtColor(pixels, cv2.COLOR_BGR2GRAY)
#Binarization
retval, img_binary = cv2.threshold(img_gray,128, 255, cv2.THRESH_BINARY)

plt.imshow(img_binary)

Execution result

Threshold = 128 スクリーンショット (9).png

Threshold = 8 スクリーンショット (12).png

Reference material

Recommended Posts

I tried simple image processing with Google Colaboratory.
I tried simple image recognition with Jupyter
I tried playing with the image with Pillow
I tried natural language processing with transformers.
I tried "smoothing" the image with Python + OpenCV
Image processing with MyHDL
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried image processing like an event camera
I tried Python-like loop processing with BigQuery Scripting
I tried "binarizing" the image with Python + OpenCV
Image processing with Python
I tried "Receipt OCR" with Google Vision API
Image Processing with PIL
I tried to make a simple image recognition API with Fast API and Tensorflow
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried a simple RPA for login with selenium
Easy learning of 100 language processing knock 2020 with "Google Colaboratory"
Image processing with Python (Part 2)
I tried fp-growth with python
I tried scraping with Python
I tried AutoGluon's Image Classification
Image processing with PIL (Pillow)
I tried Learning-to-Rank with Elasticsearch!
Study Python with Google Colaboratory
I tried clustering with PyCaret
Image processing with Python (Part 1)
Image processing with Python (Part 3)
I tried 100 language processing knock 2020
I tried gRPC with Python
I tried scraping with python
Try OpenCV with Google Colaboratory
[Python] Image processing with scikit-image
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
Code snippets often used when processing videos with Google Colaboratory
I tried Google Sign-In with Spring Boot + Spring Security REST API
Real-time image processing basics with opencv
I tried trimming efficiently with OpenCV
I tried summarizing sentences with summpy
seam carving (image cropping) I tried
I tried machine learning with liblinear
I tried web scraping with python.
I tried knocking 100 image processes (Q1 ~ Q10)
I tried moving food with SinGAN
I tried 100 language processing knock 2020: Chapter 3
Image processing with Python 100 knocks # 3 Binarization
I tried implementing DeepPose with PyTorch
I tried face detection with MTCNN
OpenCV feature detection with Google Colaboratory
100 language processing knock 2020 "for Google Colaboratory"
I tried 100 language processing knock 2020: Chapter 1
Image processing with Python 100 knocks # 2 Grayscale
I tried SMTP communication with Python
I tried learning LightGBM with Yellowbrick
I tried face recognition with OpenCV
I tried 100 language processing knock 2020: Chapter 2
I tried 100 language processing knock 2020: Chapter 4
I tried asynchronous processing using asyncio
I tried natural number expression and arithmetic processing only with list processing