c++ opencv 使用hsv图消除绿色
hsv图颜色范围
代码实现
int hmin = 35, smin = 43, vmin = 46;
int hmax = 77, smax = 255, vmax = 255;
cv::Mat srcMat;
//回调函数
void text(int, void *)
{
// 绿色阈值范围 cv::Scalar(35, 43, 46), cv::Scalar(77, 255, 255)
cv::Mat outMat;
cv::inRange(srcMat, cv::Scalar(hmin, smin, vmin), cv::Scalar(hmax, smax, vmax), outMat);
cv::Mat tempMat = srcMat.clone();
int rows = srcMat.rows;
int cols = srcMat.cols;
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
if (outMat.at<uchar>(row, col) == 255)
{
tempMat.at<Vec3b>(row, col)[0] = 255;
tempMat.at<Vec3b>(row, col)[1] = 255;
tempMat.at<Vec3b>(row, col)[2] = 255;
}
}
}
cv::imshow("show out", tempMat);
}
int main(int argc, char **argv)
{
srcMat = cv::imread(argv[1], cv::IMREAD_COLOR);
cv::cvtColor(srcMat, srcMat, cv::COLOR_BGR2HSV);
//新建一个窗口
namedWindow("show out", WINDOW_AUTOSIZE);
namedWindow("Trackbars", WINDOW_AUTOSIZE);
createTrackbar("Hue Min", "Trackbars", &hmin, 255, text);
createTrackbar("Hue Max", "Trackbars", &hmax, 255, text);
createTrackbar("Sat Min", "Trackbars", &smin, 255, text);
createTrackbar("Sat Max", "Trackbars", &smax, 255, text);
createTrackbar("Val Min", "Trackbars", &vmin, 255, text);
createTrackbar("Val Max", "Trackbars", &vmax, 255, text);
text(0, 0);
waitKey(0);
return 0;
}