给定BGR值,是否有公式来确定总体颜色? (OpenCV和C ++) [英] Is there a formula to determine overall color given BGR values? (OpenCV and C++)

查看:290
本文介绍了给定BGR值,是否有公式来确定总体颜色? (OpenCV和C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++和OpenCV来创建一个函数,它将检测图像中像素的颜色,确定其中的颜色范围,并将其替换为一般颜色。例如,绿色可以从深绿色到浅绿色,程序将确定它仍然是绿色的,并用简单的绿色代替它,使得输出图像看起来很简单。一切都设置了,但我有麻烦定义每个范围的特征,并且好奇,如果任何人知道或公式,给定BGR值,可以确定像素的整体颜色。如果不是,我必须做许多实验,并使自己,但如果已经存在,可以节省时间的东西。我已经做了大量的研究,迄今还没有找到任何东西。

解决方案

如果你想让你的图像更简单(即颜色较少),但看起来很好,您有几个选项:




  • 方法是用图像中的因子 N 除(整除),然后乘以因子 N


  • 或者您可以将图片分为 K 诸如 kmeans 的聚类算法,或中值剪切算法。




原始图片:





代码量化:

  #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;
}

代码kmeans:

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

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

imshow(Original,img);

//集群

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

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

for(int i = 0; i {
data.at< float>(i,0)= colors ],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;
}


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.

解决方案

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:

Reduced colors (quantized, N = 64):

Reduced colors (clustered, K = 8):

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;
}

这篇关于给定BGR值,是否有公式来确定总体颜色? (OpenCV和C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