[PYTHON] Image expansion and contraction processing

Execution environment

Google Colaboratory

Preparing to load images with Google Colaboratory

python


from google.colab import files
from google.colab import drive
drive.mount('/content/drive')

Loading the required libraries

python


import cv2 #opencv
import numpy as np
import matplotlib.pyplot as plt 
%matplotlib inline

Image preparation

python


img = plt.imread("/content/drive/My Drive/Colab Notebooks/img/Lenna.bmp")
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

code

python


#Original image
plt.subplot(2,3,1)
plt.title("Original", fontsize=10)
plt.imshow(gray)

kernel = np.ones((3,3),np.uint8)

#Select the one with high brightness from the expanded kernel size area
plt.subplot(2,3,4)
plt.title("dilate", fontsize=10)
dst = cv2.dilate(gray,kernel,iterations = 1) #iterations: Number of expansions
plt.imshow(dst)

#Select the one with low brightness from the contraction kernel size area
plt.subplot(2,3,5)
plt.title("erode", fontsize=10)
dst = cv2.erode(gray,kernel,iterations = 1) #iterations: Number of shrinkages
plt.imshow(dst)

plt.show()

result

image.png

If used properly, it can be used for complementing missing values and removing noise.

Recommended Posts

Image expansion and contraction processing
100 image processing knocks !! (001 --010) Carefully and carefully
[Image processing] Posterization
python image processing
Image processing 100 knocks ①
Image processing with MyHDL
Flat Field image processing
First Python image processing
Read digital image processing
Image processing with Python
Image Processing with PIL
Notes on HDR and RAW image processing with Python
opencv-python Introduction to image processing
Image processing with PIL (Pillow)
Digital image processing (spatial filtering)
[Let's play with Python] Image processing to monochrome and dots
Image Processing Collection in Python
[Python] Image processing with scikit-image
100 image processing knocks !! (001 --010) Carefully and carefully
Image expansion and contraction processing
[Image processing] Edge detection using Python and OpenCV makes Poo naked!
[Image processing] Edge detection using Python and OpenCV makes Poo naked!
Real-time image processing basics with opencv
Python parallel processing (multiprocessing and Joblib)
Grayscale image and save as csv
Image segmentation with scikit-image and scikit-learn
Personal notes for python image processing
Image processing with Python 100 knocks # 3 Binarization
Image processing 100 knocks Q9, Q10 (filter) speedup
Artificial language Lojban and natural language processing (artificial language processing)
Path processing with takewhile and dropwhile
Environmentally friendly scraping using image processing
python string processing map and lambda
Image recognition environment construction and basics
Image processing | predicting species from images
Judge the extension and download the image
About the main tasks of image processing (computer vision) and the architecture used