私は歩行者を検出するコードを書きました。コードは完全に機能していますが、以下のコードに示すように、多くのforループがあります。私の質問は、どのように出力に影響を与えずにforループの数を減らすことができるかです。forループの回数を減らす
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
void detectAndDisplay(Mat frame);
/** Global variables */
CascadeClassifier pedestrians_cascade;
Mat image1,image2,image3,image4,image5,image6,image7,image8,image9,image10;
Mat ped1,ped2,ped3,ped4,ped5,ped6,ped7,ped8,ped9,ped10;
int x;
int y;
int width, height;
int main()
{
// Load Face cascade (.xml file)
pedestrians_cascade = CascadeClassifier("hogcascade_pedestrians.xml");
// Read an images
image1 = imread("ped1.jpg");
image2 = imread("ped2.jpg");
image3 = imread("ped3.jpg");
image4 = imread("ped4.jpg");
image5 = imread("ped5.jpg");
image6 = imread("ped6.jpg");
image7 = imread("ped7.jpg");
image8 = imread("ped8.jpg");
image9 = imread("ped9.jpg");
image10 = imread("ped10.jpg");
// these functions use to draw rectangles
std::vector<Rect> ped1;
pedestrians_cascade.detectMultiScale(image1, ped1, 1.1, , 0|CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < ped1.size(); i++) // Iterate through all current elements (detected ped)
{
Point pt1(ped1[i].x, ped1[i].y); // Display detected ped on main window - live stream from camera
Point pt2((ped1[i].x + ped1[i].width), (ped1[i].y + ped1[i].height));
//rectangle(image1, ped1[i], Scalar(255, 0 , 0), 2, 8, 0);
rectangle(image1,pt2,pt1, Scalar(255,0,0), 2,8,0);
}
std::vector<Rect> ped2;
pedestrians_cascade.detectMultiScale(image2, ped2, 1.1, 5, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < ped2.size(); i++) // Iterate through all current elements (detected ped)
{
Point pt1(ped2[i].x, ped2[i].y); // Display detected ped on main window - live stream from camera
Point pt2((ped2[i].x + ped2[i].width), (ped2[i].y + ped2[i].height));
rectangle(image2, pt2, pt1, Scalar(255, 0 , 0), 2, 8, 0);
}
std::vector<Rect> ped3;
pedestrians_cascade.detectMultiScale(image3, ped3, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < ped3.size(); i++) // Iterate through all current elements (detected ped)
{
Point pt1(ped3[i].x, ped3[i].y); // Display detected ped on main window - live stream from camera
Point pt2((ped3[i].x + ped3[i].width), (ped3[i].y + ped3[i].height));
rectangle(image3, pt2, pt1, Scalar(255, 0 , 0), 2, 8, 0);
}
std::vector<Rect> ped4;
pedestrians_cascade.detectMultiScale(image4, ped4, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < ped4.size(); i++) // Iterate through all current elements (detected ped)
{
Point pt1(ped4[i].x, ped4[i].y); // Display detected ped on main window - live stream from camera
Point pt2((ped4[i].x + ped4[i].width), (ped4[i].y + ped4[i].height));
rectangle(image4, pt2, pt1, Scalar(255, 0 , 0), 2, 8, 0);
}
std::vector<Rect> ped5;
pedestrians_cascade.detectMultiScale(image5, ped5, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < ped5.size(); i++) // Iterate through all current elements (detected ped)
{
Point pt1(ped5[i].x, ped5[i].y); // Display detected ped on main window - live stream from camera
Point pt2((ped5[i].x + ped5[i].width), (ped5[i].y + ped5[i].height));
rectangle(image5, pt2, pt1, Scalar(255, 0 , 0), 2, 8, 0);
}
std::vector<Rect> ped6;
pedestrians_cascade.detectMultiScale(image6, ped6, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < ped6.size(); i++) // Iterate through all current elements (detected ped)
{
Point pt1(ped6[i].x, ped6[i].y); // Display detected ped on main window - live stream from camera
Point pt2((ped6[i].x + ped6[i].width), (ped6[i].y + ped6[i].height));
rectangle(image6, pt2, pt1, Scalar(255, 0 , 0), 2, 8, 0);
}
std::vector<Rect> ped7;
pedestrians_cascade.detectMultiScale(image7, ped7, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < ped7.size(); i++) // Iterate through all current elements (detected ped)
{
Point pt1(ped7[i].x, ped7[i].y); // Display detected ped on main window - live stream from camera
Point pt2((ped7[i].x + ped7[i].width), (ped7[i].y + ped7[i].height));
rectangle(image7, pt2, pt1, Scalar(255, 0 , 0), 2, 8, 0);
}
std::vector<Rect> ped8;
pedestrians_cascade.detectMultiScale(image8, ped8, 1.1, 20, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < ped8.size(); i++) // Iterate through all current elements (detected ped)
{
Point pt1(ped8[i].x, ped8[i].y); // Display detected ped on main window - live stream from camera
Point pt2((ped8[i].x + ped8[i].width), (ped8[i].y + ped8[i].height));
rectangle(image8, pt2, pt1, Scalar(255, 0 , 0), 2, 8, 0);
}
std::vector<Rect> ped9;
pedestrians_cascade.detectMultiScale(image9, ped9, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < ped9.size(); i++) // Iterate through all current elements (detected ped)
{
Point pt1(ped9[i].x, ped9[i].y); // Display detected ped on main window - live stream from camera
Point pt2((ped9[i].x + ped9[i].width), (ped9[i].y + ped9[i].height));
rectangle(image9, pt2, pt1, Scalar(255, 0 , 0), 2, 8, 0);
}
std::vector<Rect> ped10;
pedestrians_cascade.detectMultiScale(image10, ped10, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < ped10.size(); i++) // Iterate through all current elements (detected ped)
{
Point pt1(ped10[i].x, ped10[i].y); // Display detected ped on main window - live stream from camera
Point pt2((ped10[i].x + ped10[i].width), (ped10[i].y + ped10[i].height));
rectangle(image10, pt2, pt1, Scalar(255, 0 , 0), 2, 8, 0);
}
imshow("ped1", image1);
/*imshow("ped2", image2);
imshow("ped3", image3);
imshow("ped4", image4);
imshow("ped5", image5);
imshow("ped6", image6);
imshow("ped7", image7);
imshow("ped8", image8);
imshow("ped9", image9);
imshow("ped10", image10);*/
waitKey(0);
return 0;
}
この質問はおそらく[codereview.se](http://codereview.stackexchange.com/)に適しています。 – ArchbishopOfBanterbury
なぜ4つのループの数を減らしたいのですか?私はむしろ少なくとも1つを追加することをお勧めしたいと思います。なぜなら、あなたはほぼ正確なコードを10回書いたように思えるのですが、異なるオブジェクトで同じことを10回行うだけです。 – user463035818
コードが長すぎるため、forループを10回繰り返しました。私はforループを最小限にしたいと思いますが、私は出力に何の影響も与えないとわかりません。 –