将感兴趣区域提取为单独的图像

2024-07-05 14:39:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,我必须从人体的热图像中检测鼻孔,以检测呼吸时间,利用它我可以检测呼吸频率。我已经完成了分割,但我无法将鼻孔部分提取为单独的图像。我已附上代码和下图:

#import the required packages
from imutils import face_utils
import argparse
import dlib
import imutils
import cv2
import numpy as np

#passing the shape predictor and image thro' argparse
ap=argparse.ArgumentParser()
ap.add_argument("-p", "--shapepredictor", required=True, help="path to find rquired facial landmark")
ap.add_argument("-img", "--image", required=True, help="path to input image")
args=vars(ap.parse_args())


detector=dlib.get_frontal_face_detector()
predictor=dlib.shape_predictor(args["shapepredictor"])

while True:
      frame=cv2.imread(args["image"])
      frame=imutils.resize(frame,width=500)
      gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
      rect=detector(gray, 0)

      #detecting the face and then the nose
      for i in rect:
            x,y,w,h=face_utils.rect_to_bb(i)
            cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
            shape=predictor(gray,i)
            shape=face_utils.shape_to_np(shape)

            for nx,ny in shape:
                cv2.circle(frame,(nx,ny),3,(255,0,0),-1)


      cv2.imshow("Frame",frame)
      key=cv2.waitKey(0) & 0xff
      if key==27:
              break
cv2.destroyAllWindows()

enter image description here

既然我已经检测到鼻孔部分,我该如何将其提取为单独的图像?谁能帮帮我吗


Tags: theto图像imageimportrequiredargscv2
1条回答
网友
1楼 · 发布于 2024-07-05 14:39:20

对图像进行子集的方法是在帧变量(2d np.array)上建立索引

例如:

frame=cv2.imread("somepath.jpg") # To load an image to frame
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #To convert to 2D grayscale
RoI=gray[start_x:width,start_y:height]

为了扩展我的答案,这里有一个最小的功能示例,它是从url加载的图像运行的图像子集,但使用cv2包

import numpy as np
import urllib
import cv2

url='http://scipy-lectures.org/_images/sphx_glr_plot_camera_001.png'
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)

cv2.imshow("Image", image[:200,:200])
cv2.waitKey(0)

相关问题 更多 >