程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How does python implement image edge detection

編輯:Python

pythonHow to implement image edge detection

這篇文章主要介紹“pythonHow to implement image edge detection”,在日常操作中,相信很多人在pythonThere are doubts about how to implement image edge detection,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”pythonHow to implement image edge detection”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

任務描述

背景

Edge detection is a common technique in the field of digital image processing,It is widely used in image feature extraction、目標識別、計算機視覺等領域.The edge can be understood as the place where the pixel value transitions,And edge detection is all about finding such places.如下圖所示,Edge detection is performed on the left image,The result is the image on the right.

Edge detection can be achieved based on the convolution operation,對圖像 1(Set the resolution to w×h)The method of performing edge detection is as follows:

1)將圖像 1 轉換成灰度圖,Still called an image 1;
2)新建圖像 2,圖像 2 為灰度圖,Resolution and Image 1 相同;
3)對於圖像 1 中坐標為 (x,y) 的像素 p,求 p 的卷積 c,若 c>0,則將圖像 2 中 (x,y) The pixel value at is set to 0(即黑色),否則設置成 255(即白色),其中,1≤x≤w−2、1≤y≤h−2;
4)保存圖像 2,圖像 2That is, the test results are stored.

其中,第 3 step to calculate (x,y) 處像素 p 的卷積 c,計算方法如下:

1)Choose a convolution kernel(The convolution kernel selected for this level is the one shown on the left above 3×3 矩陣);
2)選取以 p 為中心的 3×3 圖像區域,如上右圖所示,Each small square represents a pixel,Numbers represent pixel color values,像素 p 的坐標為 (x,y),Then the selected image area is the yellow shading area;
3)對於步驟 1 中選擇的 3×3 Convolution kernels and steps 2 中選取的 3×3 圖像區域,Multiply the elements at their corresponding positions,然後再求和,That is to get pixels p 的卷積 c,例如,Pixels in the image above p 的卷積為:

c=1×11+1×12+1×13+1×14−8×15+1×101+1×16+1×102+1×103=252

任務

The task of this level is to complete the program,to enable edge detection.The procedure of this pass is similar to the structure of Shangguan,You can refer to the above.

相關知識

略.

編程要求

在 Begin-End Interval completion code,See above for specific requirements.

測試說明

The correct results of the test set are as follows:

(1  , 1 ) -> 0
(34 , 27) -> -537
(117, 78) -> -576
(242, 97) -> 528
(276, 61) -> 0

Your image is the same as the correct answer!

說明如下:

1)The system will call what you wroteconvolute函數,並以“像素坐標 -> 卷積”format to print the results,as in the test set 1 行的(1 , 1 ) -> 0表示:(1,1) The convolution of the pixel at is 0;
2)此外,The system checks the image files generated by the program,If it is correct then print your image on the last line of the test set with the same answer as the correct answer!.

開始你的任務吧,祝你成功!

from PIL import Image# 求圖像img中(x,y)The convolution of the pixel atcdef convolute(img, x, y):    ########## Begin ##########    juanjihe = [1,1,1,1,-8,1,1,1,1]    L = []    xl = [x - 1, x, x + 1]    yl = [y - 1, y, y + 1]    for j in yl:        for i in xl:            gray = img.getpixel((i, j))  # Take out the gray value            L.append(gray)    c = 0    for i,j in zip(juanjihe,L):        c = c + i*j    ########## End ##########    return c# 對圖像文件1進行邊緣檢測,and save the result as an image file2# 圖像文件1和2的路徑分別為path2和path3def detectEdge(path2, path3):    img1 = Image.open(path2)  # 圖像1    img1 = img1.convert('L')  # 將圖像1轉換為灰度圖    w, h = img1.size    img2 = Image.new('L', (w, h), 'white')  # 圖像2    ########## Begin ##########    ##此部分功能:依次求img1The convolution of each pixel in c,再將c放到img2的對應位置    for x in range(1, w - 1):        for y in range(1, h - 1):            c = convolute(img1, x, y)  # 計算卷積c            if c>0:                s=0            else:                s=255            img2.putpixel((x, y), s)  # 再將c放到img2的對應位置    ########## End ##########    img2.save(path3)path2 = 'step5.bmp'  # 原始圖像path3 = 'step5_2.bmp'  # Detected edge imagedetectEdge(path2, path3)

到此,關於“pythonHow to implement image edge detection”的學習就結束了,希望能夠解決大家的疑惑.理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速雲網站,小編會繼續努力為大家帶來更多實用的文章!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved