Image editing with python OpenCV

Edit to a readable image with OpenCV

python


import cv2
import numpy as np

from IPython.display import display, Image

def display_cv_image(image, format='.png'):
    decoded_bytes = cv2.imencode(format, image)[1].tobytes()
    display(Image(data=decoded_bytes))
    img = cv2.imread("{Image file path}")
    display_cv_image(img)
    #Grayscale
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    #Binarization
    ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
    display_cv_image(th1)
    contours, hierarchy = cv2.findContours(th1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    #Sort only those with a large area
    areas = []
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > 10000:
            epsilon = 0.1*cv2.arcLength(cnt,True)
            approx = cv2.approxPolyDP(cnt,epsilon,True)
            areas.append(approx)

    cv2.drawContours(img,areas,-1,(0,255,0),3)
    display_cv_image(img)
    img = cv2.imread("{Image file path}")

    dst = []

    pts1 = np.float32(areas[0])
    pts2 = np.float32([[600,300],[600,0],[0,0],[0,300]])

    M = cv2.getPerspectiveTransform(pts1,pts2)
    dst = cv2.warpPerspective(img,M,(600,300))

    display_cv_image(dst)


Recommended Posts

Image editing with python OpenCV
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
Find image similarity with Python + OpenCV
Binarization with OpenCV / Python
Image processing with Python & OpenCV [Tone Curve]
Image acquisition from camera with Python + OpenCV
Image processing with Python
Light image processing with Python x OpenCV
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
How to crop an image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
[Small story] Test image generation with Python / OpenCV
Image processing with Python (Part 2)
Camera capture with Python + OpenCV
[Python] Using OpenCV with Python (Basic)
Sorting image files with Python (2)
Sorting image files with Python (3)
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Image processing with Python (Part 3)
Face detection with Python + OpenCV
Get image features with OpenCV
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
[Python] Image processing with scikit-image
[OpenCV / Python] I tried image analysis of cells with OpenCV
JPEG image generation by specifying quality with Python + OpenCV
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
Shining life with Python and OpenCV
Cut out an image with python
Real-time image processing basics with opencv
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Edge Detection)
Image processing with Python 100 knocks # 3 Binarization
Easy Python + OpenCV programming with Canopy
Let's do image scraping with Python
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Load gif images with Python + OpenCV
Try blurring the image with opencv2
Use OpenCV with Python 3 in Window
Image processing with Python 100 knocks # 2 Grayscale
Draw an illustration with Python + OpenCV
Introduction to image analysis opencv python
Track baseball balls with Python + OpenCV
Graph Based Segmentation with Python + OpenCV
Send image with python, save with php
Draw arrows (vectors) with opencv / python
Basic study of OpenCV with Python
Gradation image generation with Python [1] | np.linspace
I tried "gamma correction" of the image with Python + OpenCV
Paste png with alpha channel as transparent image with Python / OpenCV
[Python] Easy reading of serial number image files with OpenCV
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Face detection with Python + OpenCV (rotation invariant)
FizzBuzz with Python3