今天我們來聊一點能讓大家比較提的起興趣的內容,大家可能覺得說Python的數據處理也挺枯燥的,尤其那些對於數字沒有什麼感覺的同學。當然,Python不僅僅是可以處理數據了。我們今天就來看看一些好玩的圖像的處理。

圖像處理對於很多同學來說,可能覺得這也太複雜了吧。本來圖像處理確實是一個不那麼容易的學科,但是我們有了Python的一些包之後,這件事就變得無比的簡單。我們甚至可以用Python來做很多有意思的和好玩的事情,並非只有算法科學家們才能玩轉圖像處理了。

在圖像處理界有一個鼎鼎有名的美女,名叫Lenna。相信看到圖片之後,大家會說,原來是她。

零基礎學Python--有趣的圖像處理

那麼我們在Python裏如何來秀出這張圖呢?首先做圖像處理,OpenCV是少不了的選擇了。我們先import包,然後我們這裏會定一個函數,這個函數用來顯示圖片。在整片文章裏都將會用到這個函數。

import cv2

def show_image(image):

>>>>cv2.imshow('Baby', image)

>>>>cv2.waitKey(0)

>>>>cv2.destroyAllWindows()

那麼同學們,我們現在就開始擺弄一下Lenna嗎?不,我們的主角今天是一個可愛的小姑娘,先上圖片。

零基礎學Python--有趣的圖像處理

灰度圖

閒話不多說,我們先來看看灰度圖如何獲得!

file = 'baby.jpg'

img = cv2.imread(file)

gray = cv2.cvtColor(

img,

cv2.COLOR_BGR2GRAY)

show_image(gray)

運行結果:

零基礎學Python--有趣的圖像處理

修改對比度

可能同學們覺得,不就用個軟件就搞定了嗎?但是自己寫個代碼,來實現這個功能不是更有意思嗎?並且只用了區區幾行代碼!

file = 'baby.jpg'

img = cv2.imread(file)

contrast = cv2.addWeighted(

img,

1.5,

np.zeros(img.shape, img.dtype),

0,

0)

show_image(contrast)

運行結果:

零基礎學Python--有趣的圖像處理

高斯模糊

高斯模糊是一個非常有用的圖像處理過程,我們很多的操作都需要這個步驟。在電子世界裏面,高斯噪聲也是非常常見的一種噪聲,因此在去噪上用途也很廣泛。

file = 'baby.jpg'

img = cv2.imread(file)

blur = cv2.GaussianBlur(

img,

(15,15),

0)

show_image(blur)

運行結果:

零基礎學Python--有趣的圖像處理

二值化

做過二值化之後,我們的圖像就會變成黑白色,事實上二值化也是圖像處理和識別中必不可少的一個過程。

file = 'baby.jpg'

img = cv2.imread(file)

gauss = cv2.cvtColor(

cv2.GaussianBlur(img,(7,7), 0),

cv2.COLOR_BGR2GRAY)

_, thresh = cv2.threshold(

gauss,

80,

255,

cv2.THRESH_BINARY)

colored = cv2.cvtColor(

thresh,

cv2.COLOR_GRAY2RGB)

show_image(colored)

運行結果:

零基礎學Python--有趣的圖像處理

邊緣風格

file = 'baby.jpg'

img = cv2.imread(file)

gauss = cv2.cvtColor(

cv2.GaussianBlur(img, (7,7), 0),

cv2.COLOR_BGR2GRAY)

_, thresh = cv2.threshold(

gauss,

80,

255,

cv2.THRESH_BINARY)

cups_edges = cv2.Canny(

thresh,

threshold1=90,

threshold2=110)

colored = cv2.cvtColor(

cups_edges,

cv2.COLOR_GRAY2RGB)

show_image(colored)

零基礎學Python--有趣的圖像處理

降噪

我們先將原圖加上一些高斯噪點,加過噪點的圖片如下。

零基礎學Python--有趣的圖像處理

接下來我們進行去噪。

file = 'noise.jpg'

img = cv2.imread(file)

denoised = cv2.fastNlMeansDenoisingColored(

img,

None,

20,

10,

7,

21)

show_image(denoised)

運行結果:

零基礎學Python--有趣的圖像處理

我們可以看到去噪後的圖片顯得有點模糊,但是整體比有噪點的效果是不是看着舒服多了呢?這裏其實和我們的參數設置有關係,噪點也比較狠一點。

畫出輪廓

下面我們來找出圖像中的輪廓,Python可以完美的勾畫

file = 'baby.jpg'

img = cv2.imread(file)

gray = cv2.cvtColor(

img,

cv2.COLOR_BGR2GRAY)

