我已经添加了两个跟踪条,它们负责更改Canny的检测边缘算法中的最低和最高阈值。而且,我希望它能够在循环中读取另一张图片并为它们调整阈值水平。
这是代码(没有边缘检测功能,它只是灰度->模糊-> Canny->模糊->绘制边缘->适合最小矩形):
struct Thresh
{
double thMin = 0;
double thMax = 255;
double resolution = 1;
int minTbV = 0;
int maxTbV = 0;
double Range()
{return thMax - thMin;}
int Ticks()
{return cvRound(Range()/resolution);};
double minValue()
{return thMin + minTbV * Range() / Ticks();}
double maxValue()
{return thMin + maxTbV * Range() / Ticks();}
//int tbValue = cvRound(Ticks() / 2.0); // start at 50%
};
void onTrackBar(int, void* userData)
{
Thresh *pdata = (Thresh*)userData;
double minTh = pdata->minValue();
double maxTh = pdata->maxValue();
std::cout << "MIN threshold:" << minTh << endl;
std::cout << "MAX threshold:" << maxTh << endl;
}
Mat open_images(int i)
{
string fullfileName;
stringstream ss;
Mat image;
ss << "C:/Users/Filip/Desktop/DATA/test" << i << ".jpg" << endl;
ss >> fullfileName;
image = cv::imread(fullfileName, 1);
return image;
}
void changing_thresh()
{
Thresh data;
static const char * winName = "MyWindow";
Mat IMAGE = cv::imread("C:/Users/Filip/Desktop/Frame.jpg", 1);
Mat img;
int i = 1;
while (1)
{
if (waitKey(30) == 27)
{
i++;
IMAGE = open_images(i);
}
cv::namedWindow(winName);
cv::createTrackbar("MinThresh", winName, &(data.minTbV),
data.Ticks(), onTrackBar, &data);
cv::createTrackbar("MaxThresh", winName, &(data.maxTbV),
data.Ticks(), onTrackBar, &data);
onTrackBar(0, &data);
img = edgedetect(IMAGE, data.minTbV, data.maxTbV);
imshow("MyWindow", img);
char c = (char)waitKey(25);
}
cv::waitKey(1);
}
int main()
{
changing_thresh();
waitKey();
}
不幸的是,我遇到了两个障碍:
1)(第57-72行),while循环是将data.minTbV / data.maxTbV的更改值传递到跟踪栏中的唯一方法吗?
2)我确实通过按ESC(第59行)来更改图片,但是由于在waitKey()内设置了时间,因此它实际上无法正常工作。有什么更好的办法吗?或者我该如何使用向右箭头前进和向左箭头前进?
谢谢!