我正在尝试在 Visual Studio 中创建一个程序来检测黑白视频剪辑中的颜色。这是我的代码:
#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;
}
问题出在这行代码:
createTrackbar("Lower Hue", "Adjust", &hLowVal, 179);
当我尝试运行它时,它出现了以下错误消息:
错误:0x00007FFD5B4E4C3C 处出现未处理的异常 ThermalSpotDetector.exe:Microsoft C++ 异常:cv::Exception at 内存位置 0x000000BA269DD5C0
有人知道发生了什么事吗?由于时间紧迫,您能具体告诉我如何解决吗?
正如您在
cv:createTrackbar
文档中看到的,winname
是:
将用作父窗口的窗口的名称 创建了轨迹栏。
当您调用
namedWindow
时,您使用了名称 "Control"
(调用中的第一个参数),这应该是窗口名称。
因此您应该将第一个
createTrackbar
呼叫更改为:
//--------------------------vvvvvvvv----------------
createTrackbar("Lower Hue", "Control", &hLowVal, 179);
所有其他
createTrackbar
呼叫也是如此。