使用 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 将在 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;
I setup an area of interest somewhere near the center of my image using:
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
I'm using a camera and at any time when I grab a frame, the ROI will be anywhere between 30% to 100% filled with my desired color, which is Red in this case. What is the most efficient method to know if Red is present in my current frame?
Solution:
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您只想知道投资回报率中红色的百分比。如果这不正确,请澄清。
我会扫描 ROI 并将每个像素转换为更好的颜色空间以进行颜色比较,例如 YCbCr 或 HSV。然后我会计算色调在红色色调的某个增量(通常是色轮上的 0 度)内的像素数。您可能需要处理一些边缘情况,其中亮度或饱和度太低,以至于人们认为它们是红色的,即使从技术上讲它们是红色的,这取决于您想要实现的目标。
I'm assuming you just want to know the percentage of red in the ROI. If that's not correct, please clarify.
I'd scan the ROI and convert each pixel into a better color space for color comparison, such as YCbCr, or HSV. I'd then count the number of pixels where the hue is within some delta of red's hue (usually 0 degrees on the color wheel). You might need to deal with some edge cases where the brightness or saturation are too low for a human to think they're red, even though technically they are, depending on what you're trying to achieve.
此解决方案不仅应解决红色问题,还应解决任何颜色分布:
calcBackProject
来投影直方图回到完整图像。您将获得更大的像素值,呈现接近直方图众数的颜色(在本例中为红色)。例如,该解决方案可用于获得简单但功能强大的皮肤检测器。
This solution should address not only red but any color distribution:
calcBackProject
to project the histogram back in the full image. You will get larger values in pixels presenting a color near the modes of the histogram (in this case, reds).This solution can be used, for example, to get a simple but very functional skin detector.