使用opencv进行颜色序列识别 [英] Color sequence recognition using opencv
问题描述
使用opencv正确识别颜色的机器视觉解决方案可能是什么?
我必须检查连接器波纹管的颜色顺序是否正确. 使用颜色重调技术或图案匹配技术更好吗? 有没有更好的方法来解决这个问题?
图像波纹管中的连接器带有彩色电线,如何检查正确的电线顺序?
我建议执行以下步骤(使用简单的代码ilustration):
-
转换为L a b颜色空间;
https://en.wikipedia.org/wiki/Lab_color_space/ >
cv::cvtColor(img,img,CV_BGR2Lab);
-
拍摄仅包含电线的子图像
img = img(cv::Rect(x,y,width,height)); // detect wires
-
计算每列的平均值并获得值的一维向量
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); }
-
使用例如梯度提取均匀字段并获得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++ }
-
计算向量与参考之间的颜色距离的向量
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):
converting to Lab color space;
https://en.wikipedia.org/wiki/Lab_color_space/
cv::cvtColor(img,img,CV_BGR2Lab);
take subimage which contains only wires
img = img(cv::Rect(x,y,width,height)); // detect wires
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); }
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++ }
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屋!