gauss = cv2.GaussianBlur(

gray,

(5, 5),

0)

_, bin = cv2.threshold(

gauss,

150,

255,

cv2.THRESH_BINARY)

bin = cv2.bitwise_not(bin)

_, contours, _ = cv2.findContours(

bin,

cv2.RETR_EXTERNAL,

cv2.CHAIN_APPROX_SIMPLE)

imgWithContours = np.copy(img)

minArea = 300

filtedContours = [cnt

for cnt in contours

if cv2.contourArea(cnt) > minArea]

cv2.drawContours(

imgWithContours,

filtedContours,

-1,

(0,255,0))

show_image(imgWithContours)

運行結果:

零基礎學Python--有趣的圖像處理

大家可以看到,圖中畫出了僅僅畫帽子的輪廓,原因是因爲我們要去做更精細的一些調試纔可以畫出其他的輪廓,畢竟我們這裏僅僅只是秀一下Python可以做什麼。幾行代碼已經很讓人驚豔了對嗎?

物體檢測

物體檢測我們用了一羣孩子的圖片,當然物體檢測只是個名稱而已,之所以我們沒有叫行人檢測,是因爲我比較不喜歡吹牛,我們僅僅是檢測到不知道是什麼,然後畫個最大框。

零基礎學Python--有趣的圖像處理

file = 'children.jpg'

img = cv2.imread(file)

img = cv2.resize(

img,

(1060, 707))

gray = cv2.cvtColor(

img,

cv2.COLOR_BGR2GRAY)

gauss = cv2.GaussianBlur(

gray,

(5, 5),

0)

_, bin = cv2.threshold(

gauss,

150,

255,

cv2.THRESH_BINARY)

bin = cv2.bitwise_not(bin)

_, contours, _ = cv2.findContours(

bin,

cv2.RETR_EXTERNAL,

cv2.CHAIN_APPROX_SIMPLE)

imgWithContours = np.copy(img)

minArea = 3000

maxArea = 20000

filtedContours = [cnt

for cnt in contours

if cv2.contourArea(cnt) > minArea

and cv2.contourArea(cnt) < maxArea]

cv2.drawContours(

imgWithContours,

filtedContours,

-1,

(0,255,0))

imgWithBounding = np.copy(img)

for contour in filtedContours:

>>>>x, y, w, h = cv2.boundingRect(contour)

>>>>cv2.rectangle(

>>>>imgWithBounding,

>>>>(x, y),

>>>>(x + w, y + h),

>>>>(0, 255, 0),

>>>>3)

show_image(imgWithBounding)

運行結果:

零基礎學Python--有趣的圖像處理

人臉檢測

file = 'baby.jpg'

cascFile = "haarcascade_frontalface_default.xml"

cascade = cv2.CascadeClassifier(cv2.data.haarcascades + cascFile)

img = cv2.imread(file)

gray = cv2.cvtColor(

img,

cv2.COLOR_BGR2GRAY)

faces = cascade.detectMultiScale(

gray,

scaleFactor=1.1,

minNeighbors=5,

minSize=(300, 300),

flags = cv2.CASCADE_SCALE_IMAGE)

for (x, y, w, h) in faces:

>>>>cv2.rectangle(img,

>>>>(x, y),

>>>>(x+w, y+h),

>>>>(0, 255, 0),

>>>>2)

show_image(img)

運行結果:

零基礎學Python--有趣的圖像處理

同學們可能要說了,就一人臉,我們能檢測多張人臉嗎?當然可以,我們來看下面的代碼。

file = 'children.jpg'

cascFile = "haarcascade_frontalface_default.xml"

cascade = cv2.CascadeClassifier(cv2.data.haarcascades + cascFile)

img = cv2.imread(file)

img = cv2.resize(

img,

(1920, 1080))

gray = cv2.cvtColor(

img,

cv2.COLOR_BGR2GRAY)

faces = cascade.detectMultiScale(gray,

scaleFactor=1.1,

minNeighbors=5,

minSize=(10, 10),

maxSize=(100,100),

flags = cv2.CASCADE_SCALE_IMAGE)

for (x, y, w, h) in faces:

>>>>cv2.rectangle(img,

(x, y),

(x+w, y+h),

(0, 255, 0),

2)

show_image(img)

運行結果:

零基礎學Python--有趣的圖像處理

是不是很神奇,我們可能一直覺得人臉檢測是前沿技術,再和人工智能、深度學習一結合,簡直就是難上加難。其實不然,大家看到,其實很簡單的幾行代碼,也可以實現一個簡單的人臉檢測。當然,背後其實也有很多的代碼。但是Python的要義就是,如果有輪子,我們幹嘛要再造一個。

相关文章