c++ - 当对象靠在一起时将帧保存到文件opencv

标签 c++ file opencv save frame

我正在跟踪两个对象,并希望在它们彼此相距不到 100 时保存一帧。 我使用以下方法测量距离:

bool bSavePic = false;

void example{
    int x1,y1,x2,y2;
int distance; //distance between two objects

if(initialMarkers.size()>1){
x1 = initialMarkers.at(0).getXPos();
y1 = initialMarkers.at(0).getYPos();
x2 = initialMarkers.at(1).getXPos();
y2 = initialMarkers.at(1).getYPos();

distance = (int)sqrt((double)((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)));

cv::putText(cameraFeed,intToString(distance),cv::Point(50,50),1,1,Scalar(255,0,0));

  if (distance < 100 )
        {
            bSavePic= true;
            }else{
            bSavePic= false;
        }
     }

我遇到的问题是当它们彼此相距不到 100 时,以下代码输出视频流,然后在标记距离再次大于 100 时保存帧。

if (bSavePic == true)
        {
            putText(cameraFeed,"Saving Image",Point(50,70),1,1,Scalar(0,0,255),1);
            waitKey(10);
            capture >> saveImage;
        }

    char buffer[1000];
    for(int c=0; c<1; c++)
        {
            sprintf(buffer,"C:\\Users\\Scott\\Documents\\Visual Studio 2010\\Projects\\MultipleObjectTracking\\Image-%d.jpg",c);
            imwrite(buffer, saveImage);             
        }

    if(!saveImage.empty())
        {
            Mat readImage;
            readImage = imread(buffer,CV_LOAD_IMAGE_COLOR);
            imshow(windowName4, readImage);
        }

关于如何在距离小于 100 时立即保存第一帧,并且仅在距离超出 100 并返回范围内时才保存另一帧,有什么建议吗?

备注c<1因为它在距离小于 100 时保存每一帧。

最佳答案

我用

解决了这个问题
bool bSingleFrame = true;

 int main{

        if (bSavePic == true)//distance<100
    {

        if (bSingleFrame == true)
        {
        putText(cameraFeed,"Saving Image",Point(50,70),1,1,Scalar(0,0,255),1);
        Mat saveImage;
        capture >> saveImage;
            stringstream ssFileName;                    //string stream initialised through each passing of while loop
            ssFileName << "Image-" << c << ".jpg";      //File name based on frame captured 0++
            ssFileName >> sFileName;


        if(!saveImage.empty())                          //check if frame captured is stored in matrix
        {
            cout << "Frame captured." << endl;          //if data in frame cout
            imwrite(sFileName.c_str(), saveImage);      //save frame
            saveImage.release();                        
            c++;
            if (bSavePic == true)                       //if distance still <100
            {
                bSingleFrame = false;
            }

            Mat readImage;
            readImage = imread(sFileName.c_str(),CV_LOAD_IMAGE_COLOR);  
            imshow(windowName4, readImage);             //show captured frame in new window
        }else{
            cout << "Error, could not capture frame to save." << endl;
        }//if(!saveImage.empty())
        }//bSingleFrame
    }//ifbSavePic

    if (bSavePic == false)
            {
                bSingleFrame = true;//if distance back >100 can capture another frame
                }
}

请注意,这只是一个片段。但它满足了需要。

关于c++ - 当对象靠在一起时将帧保存到文件opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22761280/

相关文章:

java - 在 Java 中存储数据的最佳方式(如 pickle)

iphone - OpenCV 人脸检测 - vector 问题

Python/opencv 在使用 self 时从 .read() 获取 ret、frame

C++ Shell 多管道不工作?

c++ - DebugBreak 不中断

c - C 中的文件正在识别不必要的行

具有多个 *.cpp 文件的 Linux 上的 C++ 生成文件

python - 属性错误: module 'cv2.cv2' has no attribute 'faces' in OpenCV

c++ - boost::Unique_Ptr 对象列表

C++ constexpr 构造函数初始化垃圾值