零基礎學Python--有趣的影象處理
今天我們來聊一點能讓大家比較提的起興趣的內容,大家可能覺得說Python的資料處理也挺枯燥的,尤其那些對於數字沒有什麼感覺的同學。當然,Python不僅僅是可以處理資料了。我們今天就來看看一些好玩的影象的處理。
影象處理對於很多同學來說,可能覺得這也太複雜了吧。本來影象處理確實是一個不那麼容易的學科,但是我們有了Python的一些包之後,這件事就變得無比的簡單。我們甚至可以用Python來做很多有意思的和好玩的事情,並非只有演算法科學家們才能玩轉影象處理了。
在影象處理界有一個鼎鼎有名的美女,名叫Lenna。相信看到圖片之後,大家會說,原來是她。

那麼我們在Python裡如何來秀出這張圖呢?首先做影象處理,OpenCV是少不了的選擇了。我們先import包,然後我們這裡會定一個函式,這個函式用來顯示圖片。在整片文章裡都將會用到這個函式。
import cv2
def show_image(image):
>>>>cv2.imshow('Baby', image)
>>>>cv2.waitKey(0)
>>>>cv2.destroyAllWindows()
那麼同學們,我們現在就開始擺弄一下Lenna嗎?不,我們的主角今天是一個可愛的小姑娘,先上圖片。

灰度圖
閒話不多說,我們先來看看灰度圖如何獲得!
file = 'baby.jpg'
img = cv2.imread(file)
gray = cv2.cvtColor(
img,
cv2.COLOR_BGR2GRAY)
show_image(gray)
執行結果:

修改對比度
可能同學們覺得,不就用個軟體就搞定了嗎?但是自己寫個程式碼,來實現這個功能不是更有意思嗎?並且只用了區區幾行程式碼!
file = 'baby.jpg'
img = cv2.imread(file)
contrast = cv2.addWeighted(
img,
1.5,
np.zeros(img.shape, img.dtype),
0,
0)
show_image(contrast)
執行結果:

高斯模糊
高斯模糊是一個非常有用的影象處理過程,我們很多的操作都需要這個步驟。在電子世界裡面,高斯噪聲也是非常常見的一種噪聲,因此在去噪上用途也很廣泛。
file = 'baby.jpg'
img = cv2.imread(file)
blur = cv2.GaussianBlur(
img,
(15,15),
0)
show_image(blur)
執行結果:

二值化
做過二值化之後,我們的影象就會變成黑白色,事實上二值化也是影象處理和識別中必不可少的一個過程。
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)
執行結果:

邊緣風格
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)

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

接下來我們進行去噪。
file = 'noise.jpg'
img = cv2.imread(file)
denoised = cv2.fastNlMeansDenoisingColored(
img,
None,
20,
10,
7,
21)
show_image(denoised)
執行結果:

我們可以看到去噪後的圖片顯得有點模糊,但是整體比有噪點的效果是不是看著舒服多了呢?這裡其實和我們的引數設定有關係,噪點也比較狠一點。
畫出輪廓
下面我們來找出影象中的輪廓,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可以做什麼。幾行程式碼已經很讓人驚豔了對嗎?
物體檢測
物體檢測我們用了一群孩子的圖片,當然物體檢測只是個名稱而已,之所以我們沒有叫行人檢測,是因為我比較不喜歡吹牛,我們僅僅是檢測到不知道是什麼,然後畫個最大框。

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)
執行結果:

人臉檢測
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)
執行結果:

同學們可能要說了,就一人臉,我們能檢測多張人臉嗎?當然可以,我們來看下面的程式碼。
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的要義就是,如果有輪子,我們幹嘛要再造一個。
人工智慧與深度學習做量化請關注:AI量化(https://t.zsxq.com/RvfY37y) 星球限時免費,如需加入,請私信我獲得免費邀請碼!

零基礎學習Python與深度學習應用請關注星球:Python與深度學習 https://t.zsxq.com/bUFayZ3

微信公眾號:QTechAI
