【问题标题】:Color detection in openCV - Pixel checking issueopenCV中的颜色检测 - 像素检查问题
【发布时间】:2013-12-27 10:05:51
【问题描述】:

在 opencCV 中,我得到了这段代码来检测他们检查每个像素值的颜色,获得所需的值并将其替换为白色并将所有不必要的像素转换为黑色。这是代码。

int MaxC = 0;
for(int i = 0; i < img_output.rows; i++)
{
for(int j = 0; j < img_output.cols; j++)
{
    nPixelPos = i*img_output.cols*cn + j*cn;
    nCombinedVal = (pixelPtr[nPixelPos +2]) *( 255 - pixelPtr[nPixelPos +1]);
    if (nCombinedVal > MaxC)
        MaxC = nCombinedVal;
}
}

MaxC = MaxC / 255;
for(int i = 0; i < img_output.rows; i++)
{
for(int j = 0; j < img_output.cols; j++)
{
    nPixelPos = i*img_output.cols*cn + j*cn;
    nCombinedVal = (pixelPtr[nPixelPos +2]) *( 255 - pixelPtr[nPixelPos +1]);
    nCombinedVal = (nCombinedVal/ MaxC);;
    if (nCombinedVal > 230)
    {
        pixelPtr1[nPixelPos + 0]= nCombinedVal;
        pixelPtr1[nPixelPos+ 1] = nCombinedVal;
        pixelPtr1[nPixelPos + 2]= nCombinedVal;
    }
    else
    {
        pixelPtr1[nPixelPos + 0]= 0;
        pixelPtr1[nPixelPos+ 1] = 0;
        pixelPtr1[nPixelPos + 2]= 0;
    }
}
}

现在我的问题是:-

此代码特别适用于一种颜色(绿色)我还想让它与其他颜色(即红色)兼容..但我不知道我应该在哪里更改此代码。谁能帮我 ?

【问题讨论】:

    标签: javascript c++ objective-c opencv colors


    【解决方案1】:

    在不了解您的绿色测量值的情况下,计算测量值的行是:

    nCombinedVal = (pixelPtr[nPixelPos +2]) *( 255 - pixelPtr[nPixelPos +1]);
    

    nPixelPos 是像素的索引。如果图像遵循 OpenCV BGR 约定,+1 将为您提供绿色通道,+2 为您提供红色通道。如果遵循 RGB 约定,+1 仍为绿色,但 +2 为蓝色。

    要将检测到的颜色更改为红色,我最好的猜测是将+1 替换为+2 并将+2 更改为+1 或+0。请务必修改这两行。

    nCombinedVal = (pixelPtr[nPixelPos +1]) *( 255 - pixelPtr[nPixelPos +2]);
    

    如果您愿意替换代码,我会建议使用不同的颜色测量方法:

    greenness = green - max(red, blue)
    redness = red - max(green, blue)
    blueness = blue - max(red, green)
    

    在您的代码中看起来像这样:

    for(int i = 0; i < img_output.rows; i++)
    {
    for(int j = 0; j < img_output.cols; j++)
    {
        nPixelPos = i*img_output.cols*cn + j*cn;
        int greenness = int(pixelPtr[nPixelPos +1]) - max(pixelPtr[nPixelPos], pixelPtr[nPixelPos +2]);
        if (greeness > 10)
        {
            pixelPtr1[nPixelPos + 0] = 255;
            pixelPtr1[nPixelPos + 1] = 255;
            pixelPtr1[nPixelPos + 2] = 255;
        }
        else
        {
            pixelPtr1[nPixelPos + 0] = 0;
            pixelPtr1[nPixelPos + 1] = 0;
            pixelPtr1[nPixelPos + 2] = 0;
        }
    }
    }
    

    编辑: OpenCV 以相反的顺序存储 HSV 颜色信息,并将色调除以 2 以适应一个字节(更多详细信息 here)。因此 pixelPtr[nPixelPos +2] 是色调,而 pixelPtr[nPixelPos +1] 是饱和度。要更改您要查找的颜色,请将行替换为以下内容:

    nCombinedVal = abs(pixelPtr[nPixelPos +2]-target_hue) *( 255 - pixelPtr[nPixelPos +1]);
    

    其中 target_hue 是您想要找到的色调除以 2。

    【讨论】:

    • HI 你能告诉我OpenCV HSV约定吗?因为我在这里使用 HSV 查找颜色。
    【解决方案2】:

    您好,您可以参考下面的代码,该代码将使用鼠标访问像素值并显示结果

    #include <iostream>
    #include <stdio.h>
    #include <opencv2/opencv.hpp>
    
    using namespace cv;
    using namespace std;
     Mat image, src;
     char window_name[20]="Pixel Value Demo";
    
    void onMouse( int event, int x, int y, int f, void* ){
     image=src.clone();
     Vec3b pix=image.at<Vec3b>(y,x);
     int B=pix.val[0];
     int G=pix.val[1];
     int R=pix.val[2];
    
    
     char name[30];
        sprintf(name,"R=%d",R);
        putText(image,name, Point(10,130) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
    
        sprintf(name,"G=%d",G);
        putText(image,name, Point(10,170) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
    
        sprintf(name,"B=%d",B);
        putText(image,name, Point(10,210) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
    
        sprintf(name,"X=%d",x);
        putText(image,name, Point(10,300) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
    
        sprintf(name,"Y=%d",y);
        putText(image,name, Point(10,340) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
     imshow( window_name, image );
    }
    
    
    
    int main( int argc, char** argv )
    {
      namedWindow( window_name, CV_WINDOW_AUTOSIZE );
    
      src = imread( "ball.jpg");
      imshow( window_name, src );
    
      setMouseCallback( window_name, onMouse, 0 );
    
      waitKey(0);
    
      return 0;
    }
    

    【讨论】:

    • 谢谢@haris .. 但我只需要更正上面的代码 .. 因为我的所有代码都基于上面的代码.. 该代码工作正常.. 但仅适用于一种绿色..所以请你帮帮我..顺便说一句,我在openCV论坛上阅读了你这么多帖子..他们帮了我很多..谢谢伙计:)
    猜你喜欢
    • 2016-05-28
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 2011-06-04
    • 2014-03-15
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多