【发布时间】:2016-05-30 11:51:02
【问题描述】:
我想提取图像的调色板,类似于此(来自here):
我需要它来提取特定颜色,例如黄色、绿色和棕色,并显示该颜色覆盖的区域的百分比。另外,我可以添加更多颜色来提取。
如何减少原始图像中的颜色数量,如何获得调色板?
【问题讨论】:
-
“黄色”不是特定颜色。
#7F1D33是特定的,而且很少见。
标签: c++ opencv visual-c++ opencv3.0
我想提取图像的调色板,类似于此(来自here):
我需要它来提取特定颜色,例如黄色、绿色和棕色,并显示该颜色覆盖的区域的百分比。另外,我可以添加更多颜色来提取。
如何减少原始图像中的颜色数量,如何获得调色板?
【问题讨论】:
#7F1D33 是特定的,而且很少见。
标签: c++ opencv visual-c++ opencv3.0
这里发生了三种不同的事情。
减少颜色数量
有许多技术可以减少颜色的数量。 Here你可以看到如何使用颜色量化和kmeans。
另一种方法可以使用median cut 算法(此处未显示)。
OpenCV 提供Non-Photorealistic Rendering module。 Here你可以看一些如何使用它的例子。
获取图像的不同颜色
这很容易。只需遍历整个图像。如果你看到一种新颜色,存储它的值,计数器等于 1。如果你看到已经看到的颜色,增加它的计数器。 std::map 在这里可能很有用。
获取颜色名称
我不会在这里展示它。但是网上有some useful resources。您需要所有命名颜色的列表。请记住,并非每种颜色都有名称。事实上,RGB 值的所有可能颜色都是256*256*256。因此,在您的列表中找到最接近的颜色,并将其名称分配给您当前的颜色。
例如,对于这个输入图像,
使用 kmeans 方法,我得到 reduce color 图像:
它的调色板是:
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%
您现在可以在列表中搜索最接近的颜色名称,您将获得所需的颜色。如何组成 GUI 以显示这些信息取决于您:数据都在那里。
代码:
#include <opencv2\opencv.hpp>
#include <opencv2\photo.hpp>
#include <iostream>
#include <map>
using namespace cv;
using namespace std;
// https://stackoverflow.com/a/34734939/5008845
void reduceColor_Quantization(const Mat3b& src, Mat3b& dst)
{
uchar N = 64;
dst = src / N;
dst *= N;
}
// https://stackoverflow.com/a/34734939/5008845
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;
}
【讨论】:
the specified comparator type does not provide a const call operator i.imgur.com/JBzAyhJ.png
const 然后:bool operator()(const Vec3b& lhs, const Vec3b& rhs) const { ...