簡體   English   中英

如何在 OpenCV 中以橢圓形式捕獲圖像?

[英]How to capture image in ellipse form in OpenCV?

有人可以指導我嗎? 我正在嘗試檢測面部,然后使用 OpenCV 以橢圓形式捕獲圖像。 以下是我運行 python 腳本時的外觀示例。

在此處輸入圖像描述

目前,我只是檢測面部並創建了這個固定框架。 我還設法找到了如何以矩形形式執行此操作的解決方案,但我需要以橢圓形式捕獲圖像。 有人可以幫我嗎? 另外,這是我的代碼:

import cv2
import sys
import numpy as np
import matplotlib.pyplot as plt

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=25,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
    
        #320 horizontal position, 250 vertical position, 80 horizontal size, 120 vertical size
        #0 angle, 0 startangle, 360 endangle
        #(0, 255, 0) color, 2 thickness
    
        startAngle = 0
        endAngle = 10
        for z in range(20):
            cv2.ellipse(frame, (320, 250), (80, 120), 0, startAngle, endAngle, (255, 255, 255), 2)
            startAngle = startAngle+20
            endAngle = endAngle+20
    
        #Centered Vertical Dashed Line
        xCord = 320
        yCord = 400
        for z in range(12):
            cv2.line(frame, (xCord, yCord), (xCord, yCord-20), (255, 255, 255), 2)
            yCord = yCord-25

        #Upper Horizontal Dashed Line
        xCord = 160
        yCord = 130
        for z in range(13):
            cv2.line(frame, (xCord, yCord), (xCord+20, yCord), (255, 255, 255), 2)
            xCord = xCord+25
    
        #Lower Horizontal Dashed Line
        xCord = 160
        yCord = 370
        for z in range(13):
            cv2.line(frame, (xCord, yCord), (xCord+20, yCord), (255, 255, 255), 2)
            xCord = xCord+25
    
        cv2.putText(frame, 'Head', (330, 120), cv2.FONT_HERSHEY_SIMPLEX , 0.5, (255, 255, 255), 2) 
        cv2.putText(frame, 'Chin', (330, 390), cv2.FONT_HERSHEY_SIMPLEX , 0.5, (255, 255, 255), 2) 

        # Display the resulting frame
        cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    if cv2.waitKey(1) & 0xFF == ord('c'):
        crop_img = frame[y-50: y+h+10, x: x+w] # Crop from x, y, w, h -> 100, 200, 300, 400, y-50 is to include head in the picture too y+h+10 is to include chin.
            cv2.imwrite("media/faces/face.jpg", crop_img)

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

編輯:我已經編輯了代碼,使其成為包含一張臉的圖像的獨立代碼。

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

rgb = cv2.imread('/path/to/your/faceImage.jpg')
gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=25,
    minSize=(30, 30),
    flags=cv2.CASCADE_SCALE_IMAGE
)

x,y,w,h = faces[0] # Working with image with only one face
imh,imw = gray.shape

center_x, center_y = int(x+w/2), int(y+h/2)

mask = np.zeros((imh,imw),np.uint8)
cv2.ellipse(mask, (center_x, center_y), (int(w/2), int(h/2)), 0, 0, 360, 255, cv2.FILLED)
rgb[mask == 0] = 255
plt.imshow(rgb[y:y+h, x:x+w])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM