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!
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!
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.
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!
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.
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!
・ For the sake of clarity, change the name from Untitled.ipynb. -You can save from the file column. Let's keep it diligent!
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)
First, below, we are importing OpenCV and setting the directory.
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.
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
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.
I'm sorry ...
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