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

Python 将感兴趣区域提取为单独的图像,python,python-3.x,image-processing,image-segmentation,Python,Python 3.x,Image Processing,Image Segmentation,我正在做一个项目,我必须从人体的热图像中检测鼻孔,以检测呼吸时间,利用它我可以检测呼吸频率。我已经完成了分割,但我无法将鼻孔部分提取为单独的图像。我已附上代码和下图: #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 an

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

#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()


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

在帧变量(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)

你是只想要鼻孔还是想要子集只包含你的“点”或保持“正方形”面边界框?我只需要鼻孔作为一个图像,你将如何定义鼻孔?它是基于点,某个包含所有点的边界框,还是给出鼻孔位置的某个子集?我实际上使用ibug300W数据集来训练检测鼻孔…因此基于此,鼻孔区域可以定义为如上所示的点,但是我更喜欢将鼻孔区域提取为一个单独的图像。我也尝试过这样做。但是我得到了一个属性错误,指出“numpy.ndarray”对象没有“left”属性。@Alexander:是因为数据集的缘故,我不能像你上面说的那样使用边界框提取鼻孔吗?不应该是这样。如果您只是在终端中运行框架变量,它应该类似于:array([[int,int,int,int,int,int,int,int,int,int,]],dtype=uint8)对吗?基本上,我所看到的框架变量是一个数组,在调整大小后,它的宽度为500。只要它是一个框架,你就应该能够对它进行子集。我会试试你所说的……谢谢你,顺便说一句:)