我想提取一张图片的调色板,类似于这个例子(来自这里):
我需要它提取特定的颜色,比如黄色、绿色和棕色,并显示该颜色覆盖区域的百分比。此外,我可以添加更多要提取的颜色。如何减少原始图像中的颜色数量,以及如何获取颜色调色板?
我想提取一张图片的调色板,类似于这个例子(来自这里):
我需要它提取特定的颜色,比如黄色、绿色和棕色,并显示该颜色覆盖区域的百分比。此外,我可以添加更多要提取的颜色。这里有三个不同的事情正在进行。
减少颜色数量
有许多技术可以减少颜色数量。 在这里 您可以看到如何使用颜色量化和kmeans。
另一种方法可以使用中位数剪切算法(此处未显示)。
OpenCV提供了非真实感渲染模块。 在这里 您可以查看如何使用它的一些示例。
获取图像的不同颜色
这很容易。只需遍历整个图像。如果你看到一个新的颜色,存储它的值,并将计数器设置为1。如果你看到一个已经存在的颜色,则增加它的计数器。在这里,std::map
可能会有用。
获取颜色名称
我不会在这里展示它。但是在网上有一些有用的资源。你需要一个所有命名颜色的列表。请记住,并非每种颜色都有名称。事实上,RGB 值的所有可能颜色都将是 256*256*256
。因此,在你的列表中找到最接近的颜色,并将其名称分配给当前颜色。
它的调色板是:
Color: [14, 134, 225] - Area: 5.28457%
Color: [16, 172, 251] - Area: 27.3851%
Color: [22, 68, 101] - Area: 3.41029%
Color: [28, 154, 161] - Area: 3.89029%
Color: [40, 191, 252] - Area: 22.3429%
Color: [87, 204, 251] - Area: 8.704%
Color: [161, 222, 251] - Area: 3.47429%
Color: [253, 255, 255] - Area: 25.5086%
#include <opencv2\opencv.hpp>
#include <opencv2\photo.hpp>
#include <iostream>
#include <map>
using namespace cv;
using namespace std;
// https://dev59.com/JlsW5IYBdhLWcg3wqYp7#34734939
void reduceColor_Quantization(const Mat3b& src, Mat3b& dst)
{
uchar N = 64;
dst = src / N;
dst *= N;
}
// https://dev59.com/JlsW5IYBdhLWcg3wqYp7#34734939
void reduceColor_kmeans(const Mat3b& src, Mat3b& dst)
{
int K = 8;
int n = src.rows * src.cols;
Mat data = src.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, src.rows);
reduced.convertTo(dst, CV_8U);
}
void reduceColor_Stylization(const Mat3b& src, Mat3b& dst)
{
stylization(src, dst);
}
void reduceColor_EdgePreserving(const Mat3b& src, Mat3b& dst)
{
edgePreservingFilter(src, dst);
}
struct lessVec3b
{
bool operator()(const Vec3b& lhs, const Vec3b& rhs) const {
return (lhs[0] != rhs[0]) ? (lhs[0] < rhs[0]) : ((lhs[1] != rhs[1]) ? (lhs[1] < rhs[1]) : (lhs[2] < rhs[2]));
}
};
map<Vec3b, int, lessVec3b> getPalette(const Mat3b& src)
{
map<Vec3b, int, lessVec3b> palette;
for (int r = 0; r < src.rows; ++r)
{
for (int c = 0; c < src.cols; ++c)
{
Vec3b color = src(r, c);
if (palette.count(color) == 0)
{
palette[color] = 1;
}
else
{
palette[color] = palette[color] + 1;
}
}
}
return palette;
}
int main()
{
Mat3b img = imread("path_to_image");
// Reduce color
Mat3b reduced;
//reduceColor_Quantization(img, reduced);
reduceColor_kmeans(img, reduced);
//reduceColor_Stylization(img, reduced);
//reduceColor_EdgePreserving(img, reduced);
// Get palette
map<Vec3b, int, lessVec3b> palette = getPalette(reduced);
// Print palette
int area = img.rows * img.cols;
for (auto color : palette)
{
cout << "Color: " << color.first << " \t - Area: " << 100.f * float(color.second) / float(area) << "%" << endl;
}
return 0;
}
指定的比较器类型没有提供const调用运算符
。https://i.imgur.com/JBzAyhJ.png - user969068const
,然后:bool operator()(const Vec3b& lhs, const Vec3b& rhs) const { ...
- Miki
#7F1D33
是具体且罕见的一种颜色。 - MSalters