简体   繁体   English

使用opencv识别颜色序列

[英]Color sequence recognition using opencv

What could be the possible machine vision solution for correct color recognition using opencv? 使用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): 我建议执行以下步骤(使用简单的代码ilustration):

  1. converting to L a b color space; 转换为L a b颜色空间;

    https://en.wikipedia.org/wiki/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 使用例如梯度提取均匀字段并获取具有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. 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! 玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM