Image processing by Python 100 knock # 1 channel replacement

Introduction

Hi, I'm Ramu. Suddenly, I belong to an image processing laboratory. In my laboratory, I have to teach my juniors about image processing methods in seminars from next month.

To be honest, I feel that I can't implement or explain basic image processing due to lack of study recently. So I decided to try imori_imori's 100 knocks on image processing.

Here, I will implement image processing one by one and explain it lightly. Please note that python has just started recently, so please understand the coding ability.

1st: Channel replacement

The first image processing to commemorate is channel switching. A color image has three color components, red, blue, and green, that is, three channel components. When reading using openCV, the order is blue, green, and red, but this time, this is replaced in the order of red, green, and blue.

reverseChannel.py


import cv2
import matplotlib.pyplot as plt


def reverseChannel(img):
  #Image copy
  dst = img.copy()
  #Channel replacement
  dst[:, :, 0] = img[:, :, 2]
  dst[:, :, 2] = img[:, :, 0]

  return dst


#Image reading
img = cv2.imread('image.jpg')

#Channel replacement
img = reverseChannel(img)

#Save image
cv2.imwrite("result.jpg ", img)
#Image display
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()

ファイル名 ファイル名

The image on the left is the input image, and the image on the right is the output image. The red color of the sashimi has changed to blue due to the replacement of the red and blue components.

in conclusion

I would like to output like this. If you have any questions, please feel free to contact us. By the way, the official answer is posted on Github, the official website for 100 image processing knocks, so please check that as well.

Recommended Posts

Image processing by Python 100 knock # 1 channel replacement
100 image processing by Python Knock # 6 Color reduction processing
Image processing by python (Pillow)
Image processing by Python 100 knock # 11 smoothing filter (average filter)
100 Language Processing Knock Chapter 1 by Python
python image processing
Image processing with Python 100 knock # 10 median filter
Grayscale by matrix-Reinventor of Python image processing-
Image processing with Python 100 knock # 12 motion filter
Communication processing by Python
First Python image processing
Image processing with Python
Image processing with Python (Part 2)
100 Language Processing with Python Knock 2015
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
100 Language Processing Knock Chapter 1 (Python)
100 Language Processing Knock Chapter 2 (Python)
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Image Processing Collection in Python
[Python] Image processing with scikit-image
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock Chapter 1 in Python
100 Language Processing Knock with Python (Chapter 3)
Personal notes for python image processing
Image processing with Python 100 knocks # 3 Binarization
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
Image processing with Python 100 knocks # 2 Grayscale
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
Straight line drawing by matrix-Inventor's original research of Python image processing-
Basics of binarized image processing with Python
Socket communication and multi-thread processing by Python
100 Language Processing Knock-89: Analogy by Additive Constitutiveness
Image processing with Python 100 knocks # 8 Max pooling
"Data Science 100 Knock (Structured Data Processing)" Python-007 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-006 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-001 Explanation
100 Language Processing Knock with Python (Chapter 2, Part 2)
Image processing with Python & OpenCV [Tone Curve]
100 Language Processing Knock with Python (Chapter 2, Part 1)
"Data Science 100 Knock (Structured Data Processing)" Python-002 Explanation
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Analysis of X-ray microtomography image by Python
[Python] Data Science 100 Knock (Structured Data Processing) 021 Explanation
Image processing with Python 100 knocks # 7 Average pooling
Image processing 100 knock Q.6. Color reduction processing explanation
"Data Science 100 Knock (Structured Data Processing)" Python-005 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-004 Explanation
[Python] Data Science 100 Knock (Structured Data Processing) 020 Explanation
Light image processing with Python x OpenCV
[Python] Data Science 100 Knock (Structured Data Processing) 025 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-003 Explanation
Matrix Convolution Filtering-Reinventor of Python Image Processing-
Image processing with Python 100 knocks # 9 Gaussian filter
[Python] Data Science 100 Knock (Structured Data Processing) 019 Explanation
[Image processing] Posterization
Python inexperienced person tries to knock 100 language processing 14-16
Image processing from scratch with python (5) Fourier transform
100 Language Processing Knock (2020): 28
Entry where Python beginners do their best to knock 100 language processing little by little