[Image processing] Edge detection using Python and OpenCV makes Poo naked!

Introduction

Nice to meet you! My name is Yoshiki and I specialize in machine learning and deep learning at university! This time, I would like to explain about edge detection using Python and OpenCV. (It's also to deepen my understanding lol) For the time being, I will do my best to have fun and understand even those who are wondering what Python can do!

What is edge detection in the first place?

In the world of image processing, the edge has the meaning of a part in the image where the brightness changes suddenly, but it's not very clear. What do you usually think of when you hear the word edge? The correct answer is for those who think of edges and contours! In other words, edge detection is a technology that extracts only the contour feature to make image processing easier!

References https://it-mint.com/2018/11/05/feature-value-in-image-recognition-whats-edge-detection-and-spatial-filter-1839.html

environment

This time, I wanted everyone to actually move their hands to run the program, so I would like to implement it using Google Drive and Google Colaboratory instead of locally. As a merit, I chose it because it is convenient because it can be used without installing the library with pip etc.

Creating an environment

    1. First, let's create a google account! (If you already have it, you can use it.) If you can, access google drive and log in.
  1. Once you have access, select the item called Folder from the new button on the upper left. If you click it, a name input field will appear. Anything is fine, but I named it Edge!

    1. Then, I think that a folder is created in My Drive. Then go there and create the Images and Src folders again from the new button. In Images, you can upload the image you want to detect the edge, or upload the image after edge detection. I will write the source code in Src.
  2. Well, it's almost the end! In Images, put the images you want to detect edges! You can do it by uploading the file with the new button. In Src, select Google Colaboratory from New at the bottom. (If not, search for the app from Add and install it!) Once selected, you should be taken to the editor screen.

  3. Finally, about the specifications of Google Colaboratory. ・ Please note that Google Colaboratory will be disconnected in about 30 minutes, and if it is disconnected, you will have to reconnect. (The source code never disappears lol) ・ When connecting, you will be asked for the code as shown below, so access the URL to get the code! messageImage_1605587862536.jpg

・ For the sake of clarity, change the name from Untitled.ipynb. -You can save from the file column. Let's keep it diligent!

Source code

I was conscious of the object-oriented design as a whole. I left the basic code description in the comment out.

import cv2

#------------Setting------------#
#Setting for using google drive
from google import colab
colab.drive.mount('/content/gdrive')

#Directory setting
b_dir='gdrive/My Drive/Edge/' #Setting working directory

#Experiment setting (Parameter setting for canny operator)
min_val=100
max_val=150

#Imput file setting
t_dir=b_dir+'Images/'
data='Pooh'
ext='.JPG'
org_name=t_dir+data+ext

#Output file setting
canny_name=t_dir+data+'_Canny _'+str(min_val)+'_'+str(max_val)+ext

#------------Image processing------------#

#Image read
org=cv2.imread(org_name)
if org is None:
  print('\n**********************************************************\n')
  print(org_name+' cannot be read\n')
  print('************************************************************\n')
else:
  #Grayscale image generation
  gray=cv2.cvtColor(org,cv2.COLOR_BGR2GRAY)

  #Apply image operator
  canny=cv2.Canny(gray,min_val,max_val)

  #Save image
  cv2.imwrite(canny_name,canny)

Explanation (Notes)

First, below, we are importing OpenCV and setting the directory.

There is one caveat here.

The last line is Edge. This refers to the name of the folder you set first, so let's rewrite it to the name of the folder you created first.

import cv2

#------------Setting------------#
#Setting for using google drive
from google import colab
colab.drive.mount('/content/gdrive')

#Directory setting
b_dir='gdrive/My Drive/Edge/' #Setting working directory

Next, in the following, parameter settings, image file settings, and output image file settings are made.

There are two caveats here!

The first point is about parameters. This time, we use the Canny method as the edge detection method. (I won't explain the Canny method in this article.) This parameter is a value that I set to be able to take an edge well with this value, so you are free to change it. .. The second point is about the setting of the image file. I think that you uploaded the image to the Images folder, so please store the image before the extension in data and the extension in ext.

#Experiment setting (Parameter setting for canny operator)
min_val=100
max_val=150

#Imput file setting
t_dir=b_dir+'Images/'
data='Pooh'
ext='.JPG'
org_name=t_dir+data+ext

#Output file setting
canny_name=t_dir+data+'_Canny _'+str(min_val)+'_'+str(max_val)+ext

Output result

Mounted at /content/gdrive If it is output like this, it is successful! Check out Images in My Drive. The image with edge detection should be output. Then, as the title says, I detected the edge of Poo in the profile image, so please see the result.

Original image Poo

ぷーさん.JPG

Edge detection Poo

ぷーさん_Canny _100_150.JPG

I'm sorry ...

Finally

thank you for your hard work! I'm glad if anyone has been dating so far lol Also, I hope this article will give you an interest in how Python can do this. Since this is my first post, I intend to do it as carefully as possible, but if you have any questions, questions, or mistakes, please comment. I will continue to write many articles such as machine learning, so please follow me if you like!

Recommended Posts

[Image processing] Edge detection using Python and OpenCV makes Poo naked!
[Python] Using OpenCV with Python (Edge Detection)
I tried object detection using Python and OpenCV
[Python] Accessing and cropping image pixels using OpenCV (for beginners)
[Python] Using OpenCV with Python (Image transformation)
Draw a watercolor illusion with edge detection in Python3 and openCV3
Image processing with Python & OpenCV [Tone Curve]
Video processing using Python + OpenCV on Mac
Light image processing with Python x OpenCV
python image processing
Head orientation estimation using Python and OpenCV + dlib
Notes on HDR and RAW image processing with Python
First Python image processing
[Ubuntu] [Python] Face detection comparison between dlib and OpenCV
Image processing with Python
Rotate and scale the image before cropping [python] [OpenCV]
Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data
[Let's play with Python] Image processing to monochrome and dots
Similar face image detection using face recognition and PCA and K-means clustering
Shoot time-lapse from a PC camera using Python and OpenCV
Image processing with Python (Part 2)
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
Feature detection using opencv (corner detection)
[Python] Using OpenCV with Python (Basic)
100 image processing knocks !! (001 --010) Carefully and carefully
Image processing with Python (Part 1)
Real-time edge detection with OpenCV
Image processing with Python (Part 3)
Face detection with Python + OpenCV
Image processing by python (Pillow)
Image Processing Collection in Python
Image expansion and contraction processing
Using Python mode in Processing
Using OpenCV with Python @Mac
[Python] Image processing with scikit-image
Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
Create a simple scheduled batch using Docker's Python Image and parse-crontab
I just erased the object using image repair (inpaint) (OpenCV: Python)
Build and try an OpenCV & Python environment in minutes using Docker
Excel file column addition and row deletion processing using Python Openpyxl
Aligning scanned images of animated video paper using OpenCV and Python
One-dimensional and two-dimensional vertex detection processing
Environment construction of python and opencv
Shining life with Python and OpenCV
Python parallel processing (multiprocessing and Joblib)
Neural network with OpenCV 3 and Python 3
Judgment of backlit image using OpenCV
Personal notes for python image processing
Image processing with Python 100 knocks # 3 Binarization
Environmentally friendly scraping using image processing
Clustering and visualization using Python and CytoScape
Horizon processing using OpenCV morphology transformation
python string processing map and lambda
Find image similarity with Python + OpenCV
Image processing with Python 100 knocks # 2 Grayscale
Introduction to image analysis opencv python
Reading and creating a mark sheet using Python OpenCV (Tips for reading well)
Get and estimate the shape of the head using Dlib and OpenCV with python