Is there a formula to determine overall color given BGR values? (OpenCV and C++)
Asked Answered
V

3

10

I am making a function using C++ and OpenCV that will detect the color of a pixel in an image, determine what color range it is in, and replace it with a generic color. For example, green could range from dark green to light green, the program would determine that its still green and replace it with a simple green, making the output image very simple looking. everything is set up but I'm having trouble defining the characteristics of each range and was curious if anyone knows or a formula that, given BGR values, could determine the overall color of a pixel. If not I'll have to do much experimentation and make it myself, but if something already exists that'd save time. I've done plenty of research and haven't found anything so far.

Ventriloquize answered 12/1, 2016 at 1:57 Comment(3)
'overall color of a pixel' is somewhat vague -- is there a fixed number of colors you want to map all colors into? You could divide each color space into k distinct values to create a small space you can label. int three_reds = int(floor(R / 3)); ... You could then just map each range to a single color from the RGB space.Schuyler
While there are thousands of ways to do this, some much better than others, I believe you should have been able to come up with at least a basic implementation once you've seen the idea of a color cube. The 8 corner colors are black&white, pure RGB and the complementary colors. (so all 6 rainbow colors). Just picking the closest corner would have given you something to show.Paisa
There are several. Look up "color quantization".Sestos
C
22

If you want to make your image simpler (i.e. with less colors), but good looking, you have a few options:

  • A simple approach would be to divide (integer division) by a factor N the image, and then multiply by a factor N.

  • Or you can divide your image into K colors, using some clustering algorithm such as kmeans showed here, or median-cut algorithm.

Original image:

enter image description here

Reduced colors (quantized, N = 64):

enter image description here

Reduced colors (clustered, K = 8):

enter image description here

Code Quantization:

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

int main()
{
    Mat3b img = imread("path_to_image");

    imshow("Original", img);

    uchar N = 64;
    img  /= N;
    img  *= N;

    imshow("Reduced", img);
    waitKey();

    return 0;
}

Code kmeans:

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

int main()
{
    Mat3b img = imread("path_to_image");

    imshow("Original", img);

    // Cluster

    int K = 8;
    int n = img.rows * img.cols;
    Mat data = img.reshape(1, n);
    data.convertTo(data, CV_32F);

    vector<int> labels;
    Mat1f colors;
    kmeans(data, K, labels, cv::TermCriteria(), 1, cv::KMEANS_PP_CENTERS, colors);

    for (int i = 0; i < n; ++i)
    {
        data.at<float>(i, 0) = colors(labels[i], 0);
        data.at<float>(i, 1) = colors(labels[i], 1);
        data.at<float>(i, 2) = colors(labels[i], 2);
    }

    Mat reduced = data.reshape(3, img.rows);
    reduced.convertTo(reduced, CV_8U);


    imshow("Reduced", reduced);
    waitKey();

    return 0;
}
Collaborate answered 12/1, 2016 at 3:9 Comment(0)
V
5

Yes, what you probably mean by "Overall color of a pixel" is either the "Hue" or "Saturation" of the color.

So you want a formula that transform RGB to HSV (Hue, Saturation, Value), and then you would only be interested by the Hue or Saturation values.

See: Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both

EDIT: You might need to max out the saturation, and then convert it back to RGB, and inspect which value is the highest (for instance (255,0,0), or (255,0,255), etc.

Viminal answered 12/1, 2016 at 2:14 Comment(0)
D
-2

If you want to access RGB value of all pixels , then below is code,

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

int main()
{
   Mat image = imread("image_path");

   for(int row = 1; row < image.rows; row++)
   {
       for(int col = 1; col < image.cols; col++)
       {
           Vec3b rgb = image.at<Vec3b>(row, col);
       }
   }

}
Danelledanete answered 12/1, 2016 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.