Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用OpenCV检测颜色的最有效方法?_C++_Visual Studio 2010_Image Processing_Opencv - Fatal编程技术网

C++ 使用OpenCV检测颜色的最有效方法?

C++ 使用OpenCV检测颜色的最有效方法?,c++,visual-studio-2010,image-processing,opencv,C++,Visual Studio 2010,Image Processing,Opencv,我使用以下方法在图像中心附近设置了一个感兴趣的区域: Mat frame; //frame has been initialized as a frame from a camera input Rect roi= cvRect(frame.cols*.45, frame.rows*.45, 10, 8); image_roi= frame(roi); //I stoped here not knowing what to do next 我正在使用相机,在任何时候当我抓取一帧时,ROI都会在

我使用以下方法在图像中心附近设置了一个感兴趣的区域:

Mat frame;
//frame has been initialized as a frame from a camera input
Rect roi= cvRect(frame.cols*.45, frame.rows*.45, 10, 8);
image_roi= frame(roi);
//I stoped here not knowing what to do next
我正在使用相机,在任何时候当我抓取一帧时,ROI都会在30%到100%之间充满我想要的颜色,在这种情况下是红色的。了解我当前帧中是否存在红色的最有效方法是什么

解决方案:

image_roi= frame(roi);// a frame from my camera as a cv::Mat
cvtColor(image_roi, image_roi, CV_BGR2HSV);
thrs= new Mat(image_roi.rows, image_roi.cols, CV_8UC1);//allocate space for new img
inRange(image_roi, Scalar(0,100,100), Scalar(12,255,255), *thrs);//do hsv thresholding for red
for(int i= 0; i < thrs->rows; i++)//sum up
{
    for(int j=0; j < thrs->cols; j++)
    {
        sum= sum+ thrs->data[(thrs->rows)* i + j];
    }
}
if(sum> 100)//my application only cares about red
    cout<<"Red"<<endl;
else
    cout<<"White"<<endl;
sum=0;
image_roi=帧(roi);//作为cv::Mat的相机帧
CVT颜色(图像roi、图像roi、CV BGR2HSV);
thrs=新垫(图像roi.rows、图像roi.cols、CV 8UC1)//为新img分配空间
范围(图像的roi,标量(0100100),标量(12255255),*thrs)//是否对红色进行hsv阈值设置
对于(int i=0;irows;i++)//求和
{
对于(int j=0;jcols;j++)
{
sum=sum+thrs->data[(thrs->rows)*i+j];
}
}
if(sum>100)//我的应用程序只关心红色

cout我假设您只想知道ROI中红色的百分比。如果这不正确,请澄清


我会扫描ROI并将每个像素转换为更好的颜色空间,以便进行颜色比较,例如YCbCr或HSV。然后,我会计算色调在红色色调增量范围内的像素数(通常在色轮上为0度)。您可能需要处理一些边缘情况,其中亮度或饱和度太低,以至于人类无法认为它们是红色,即使从技术上讲是红色,这取决于您试图实现的目标。

此解决方案不仅应解决红色问题,还应解决任何颜色分布问题:

  • 获取感兴趣区域的颜色直方图、二维色调和饱和度直方图(如下示例)
  • 使用
    calcBackProject
    。在直方图模式附近(在本例中为红色),以像素表示的颜色值会更大
  • 阈值结果以获得与分布更匹配的像素(在本例中为“最佳红色”)

  • 例如,可以使用此解决方案来获得一个简单但功能非常强大的皮肤检测器。

    TH.的答案更符合我的要求,但您的方法更易于实现。我将修改我的帖子以反映解决方案。