使用opencv进行颜色序列识别 [英] Color sequence recognition using opencv

查看:134
本文介绍了使用opencv进行颜色序列识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用opencv正确识别颜色的机器视觉解决方案可能是什么?

我必须检查连接器波纹管的颜色顺序是否正确. 使用颜色重调技术或图案匹配技术更好吗? 有没有更好的方法来解决这个问题?

图像波纹管中的连接器带有彩色电线,如何检查正确的电线顺序?

解决方案

我建议执行以下步骤(使用简单的代码ilustration):

  1. 转换为L a b颜色空间;

    https://en.wikipedia.org/wiki/Lab_color_space/

    cv::cvtColor(img,img,CV_BGR2Lab);

  2. 拍摄仅包含电线的子图像

    img = img(cv::Rect(x,y,width,height)); // detect wires

  1. 计算每列的平均值并获得值的一维向量

    std::vector<cv::Vec3f> aggregatedVector;
    for(int i=0;i<img.cols;i++)
    {
      cv::Vec3f sum = cv::Vec3f(0,0,0);
      for(int j=0;j<img.rows;j++)
      {
        sum[0]+= img.at<Vecb>(j,i)[0]);
        sum[1]+= img.at<Vecb>(j,i)[1];
        sum[2]+= img.at<Vecb>(j,i)[2];
      }
      sum = sum/img.rows;
      aggregatedVector.push_back(sum);
    }
    

  2. 使用例如梯度提取均匀字段并获得20的向量 值

    std::vector<Vec3f> fields
    cv::Vec3f mean = 0;
    int counter =0;
    for(int i=0;i<aggregatedVector.size();i++)
       {
         mean+= aggregatedVector[i];
         if(cv::norm(aggregatedVector[i+1] - aggregatedVector[i]) > /*thresh here */
            {
              fields.push_back(mean/(double)counter);
              mean = cv::Vec3f(0,0,0);
              counter=0;
            }
          counter++
       }
    

  3. 计算向量与参考之间的颜色距离的向量

      double totalError = 0;
      for(int i=0;i<fields.size();i++)
         {
            totalError+= cv::mean(reference[i]-fields[i]);
         }
    

    然后,您可以基于误差向量值进行决策.玩得开心!

What could be the possible machine vision solution for correct color recognition using opencv?

I must check if the color sequence of the connector bellow is correct. Is it better to use color regonition technique or pattern match technique? Is there any better approach to solve this?

In the image bellow is connector with colored wires, how to check correct sequence of wires?

解决方案

I suggest doing following steps (with simple code ilustration):

  1. converting to Lab color space;

    https://en.wikipedia.org/wiki/Lab_color_space/

    cv::cvtColor(img,img,CV_BGR2Lab);

  2. take subimage which contains only wires

    img = img(cv::Rect(x,y,width,height)); // detect wires

  1. compute mean values for each column and get 1D vector of values

    std::vector<cv::Vec3f> aggregatedVector;
    for(int i=0;i<img.cols;i++)
    {
      cv::Vec3f sum = cv::Vec3f(0,0,0);
      for(int j=0;j<img.rows;j++)
      {
        sum[0]+= img.at<Vecb>(j,i)[0]);
        sum[1]+= img.at<Vecb>(j,i)[1];
        sum[2]+= img.at<Vecb>(j,i)[2];
      }
      sum = sum/img.rows;
      aggregatedVector.push_back(sum);
    }
    

  2. extract uniform fields using, for example gradient and get vector with 20 values

    std::vector<Vec3f> fields
    cv::Vec3f mean = 0;
    int counter =0;
    for(int i=0;i<aggregatedVector.size();i++)
       {
         mean+= aggregatedVector[i];
         if(cv::norm(aggregatedVector[i+1] - aggregatedVector[i]) > /*thresh here */
            {
              fields.push_back(mean/(double)counter);
              mean = cv::Vec3f(0,0,0);
              counter=0;
            }
          counter++
       }
    

  3. compute vector of color distances between calculated vector and reference

      double totalError = 0;
      for(int i=0;i<fields.size();i++)
         {
            totalError+= cv::mean(reference[i]-fields[i]);
         }
    

    Then you can make decision based on error vector values. Have fun!

这篇关于使用opencv进行颜色序列识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