iT邦幫忙

2024 iThome 鐵人賽

DAY 19
0
自我挑戰組

從零開始學Python系列 第 19

[Day19] Python OpenCV -4

  • 分享至 

  • xImage
  •  
  1. 輪廓偵測:把圖形上各個外框找出來
    原圖:
    https://ithelp.ithome.com.tw/upload/images/20240909/201688114y4bfSqNna.jpg
  • 先將圖片轉換成灰階
import cv2
import numpy as np

img = cv2.imread("image.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("img", img)
cv2.waitKey(0)

https://ithelp.ithome.com.tw/upload/images/20240909/20168811UpHuNj4Mm8.png

  • 利用邊緣檢測來取得圖片的輪廓
import cv2
import numpy as np

img = cv2.imread("image.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(img, 36, 100)

cv2.imshow("img", img)
cv2.imshow("canny", canny)
cv2.waitKey(0)

https://ithelp.ithome.com.tw/upload/images/20240909/20168811N4xx2Hwtc5.png

  • 偵測輪廓
    cv2.RETR_EXTERNAL:偵測外輪廓
    cv2.CHAIN_APPROX_NONE:不壓縮水平or垂直
    利用for迴圈可以跑過所有輪廓
import cv2
import numpy as np

img = cv2.imread("image.jpg")
img_contour = img.copy()
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(img, 36, 100)
contours, heirrchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for cnt in contours:
    cv2.drawContours(img_contour, cnt, -1, (0,255,0), 5)

cv2.imshow("img", img)
cv2.imshow("canny", canny)
cv2.imshow("img_contour", img_contour)
cv2.waitKey(0)  

https://ithelp.ithome.com.tw/upload/images/20240909/20168811iGmrXEnwUT.png

  • 獲得輪廓之後,可以取得輪廓內的面積、邊長
import cv2
import numpy as np

img = cv2.imread("image.jpg")
img_contour = img.copy()
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(img, 36, 100)
contours, heirrchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for cnt in contours:
    cv2.drawContours(img_contour, cnt, -1, (0,255,0), 5)
    cv2.contourArea(cnt) #面積,若要取得,可以print出來
    print(cv2.arcLength(cnt, True)) #邊長

cv2.imshow("img", img)
cv2.imshow("canny", canny)
cv2.imshow("img_contour", img_contour)
cv2.waitKey(0)  
  • 將圖片中的圖形框起來,並用if條件來過濾雜訊
import cv2
import numpy as np

img = cv2.imread("image.jpg")
img_contour = img.copy()
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(img, 36, 100)
contours, heirrchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for cnt in contours:
    cv2.drawContours(img_contour, cnt, -1, (0,255,0), 5)
    area = cv2.contourArea(cnt)
    # print(cv2.arcLength(cnt, True))
    if area>500:
        peri = cv2.arcLength(cnt, True)
        vertices = cv2.approxPolyDP(cnt, peri*0.02, True)
        print(len(vertices)) 
        x, y, w, h = cv2.boundingRect(vertices)
        cv2.rectangle(img_contour, (x, y), (x+w, y+h), (255,0,0), 5)

cv2.imshow("img", img)
cv2.imshow("canny", canny)
cv2.imshow("img_contour", img_contour)
cv2.waitKey(0)  

https://ithelp.ithome.com.tw/upload/images/20240909/20168811MzIr17DW4f.png

  • 因為前一步已經用vertices = cv2.approxPolyDP(cnt, peri*0.02, True)來判斷圖形的頂點數目,所以接下來可以用if條件來判斷框出來的框框裡面是幾邊形
import cv2
import numpy as np

img = cv2.imread("image.jpg")
img_contour = img.copy()
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(img, 36, 100)
contours, heirrchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for cnt in contours:
    cv2.drawContours(img_contour, cnt, -1, (0,255,0), 5)
    area = cv2.contourArea(cnt)
    # print(cv2.arcLength(cnt, True))
    if area>500:
        peri = cv2.arcLength(cnt, True)
        vertices = cv2.approxPolyDP(cnt, peri*0.02, True)
        corners = len(vertices)
        x, y, w, h = cv2.boundingRect(vertices)
        cv2.rectangle(img_contour, (x, y), (x+w, y+h), (255,0,0), 5)
        if corners == 3:
            cv2.putText(img_contour, "triangle", (x, y+150), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255),2)
        elif corners == 4:
            cv2.putText(img_contour, "rectangle", (x, y+200), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255),2)
        elif corners == 5:
            cv2.putText(img_contour, "pentagon", (x, y+250), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255),2)
        elif corners == 6:
            cv2.putText(img_contour, "hexagon", (x, y+250), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
        else:
            cv2.putText(img_contour, "circle", (x+50, y+250), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255),2)


cv2.imshow("img", img)
cv2.imshow("canny", canny)
cv2.imshow("img_contour", img_contour)
cv2.waitKey(0)  

https://ithelp.ithome.com.tw/upload/images/20240909/201688119limqpLJFJ.png

  1. 人臉辨識
  • 把臉辨識出來:可以使用opencv在github上面的模型
    opencv:openc github模型
    人臉辨識github code:人臉辨識github code
    可以將此file下載後,存在與opencv執行檔相同的資料夾中,檔案格式為xml
import cv2

img = cv2.imread("person.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = cv2.CascadeClassifier("face_detect.xml")
facerac = face.detectMultiScale(gray, 1.2, 3) #(圖片, 縮小倍數, 幾個框框:臉部要被框到幾次才算)

print(len(facerac))

for x, y, w, h in facerac:
    cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 3)  


cv2.imshow("img", img)
cv2.waitKey(0)

https://ithelp.ithome.com.tw/upload/images/20240909/20168811HKvhoORZhx.png


上一篇
[Day18] Python OpenCV -3
下一篇
[Day20] Python 爬蟲-1
系列文
從零開始學Python30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言