[PYTHON] Try edge detection with OpenCV

Introduction

OpenCV (Open Source Computer Vision Library) is a collection of BSD-licensed video / image processing libraries. There are many algorithms for image filtering, template matching, object recognition, video analysis, machine learning, and more.

Example of motion tracking using OpenCV (OpenCV Google Summer of Code 2015) https://www.youtube.com/watch?v=OUbUFn71S4s

Click here for installation and easy usage http://qiita.com/olympic2020/items/d5d475a446ec9c73261e

In order to track moving objects, it is necessary to first filter the image. This time, I will try edge detection using OpenCV.

program

The flow is as follows.

  1. Load a color image
  2. Convert to grayscale
  3. Edge detection

sample.py


import cv2

#Constant definition
ORG_WINDOW_NAME = "org"
GRAY_WINDOW_NAME = "gray"
CANNY_WINDOW_NAME = "canny"

ORG_FILE_NAME = "org.jpg "
GRAY_FILE_NAME = "gray.png "
CANNY_FILE_NAME = "canny.png "

#Load the original image
org_img = cv2.imread(ORG_FILE_NAME, cv2.IMREAD_UNCHANGED)
#Convert to grayscale
gray_img = cv2.imread(ORG_FILE_NAME, cv2.IMREAD_GRAYSCALE)
#Edge extraction
canny_img = cv2.Canny(gray_img, 50, 110)

#Show in window
cv2.namedWindow(ORG_WINDOW_NAME)
cv2.namedWindow(GRAY_WINDOW_NAME)
cv2.namedWindow(CANNY_WINDOW_NAME)

cv2.imshow(ORG_WINDOW_NAME, org_img)
cv2.imshow(GRAY_WINDOW_NAME, gray_img)
cv2.imshow(CANNY_WINDOW_NAME, canny_img)

#Save to file
cv2.imwrite(GRAY_FILE_NAME, gray_img)
cv2.imwrite(CANNY_FILE_NAME, canny_img)

#End processing
cv2.waitKey(0)
cv2.destroyAllWindows()

Execution result

The forest in the foreground, the building in the middle, and the sky in the back were edge-extracted as such.

org.jpg The original image

gray.png grayscale

canny.png Edge detection

to be continued

Next, let's deal with videos. Try converting videos in real time with OpenCV

Recommended Posts

Try edge detection with OpenCV
Real-time edge detection with OpenCV
[Python] Using OpenCV with Python (Edge Detection)
Face detection with Python + OpenCV
Try OpenCV with Google Colaboratory
Anime face detection with OpenCV
Try face recognition with python + OpenCV
OpenCV feature detection with Google Colaboratory
Cat detection with OpenCV (model distribution)
Try blurring the image with opencv2
Face detection with Python + OpenCV (rotation invariant)
Try using the camera with Python's OpenCV
Draw a watercolor illusion with edge detection in Python3 and openCV3
Try Object detection with Raspberry Pi 4 + Coral
Edge extraction with python + OpenCV (Sobel filter, Laplacian filter)
Resize, mosaic, face detection with OpenCV, sometimes Zojirushi
Try converting videos in real time with OpenCV
Try to detect fish with python + OpenCV2.4 (unfinished)
Try scraping with Python.
Detect stoop with OpenCV
Binarization with OpenCV / Python
Try SNN with BindsNET
Rotate sprites with OpenCV
Data Augmentation with openCV
Try regression with TensorFlow
Easy TopView with OpenCV
Stumble with homebrew opencv3
Try projective transformation of images using OpenCV with Python
Try to factorial with recursion
Improve detection accuracy quickly by specifying parameters with openCV face detection
Try converting webcam / camcorder videos in real time with OpenCV
Try deep learning with TensorFlow
Try using PythonTex with Texpad.
Face recognition with Python's OpenCV
"Apple processing" with OpenCV3 + Python3
Try implementing RBM with chainer.
Try Google Mock with C
Image editing with python OpenCV
Try using matplotlib with PyCharm
Try programming with a shell!
Camera capture with Python + OpenCV
Try GUI programming with Hy
Try an autoencoder with Pytorch
Try Python output with Haxe 3.2
Edge detection (Laplacian, Sobel, Canny)
Feature detection using opencv (corner detection)
Try matrix operation with NumPy
[Python] Using OpenCV with Python (Basic)
Try using OpenCV on Windows
Try implementing XOR with PyTorch
Try running CNN with ChainerRL
Try various things with PhantomJS
Face detection with Python + dlib
Try Deep Learning with FPGA
Binarize photo data with OpenCV
Loop video loading with opencv
Try running Python with Try Jupyter
Try implementing perfume with Go
Get image features with OpenCV
Face recognition / cutting with OpenCV
Blockchain tampering detection with Python