使用 opencv 4 和 VS 2019 的错误

Posted

技术标签:

【中文标题】使用 opencv 4 和 VS 2019 的错误【英文标题】:Errors using opencv 4 and VS 2019 【发布时间】:2020-02-25 15:09:21 【问题描述】:

以下代码在使用 Visual Studio 2019 进行编译时产生错误。

// ImageSubtractionCpp.sln
// main.cpp

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

using namespace cv;

#include<iostream>
#ifdef WINDOWS
#include<conio.h>           // it may be necessary to change or remove this line if not using Windows
#endif

#include "Blob.h"

// global variables ///////////////////////////////////////////////////////////////////////////////
const cv::Scalar SCALAR_BLACK = cv::Scalar(0.0, 0.0, 0.0);
const cv::Scalar SCALAR_WHITE = cv::Scalar(255.0, 255.0, 255.0);
const cv::Scalar SCALAR_BLUE = cv::Scalar(255.0, 0.0, 0.0);
const cv::Scalar SCALAR_GREEN = cv::Scalar(0.0, 200.0, 0.0);
const cv::Scalar SCALAR_RED = cv::Scalar(0.0, 0.0, 255.0);

///////////////////////////////////////////////////////////////////////////////////////////////////
int main(void) 

    cv::VideoCapture capVideo;

    cv::Mat imgFrame1;
    cv::Mat imgFrame2;

    capVideo.open("768x576.avi");

    if (!capVideo.isOpened())                                                  // if unable to open video file
        std::cout << "\nerror reading video file" << std::endl << std::endl;      // show error message
#ifdef WINDOWS
        _getch();                    // it may be necessary to change or remove this line if not using Windows
#endif
        return(0);                                                              // and exit program
    

    if (capVideo.get(CV_CAP_PROP_FRAME_COUNT) < 2) 
        std::cout << "\nerror: video file must have at least two frames";
#ifdef WINDOWS
        _getch();
