使用 OpenCV 检测颜色的最有效方法?

发布于 2025-01-07 23:14:28 字数 984 浏览 0 评论 0原文

我使用以下方法在图像中心附近的某个位置设置了一个感兴趣的区域:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素食主义者 2025-01-14 23:14:28

我假设您只想知道投资回报率中红色的百分比。如果这不正确,请澄清。

我会扫描 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.

木森分化 2025-01-14 23:14:28

此解决方案不仅应解决红色问题,还应解决任何颜色分布

  1. 获取 ROI 的颜色直方图,这是一个二维色调和饱和度直方图(按照示例 此处)。
  2. 使用 calcBackProject投影直方图回到完整图像。您将获得更大的像素值,呈现接近直方图众数的颜色(在本例中为红色)。
  3. 对结果进行阈值设置以获得更好地匹配分布的像素(在本例中为“最佳红色”)。

例如,该解决方案可用于获得简单但功能强大的皮肤检测器

This solution should address not only red but any color distribution:

  1. Get a color histogram for your ROI, a bidimensional hue and saturation histogram (follow the example here).
  2. Use 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).
  3. Threshold the result to get the pixels that better match the distribution (in this case, the "best reds").

This solution can be used, for example, to get a simple but very functional skin detector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文