Q&A
C++でOpenCVを利用して顔認識してその部分を切り取り、リサイズして透明度を考慮(平均)しながら合成するプログラムを考えています。
2枚の画像など、枚数を決めればできたのですが、image_numsという(後々コマンドラインで受け取る、画像は本当はカメラでキャプチャする予定)変数に依存してその操作をしたく実装して見たら、画像のサイズが一致しない(?)ことによるエラーが発生しました。
C,Pythonは勉強しましたが、C++はノータッチのままやっています(なぜか授業の関係でそうせざるを得なくなっています)。
C++
1#include <opencv2/opencv.hpp> 2#include <iostream> 3 4using namespace std; 5using namespace cv; 6 7void detectFaceInImage(Mat &image,Mat &faceimage, string &cascade_file){ 8 CascadeClassifier cascade; 9 cascade.load(cascade_file); 10 11 vector<Rect> faces; 12 cascade.detectMultiScale(image, faces, 1.1,3,0,Size(20,20)); 13 14 for (int i = 0; i < faces.size(); i++){ 15 rectangle(image, Point(faces[i].x,faces[i].y),Point(faces[i].x + faces[i].width,faces[i].y + faces[i].height),Scalar(0,200,0),3,CV_AA); 16 17 cv::Rect roi(Point(faces[i].x, faces[i].y), 18 Point(faces[i].x+faces[i].width, faces[i].y+faces[i].height)); 19 faceimage = image(roi); 20 } 21} 22 23Mat CompositeImage(Mat FaceImageList[], int image_nums){ 24 Mat Image =Mat::zeros(cv::Size(300,300), 0); 25 for (int i=0; i<image_nums; i++){ 26 resize(FaceImageList[i], FaceImageList[i], cv::Size(), 300.0/FaceImageList[i].cols ,300.0/FaceImageList[i].rows); 27 Image += (1/image_nums) * FaceImageList[i]; 28 } 29 30 return Image; 31} 32 33int main(int argc, char const *argv[]){ 34 Mat image1 = imread(argv[1]); 35 Mat image2 = imread(argv[2]); 36 int image_nums = 2; 37 Mat faceimage1, faceimage2, Image; 38 Mat ImageList[2] = {image1, image2}; 39 Mat FaceImageList[2] = {faceimage1, faceimage2}; 40 string FILE_NAME = "haarcascade_frontalface_alt.xml"; 41 for (int i=0; i<image_nums; i++){ 42 detectFaceInImage(ImageList[i], FaceImageList[i], FILE_NAME); 43 } 44 Image = CompositeImage(FaceImageList, image_nums); 45 imshow("test", Image); 46 47 waitKey(0); 48 49 return 0; 50}
ターミナルでの文章は以下の通りでMacで書いています。
OpenCV(3.4.1) Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, file /tmp/opencv-20180504-71644-brjrbc/opencv-3.4.1/modules/core/src/arithm.cpp, line 659 libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.1) /tmp/opencv-20180504-71644-brjrbc/opencv-3.4.1/modules/core/src/arithm.cpp:659: error: (-209) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function arithm_op Abort trap: 6
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2018/05/29 07:52
2018/06/04 03:09