在 Raspbian 上裁剪图像 OpenCV 2
cropping image OpenCV 2 on Raspbian
我正在使用连接到 Raspberry Pi 2 型号 B 的网络摄像头拍照。然后,我检测图片中的面部并裁剪面部并将其另存为单独的图像。下面的代码在 Windows 中完美运行。
import numpy as np
import cv2
import math
from random import random
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
ret,frame = cap.read()
#naming the image and save path
img_name = str(int(math.ceil(100000*random())))
img_path = 'images/'+img_name+'.png'
crop_img_path = 'images/'+'crop_'+img_name+'.png'
# directly saving the image
cv2.imwrite(img_path,frame)
cap.release()
img = cv2.imread(img_path) #read image to numeric array
#printing image information
print str(img.shape) + " "
print str(img.size) + " "
print str(img.dtype)
#detecting face
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
face_cascade.load('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) # drawing rectangle (Blue, thickness 2)
print str(x)+" "+str(y)+" "+str(x+w)+" "+str(y+h) #printing region coordinates of rectangle
crop_img = img[y:y+h,x:x+w]
cv2.imwrite(crop_img_path,crop_img)
但它在 Raspbian 中给出以下错误:
Traceback (most recent call last):
File "xyz.py", line 35, in <module>
crop_img = img[y:y+h,x:x+w]
TypeError: 'NoneType' object has no attribute '__getitem__'
注1:原始截取图片保存在images文件夹中。
注意 2:我使用以下命令为 Python 安装了 OpenCV:
sudo apt-get install python-opencv
您的问题出在这一行:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
在这里您将 img
重新分配给 None
所以您失去了它的原始对象。
参照OpenCV2 documentation,cv2.rectangle
returnsNone
.
因此,
crop_img = img[y:y+h,x:x+w]
给你错误信息
rectangle
Draws a simple, thick, or filled up-right rectangle.
C++: void rectangle(Mat& img, Point pt1, Point pt2, const Scalar&
color, int thickness=1, int lineType=8, int shift=0)
C++: void rectangle(Mat& img, Rect rec, const Scalar& color, int
thickness=1, int lineType=8, int shift=0 )
Python: cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[,
shift]]]) → None
C: void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar
color, int thickness=1, int line_type=8, int shift=0 )
Python: cv.Rectangle(img, pt1, pt2, color, thickness=1, lineType=8,
shift=0) → None
因此,为了修复您的代码,您不应重新分配 img
,而应执行以下操作:
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) # drawing rectangle (Blue, thickness 2)
print str(x)+" "+str(y)+" "+str(x+w)+" "+str(y+h) #printing region coordinates of rectangle
crop_img = img[y:y+h,x:x+w]
cv2.imwrite(crop_img_path,crop_img)
我正在使用连接到 Raspberry Pi 2 型号 B 的网络摄像头拍照。然后,我检测图片中的面部并裁剪面部并将其另存为单独的图像。下面的代码在 Windows 中完美运行。
import numpy as np
import cv2
import math
from random import random
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
ret,frame = cap.read()
#naming the image and save path
img_name = str(int(math.ceil(100000*random())))
img_path = 'images/'+img_name+'.png'
crop_img_path = 'images/'+'crop_'+img_name+'.png'
# directly saving the image
cv2.imwrite(img_path,frame)
cap.release()
img = cv2.imread(img_path) #read image to numeric array
#printing image information
print str(img.shape) + " "
print str(img.size) + " "
print str(img.dtype)
#detecting face
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
face_cascade.load('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) # drawing rectangle (Blue, thickness 2)
print str(x)+" "+str(y)+" "+str(x+w)+" "+str(y+h) #printing region coordinates of rectangle
crop_img = img[y:y+h,x:x+w]
cv2.imwrite(crop_img_path,crop_img)
但它在 Raspbian 中给出以下错误:
Traceback (most recent call last):
File "xyz.py", line 35, in <module>
crop_img = img[y:y+h,x:x+w]
TypeError: 'NoneType' object has no attribute '__getitem__'
注1:原始截取图片保存在images文件夹中。
注意 2:我使用以下命令为 Python 安装了 OpenCV:
sudo apt-get install python-opencv
您的问题出在这一行:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
在这里您将 img
重新分配给 None
所以您失去了它的原始对象。
参照OpenCV2 documentation,cv2.rectangle
returnsNone
.
因此,
crop_img = img[y:y+h,x:x+w]
给你错误信息
rectangle
Draws a simple, thick, or filled up-right rectangle.
C++: void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
C++: void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )
Python: cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None
C: void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
Python: cv.Rectangle(img, pt1, pt2, color, thickness=1, lineType=8, shift=0) → None
因此,为了修复您的代码,您不应重新分配 img
,而应执行以下操作:
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) # drawing rectangle (Blue, thickness 2)
print str(x)+" "+str(y)+" "+str(x+w)+" "+str(y+h) #printing region coordinates of rectangle
crop_img = img[y:y+h,x:x+w]
cv2.imwrite(crop_img_path,crop_img)