Mahagney Saleh
Mahagney Saleh

Reputation: 117

Detect areas corresponding to RGB

I have to create a program that takes a video as input and after processing it has to be composed just from BGR colors. Were red color is predominant it has to be (0,0,255),where blue is predominate (255,0,0) and for green (0,255,0).Every area where a color is predominant should have that color, not every pixel. I found something but it works only for one color.

Mat redFilter(const Mat& src)
{
Mat redOnly;
    inRange(src, Scalar(0, 0, 1), Scalar(100, 100, 255), redOnly);
}

Can you give me some ideas for this project?

Upvotes: 0

Views: 96

Answers (2)

Utkarsh Sinha
Utkarsh Sinha

Reputation: 3305

Here's something that you might be looking for. http://aishack.in/tutorials/tracking-colored-objects-opencv/

This article talks about segmenting objects of a specific color. You can use the same approach and identify images in red, green and blue. The article goes a step further and figures out "where" the object is as well.

Upvotes: 0

Miki
Miki

Reputation: 41775

You can set the pure blue, red or green color according to the highest among B,G,R values for each pixel.

Image:

enter image description here

Result:

enter image description here

Code:

#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
    // Load image
    Mat3b img = imread("D:\\SO\\img\\barns.jpg", IMREAD_COLOR);

    // Result image with only pure B,G,R values
    Mat3b bgr(img.size());

    for (int r = 0; r < img.rows; ++r)
    {
        for (int c = 0; c < img.cols; ++c)
        {
            // Take highest among B,G,R
            Vec3b v = img(r,c);
            if (v[0] > v[1] && v[0] > v[2])
            {
                bgr(r, c) = Vec3b(255, 0, 0);
            } 
            else if (v[1] > v[0] && v[1] > v[2])
            {
                bgr(r, c) = Vec3b(0, 255, 0);
            }
            else 
            {
                bgr(r, c) = Vec3b(0, 0, 255);
            }
        }
    }

    imshow("Image", img);
    imshow("Result", bgr);
    waitKey();


    return 0;
}

Upvotes: 2

Related Questions