#endif
        return(0);
    

    capVideo.read(imgFrame1);
    capVideo.read(imgFrame2);

    char chCheckForEscKey = 0;

    while (capVideo.isOpened() && chCheckForEscKey != 27) 

        std::vector<Blob> blobs;

        cv::Mat imgFrame1Copy = imgFrame1.clone();
        cv::Mat imgFrame2Copy = imgFrame2.clone();

        cv::Mat imgDifference;
        cv::Mat imgThresh;

        cv::cvtColor(imgFrame1Copy, imgFrame1Copy, cv::COLOR_BGR2GRAY);
        cv::cvtColor(imgFrame2Copy, imgFrame2Copy, cv::COLOR_BGR2GRAY);

        cv::GaussianBlur(imgFrame1Copy, imgFrame1Copy, cv::Size(5, 5), 0);
        cv::GaussianBlur(imgFrame2Copy, imgFrame2Copy, cv::Size(5, 5), 0);

        cv::absdiff(imgFrame1Copy, imgFrame2Copy, imgDifference);

        cv::threshold(imgDifference, imgThresh, 30, 255.0, CV_THRESH_BINARY);

        cv::imshow("imgThresh", imgThresh);

        cv::Mat structuringElement3x3 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
        cv::Mat structuringElement5x5 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
        cv::Mat structuringElement7x7 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(7, 7));
        cv::Mat structuringElement9x9 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(9, 9));

        cv::dilate(imgThresh, imgThresh, structuringElement5x5);
        cv::dilate(imgThresh, imgThresh, structuringElement5x5);
        cv::erode(imgThresh, imgThresh, structuringElement5x5);

        cv::Mat imgThreshCopy = imgThresh.clone();

        std::vector<std::vector<cv::Point> > contours;

        cv::findContours(imgThreshCopy, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

        cv::Mat imgContours(imgThresh.size(), CV_8UC3, SCALAR_BLACK);

        cv::drawContours(imgContours, contours, -1, SCALAR_WHITE, -1);

        cv::imshow("imgContours", imgContours);

        std::vector<std::vector<cv::Point> > convexHulls(contours.size());

        for (unsigned int i = 0; i < contours.size(); i++) 
            cv::convexHull(contours[i], convexHulls[i]);
        

        for (auto& convexHull : convexHulls) 
            Blob possibleBlob(convexHull);

            if (possibleBlob.boundingRect.area() > 100 &&
                possibleBlob.dblAspectRatio >= 0.2 &&
                possibleBlob.dblAspectRatio <= 1.2 &&
                possibleBlob.boundingRect.width > 15 &&
                possibleBlob.boundingRect.height > 20 &&
                possibleBlob.dblDiagonalSize > 30.0) 
                blobs.push_back(possibleBlob);
            
        

        cv::Mat imgConvexHulls(imgThresh.size(), CV_8UC3, SCALAR_BLACK);

        convexHulls.clear();

        for (auto& blob : blobs) 
            convexHulls.push_back(blob.contour);
        

        cv::drawContours(imgConvexHulls, convexHulls, -1, SCALAR_WHITE, -1);

        cv::imshow("imgConvexHulls", imgConvexHulls);

        imgFrame2Copy = imgFrame2.clone();          // get another copy of frame 2 since we changed the previous frame 2 copy in the processing above

        for (auto& blob : blobs)                                                   // for each blob
            cv::rectangle(imgFrame2Copy, blob.boundingRect, SCALAR_RED, 2);             // draw a red box around the blob
            cv::circle(imgFrame2Copy, blob.centerPosition, 3, SCALAR_GREEN, -1);        // draw a filled-in green circle at the center
        

        cv::imshow("imgFrame2Copy", imgFrame2Copy);

        // now we prepare for the next iteration

        imgFrame1 = imgFrame2.clone();           // move frame 1 up to where frame 2 is

        if ((capVideo.get(CV_CAP_PROP_POS_FRAMES) + 1) < capVideo.get(CV_CAP_PROP_FRAME_COUNT))        // if there is at least one more frame
            capVideo.read(imgFrame2);                            // read it
        
        else                                                   // else
            std::cout << "end of video\n";                      // show end of video message
            break;                                              // and jump out of while loop
        

        chCheckForEscKey = cv::waitKey(1);      // get key press in case user pressed esc

    

    if (chCheckForEscKey != 27)                // if the user did not press esc (i.e. we reached the end of the video)
        cv::waitKey(0);                         // hold the windows open to allow the "end of video" message to show
    
    // note that if the user did press esc, we don't need to hold the windows open, we can simply let the program end which will close the windows

    return(0);

Error   C2660   'cv::threshold': function does not take 4 arguments imagesubstraction   C:\Users\admin\source\repos\imagesubstraction\main.cpp  73  
Error   C2065   'CV_CAP_PROP_FRAME_COUNT': undeclared identifier    imagesubstraction   C:\Users\admin\source\repos\imagesubstraction\main.cpp  142 
Error (active)  E0020   identifier "CV_CAP_PROP_FRAME_COUNT" is undefined   imagesubstraction   C:\Users\admin\source\repos\imagesubstraction\main.cpp  42  
Error (active)  E0020   identifier "CV_THRESH_BINARY" is undefined  imagesubstraction   C:\Users\admin\source\repos\imagesubstraction\main.cpp  73  
Error (active)  E0020   identifier "CV_CAP_PROP_POS_FRAMES" is undefined    imagesubstraction   C:\Users\admin\source\repos\imagesubstraction\main.cpp  142 
Error   C2039   'CV_CAP_PROP_FRAME_COUNT': is not a member of 'cv::VideoCapture'    imagesubstraction   C:\Users\admin\source\repos\imagesubstraction\main.cpp  42  
Error   C2882   'cv': illegal use of namespace identifier in expression imagesubstraction   C:\Users\admin\source\repos\imagesubstraction\main.cpp  73  
Error   C2882   'cv': illegal use of namespace identifier in expression imagesubstraction   C:\Users\admin\source\repos\imagesubstraction\main.cpp  142 

【问题讨论】:

不要发布从 VS 错误选项卡复制的文本。查看编译器输出,它更加详细和详细,包含有关预期内容的有用提示。 你应该将using namespace cv移到任何包含的下方 尝试使用命名空间 cv 删除,但仍然出现相同的错误...它在 openCV 3 上工作,但在 openCV 4.2 最新版本上没有 它抱怨的那些以CV_ 为前缀的符号已经过时了。例如CV_THRESH_BINARY 应该是 cv::THRESH_BINARY 等等...请参阅最新文档。 【参考方案1】:

您可以进行以下更改:

    CV_CAP_PROP_FRAME_COUNTCAP_PROP_FRAME_COUNT CV_CAP_PROP_POS_FRAMESCAP_PROP_POS_FRAMES

【讨论】:

很老的问题。 CV_THRESH_BINARY 的错误呢? 只需将CV_THRESH_BINARY 转至THRESH_BINARY

以上是关于使用 opencv 4 和 VS 2019 的错误的主要内容,如果未能解决你的问题,请参考以下文章

使用vs2019编译和调试开源项目opencv3.4.0版本+opencv3.4.0源码网盘链接

vs2019+opencv3.4.1+pcl1.8.1环境搭建

vs2015和emgu3.4.3可以配置吗

使用 OpenCV3.4.5 在 Qt5 中与 Cuda 相关的编译错误

在 VS 2008 上启动 OpenCV 2.4.6 时出现致命错误,找不到文件

VS2019配置opencv-4.5.2-vc14_vc15