2016-04-30 26 views
1

のサイズを変更する:私は、画像ファイルに顔をトリミングし、92x112にそれらのサイズを変更したい終わりPythonの - 私は顔を検出するために、以下のコードを使用していた画像

import io 
import picamera 
import cv2 
import numpy 
import PIL 
from PIL import Image 
from resizeimage import resizeimage 

#Load a cascade file for detecting faces 
face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml') 

#Create a memory stream so photos doesn't need to be saved in a file 
stream = io.BytesIO() 

#Get the picture (low resolution, so it should be quite fast) 
#Here you can also specify other parameters (e.g.:rotate the image) 
with picamera.PiCamera() as camera: 
    camera.resolution = (640, 480) 
    camera.vflip = False 
    camera.hflip = False 
    camera.brightness = 60 
    camera.capture(stream, format='jpeg') 

#Convert the picture into a numpy array 
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8) 

#Now creates an OpenCV image 
image = cv2.imdecode(buff, 1) 

#Load a cascade file for detecting faces 
#face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml') 

#Convert to grayscale 
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 

#Look for faces in the image using the loaded cascade file 
faces = face_cascade.detectMultiScale(gray, 1.1, 5) 

print "Found "+str(len(faces))+" face(s)" 

#Draw a rectangle around every found face 
#Crop faces and save to separate files 
id = 1 
for (x,y,w,h) in faces: 
    cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2) 
    cropped = image[ y : y+h, x : x+w ] 
    #RESIZE IMAGE to 92x112 
    cropped = cv2.resize(cropped,None,92,112) 
    cv2.imwrite("../reco/test_faces/cropped_face" + str(id) + ".png", cropped) 
    id = id + 1 

。 、サイズ変更にアサーションが失敗した(dsize.area()||(inv_scale_x> 0 & & inv_scale_y> 0)):私はこれを実行するとこれは私が

OpenCVのエラーを

を取得し、私は

cropped = cv2.resize(cropped,None,92,112) 

と試みるものですファイル/build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/imgproc/src/imgwarp.cpp、1835行 トレースバック(直近の最後の呼び出し): ファイル "1track.py"、48行目、 cropped = cv2.resize(cropped、None、92,112) cv2.error:/build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/imgproc/src/imgwarp.cpp:1835:エラー:(-215) dsize.area()|| (inv_scale_x> 0 & & inv_scale_y> 0)in function resize

どのような助けも素晴らしいでしょう。 申し訳ありませんが、偶然がばかげている、私はプログラミングの絶対初心者です、上のコードは、グーグルの結果です.. ありがとう!

答えて

0

イメージを新しいディメンションにサイズ変更するには、新しいディメンションと現在のディメンションの比率を知る必要があります。だから、92x112イメージに(例えば)640×480の画像を設定する場合:

640分の92 = 0.143 480分の112 = 0.233

あなたはcv2.resize機能では、これらの比率を使用します。

cropped = cv2.resize(cropped, (0,0), fx=0.143, fy=0.233) 
関連する問題