You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++ OpenCV颜色检测程序出现未处理异常求助

问题:OpenCV创建滑动条时触发未处理异常

我正在Visual Studio中开发一款用于检测黑白视频中颜色的程序,运行时createTrackbar("Lower Hue", "Adjust", &hLowVal, 179);这一行触发未处理异常:

Error:Unhandled exception at 0x00007FFD5B4E4C3C in ThermalSpotDetector.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000BA269DD5C0

相关代码如下:

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


using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    //Load clip
    VideoCapture cap("C:/Users/nicho/Videos/ThermalClip1.mp4");
    //Note: location of video file on current computer may differ from initial code. Change accordingly

    //If clip cannot be loaded, exit program
    if (cap.isOpened() == false)
    {
        cout << "Error. Cannot open video file" << endl;
        cin.get();
        return -1;
    }

    namedWindow("Control", WINDOW_AUTOSIZE);//Create control window

    int hLowVal = 0;//initial hue value (lower)
    int hUpVal = 179;//initial hue value (upper)

    int hSatLow = 0;//initial saturation (lower)
    int hSatUp = 255; //initial saturation (upper)

    int valueLower = 0;
    int valueUpper = 255;

    //Create trackbars for control window
    createTrackbar("Lower Hue", "Adjust", &hLowVal, 179);
    createTrackbar("Increase Hue", "Adjust", &hUpVal, 179);
    createTrackbar("Lower Saturation", "Adjust", &hSatLow, 255);
    createTrackbar("Increase Saturation", "Adjust", &hSatUp, 255);
    createTrackbar("Lower Value", "Adjust", &valueLower, 255);
    createTrackbar("Increase Value", "Adjust", &valueUpper, 255);

    while (1)
    {
        //Load actual image
        Mat actualImage;
        bool temp = cap.read(actualImage);


        //Store converted image
        Mat convertToHSV;
        cvtColor(actualImage, convertToHSV, COLOR_BGR2HSV);

        //Matrix for window where thermal spot will be detected
        Mat detectionScreen;
        inRange(convertToHSV, Scalar(hLowVal, hSatLow, valueLower), Scalar(hUpVal, hSatUp, valueUpper), detectionScreen);

        //remove small objects from foreground
        erode(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        dilate(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        //fill up small holes in foreground
        dilate(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        erode(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        //Show the thermal spot
        imshow("Thermal Spot detected", detectionScreen);

        imshow("Original Image", detectionScreen);

        if (waitKey(30) == 27)//If escape key is pressed, break loop
        {
            break;
        }
    }
    
    return 0;
}

问题原因分析

  • 窗口名称不匹配:代码中用namedWindow("Control", WINDOW_AUTOSIZE)创建了名为"Control"的窗口,但创建滑动条时第二个参数写的是"Adjust",OpenCV无法找到这个窗口,因此抛出cv::Exception异常。
  • 视频帧读取失败未处理:如果cap.read(actualImage)返回false(比如视频播放完毕),actualImage会是空矩阵,后续调用cvtColor会导致崩溃。
  • 显示窗口内容错误:imshow("Original Image", detectionScreen)错误地显示了检测结果,应该显示原始帧actualImage。

修复后的代码

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

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    //Load clip
    VideoCapture cap("C:/Users/nicho/Videos/ThermalClip1.mp4");
    //Note: location of video file on current computer may differ from initial code. Change accordingly

    //If clip cannot be loaded, exit program
    if (cap.isOpened() == false)
    {
        cout << "Error. Cannot open video file" << endl;
        cin.get();
        return -1;
    }

    namedWindow("Control", WINDOW_AUTOSIZE);//Create control window

    int hLowVal = 0;//initial hue value (lower)
    int hUpVal = 179;//initial hue value (upper)

    int hSatLow = 0;//initial saturation (lower)
    int hSatUp = 255; //initial saturation (upper)

    int valueLower = 0;
    int valueUpper = 255;

    //Create trackbars for control window - 修复:将窗口名称改为"Control"
    createTrackbar("Lower Hue", "Control", &hLowVal, 179);
    createTrackbar("Increase Hue", "Control", &hUpVal, 179);
    createTrackbar("Lower Saturation", "Control", &hSatLow, 255);
    createTrackbar("Increase Saturation", "Control", &hSatUp, 255);
    createTrackbar("Lower Value", "Control", &valueLower, 255);
    createTrackbar("Increase Value", "Control", &valueUpper, 255);

    while (1)
    {
        //Load actual image
        Mat actualImage;
        bool temp = cap.read(actualImage);

        // 修复:检查帧是否读取成功
        if (!temp)
        {
            cout << "Video ended or failed to read frame" << endl;
            break;
        }

        //Store converted image
        Mat convertToHSV;
        cvtColor(actualImage, convertToHSV, COLOR_BGR2HSV);

        //Matrix for window where thermal spot will be detected
        Mat detectionScreen;
        inRange(convertToHSV, Scalar(hLowVal, hSatLow, valueLower), Scalar(hUpVal, hSatUp, valueUpper), detectionScreen);

        //remove small objects from foreground
        erode(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        dilate(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        //fill up small holes in foreground
        dilate(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        erode(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        //Show the thermal spot
        imshow("Thermal Spot detected", detectionScreen);

        // 修复:显示原始图像
        imshow("Original Image", actualImage);

        if (waitKey(30) == 27)//If escape key is pressed, break loop
        {
            break;
        }
    }
    
    return 0;
}

内容的提问来源于stack exchange,提问作者Opthomas2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 16:34:50