原图效果: 更改颜色后效果: 直接上源码:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace cv;
using namespace std;
int main(int argc, char const* argv[])
{
Mat srcImg = imread("E:/img/kai.png", IMREAD_UNCHANGED);
Mat srcImg1 = imread("E:/img/kai.png", 0);
Mat srcImg2 = imread("E:/img/kai.png", 1);
cout << srcImg.channels() << endl;
cout << srcImg1.channels() << endl;
cout << srcImg2.channels() << endl;
imshow("-1", srcImg);
imshow("0", srcImg1);
imshow("1", srcImg2);
vector<Vec3d> colors;
unsigned long index = 0;
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 6; ++j) {
for (int k = 0; k < 6; ++k) {
colors.push_back(Vec3d());
colors[index][0] = i / 5.0 * 255;
colors[index][1] = j / 5.0 * 255;
colors[index][2] = k / 5.0 * 255;
index++;
}
}
}
index = 0;
Mat temp = srcImg.clone();
while (index < colors.size()) {
for (int r = 0; r < srcImg.rows; ++r) {
for (int c = 0; c < srcImg.cols; ++c) {
Vec4b &pixel_temp = temp.at<Vec4b>(r, c);
if (pixel_temp[3] == 0) {
continue;
} else {
for (int i = 0; i < 3; ++i) {
pixel_temp[i] = colors[index][i];
}
}
}
}
char outImagePath[64] = {};
sprintf_s(outImagePath, "E:/img/outImagePath/out_img_%.0f_%.0f_%.0f.png", colors[index][0], colors[index][1], colors[index][2]);
imwrite(outImagePath, temp);
index++;
}
return 0;
}
|