Opencv:将使用视频的代码更改为使用轨迹栏进行颜色检测的图像
Opencv: Changing a code that uses video to an image for color detection using trackbars
我有一个项目,我需要从树叶图像中检测特定颜色,例如绿色、棕色和黄色。
我发现这个教程 (http://opencv-srf.blogspot.com.br/2010/09/object-detection-using-color-seperation.html) 解释了如何创建实时轨迹栏以找到最佳值,但它使用来自网络摄像头的图像,我想将它与图片一起使用。
你们能帮我做一下吗?
谢谢。
这是对 HSV 图像进行阈值处理的代码,使用轨迹栏选择范围。
请注意,与视频不同(如here所述),我使用morphologyEx
进行形态学操作,并用C++函数[=13]替换了C风格cvCreateTrackbar
=].
代码中的注释要清楚。如果有什么不清楚的地方请联系我:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// Load BGR image
Mat3b bgr = imread("path_to_image");
if (bgr.empty())
{
cout << "Cannot open the image" << endl;
return -1;
}
// Transform to HSV
Mat3b hsv;
cvtColor(bgr, hsv, COLOR_BGR2HSV);
// Create a window called "Control"
namedWindow("Control", CV_WINDOW_AUTOSIZE);
// Set starting values for ranges
int iLowH = 0;
int iHighH = 179;
int iLowS = 0;
int iHighS = 255;
int iLowV = 0;
int iHighV = 255;
//Create trackbars in "Control" window
createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
createTrackbar("HighH", "Control", &iHighH, 179);
createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
createTrackbar("HighS", "Control", &iHighS, 255);
createTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
createTrackbar("HighV", "Control", &iHighV, 255);
//Show the original image
imshow("Original", bgr);
// Create kernel for morphological operation
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));
// Infinte loop, until user press "esc"
while (true)
{
Mat mask;
inRange(hsv, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), mask); //Threshold the image
//morphological opening (remove small objects from the foreground)
morphologyEx(mask, mask, MORPH_OPEN, kernel);
//morphological closing (fill small holes in the foreground)
morphologyEx(mask, mask, MORPH_CLOSE, kernel);
//Show the thresholded image
imshow("Thresholded Image", mask);
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
我有一个项目,我需要从树叶图像中检测特定颜色,例如绿色、棕色和黄色。 我发现这个教程 (http://opencv-srf.blogspot.com.br/2010/09/object-detection-using-color-seperation.html) 解释了如何创建实时轨迹栏以找到最佳值,但它使用来自网络摄像头的图像,我想将它与图片一起使用。 你们能帮我做一下吗?
谢谢。
这是对 HSV 图像进行阈值处理的代码,使用轨迹栏选择范围。
请注意,与视频不同(如here所述),我使用morphologyEx
进行形态学操作,并用C++函数[=13]替换了C风格cvCreateTrackbar
=].
代码中的注释要清楚。如果有什么不清楚的地方请联系我:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// Load BGR image
Mat3b bgr = imread("path_to_image");
if (bgr.empty())
{
cout << "Cannot open the image" << endl;
return -1;
}
// Transform to HSV
Mat3b hsv;
cvtColor(bgr, hsv, COLOR_BGR2HSV);
// Create a window called "Control"
namedWindow("Control", CV_WINDOW_AUTOSIZE);
// Set starting values for ranges
int iLowH = 0;
int iHighH = 179;
int iLowS = 0;
int iHighS = 255;
int iLowV = 0;
int iHighV = 255;
//Create trackbars in "Control" window
createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
createTrackbar("HighH", "Control", &iHighH, 179);
createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
createTrackbar("HighS", "Control", &iHighS, 255);
createTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
createTrackbar("HighV", "Control", &iHighV, 255);
//Show the original image
imshow("Original", bgr);
// Create kernel for morphological operation
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));
// Infinte loop, until user press "esc"
while (true)
{
Mat mask;
inRange(hsv, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), mask); //Threshold the image
//morphological opening (remove small objects from the foreground)
morphologyEx(mask, mask, MORPH_OPEN, kernel);
//morphological closing (fill small holes in the foreground)
morphologyEx(mask, mask, MORPH_CLOSE, kernel);
//Show the thresholded image
imshow("Thresholded Image", mask);
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}