Image processing with Python (Part 3)

Pretend before

The continuation of the article I wrote earlier for the first time in a year. -Image processing with Python (1) -Image processing with Python (2)

The place to binarize using OpenCV is that I wrote the OpenCV document as it is, Image processing with Python (1). Instead of "binarizing", try something like "find the threshold value for binarization and cut off the pixels below that threshold value".

Image binarization

Decrease the image of lenna by 2.

import cv2

lenna = "4.2.04.tiff"

orig = cv2.imread(lenna, 0)
img = cv2.medianBlur(orig, 5)
ret,th = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

In the image where th is binarized, ret is the threshold value.

Pixels below the threshold

If you think simply, it seems that you can do it with the following code.

height, width = img.shape
for y in range(height):
  for x in range(width):
    if img[y, x] < ret:
      img[y, x] = 0

However, there is a problem with this code. Applying for to a numpy array is really slow.

So is there a way to make it faster? Of course there is.

low_idx = img < ret
img[low_idx] = 0

Now, when you compare the images, it looks like this.

from matplotlib import pyplot as plt

plt.subplot(1,3,1)
plt.imshow(orig, cmap='gray'), plt.title('Original')
plt.subplot(1,3,2)
plt.imshow(img, cmap='gray'), plt.title('Threshold')
plt.subplot(1,3,3)
plt.imshow(th, cmap='gray'), plt.title('Binarize')
plt.show()

lenna_20170917.png


Today's summary

I found the threshold for binarization and cut off at that threshold.

Today's code

Recommended Posts

Image processing with Python (Part 3)
Image processing with Python
[Python] Image processing with scikit-image
python image processing
Image processing with Python 100 knocks # 3 Binarization
Image processing with Python 100 knocks # 2 Grayscale
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Image processing with MyHDL
Image processing with Python 100 knocks # 8 Max pooling
First Python image processing
100 Language Processing Knock with Python (Chapter 2, Part 2)
Image processing with Python 100 knock # 12 motion filter
100 Language Processing Knock with Python (Chapter 2, Part 1)
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Image processing with Python 100 knocks # 7 Average pooling
Light image processing with Python x OpenCV
Image processing with Python 100 knocks # 9 Gaussian filter
Image Processing with PIL
Image processing from scratch with python (5) Fourier transform
Image processing from scratch with python (4) Contour extraction
100 Language Processing with Python Knock 2015
Studying Python with freeCodeCamp part1
Bordering images with python Part 1
Image processing with PIL (Pillow)
"Apple processing" with OpenCV3 + Python3
Scraping with Selenium + Python Part 1
Image editing with python OpenCV
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Sorting image files with Python (3)
Studying Python with freeCodeCamp part2
Sorting image files with Python
Solving Sudoku with Python (Part 2)
Image processing by python (Pillow)
Image Processing Collection in Python
Scraping with Selenium + Python Part 2
Notes on HDR and RAW image processing with Python
Cut out an image with python
Real-time image processing basics with opencv
Playing handwritten numbers with python Part 1
[Python] Using OpenCV with Python (Image Filtering)
[Python] Easy parallel processing with Joblib
[Automation with python! ] Part 1: Setting file
100 Language Processing Knock with Python (Chapter 1)
[Python] Using OpenCV with Python (Image transformation)
100 Language Processing Knock with Python (Chapter 3)
Personal notes for python image processing
Let's do image scraping with Python
Find image similarity with Python + OpenCV
Automate simple tasks with Python Part0
[Automation with python! ] Part 2: File operation
Send image with python, save with php
Excel aggregation with Python pandas Part 1
Gradation image generation with Python [1] | np.linspace
[Let's play with Python] Image processing to monochrome and dots
[Image processing] Posterization
QGIS + Python Part 2
FizzBuzz with Python3
Scraping with Python