我一直在努力获得最高的饱和度和鲜艳的色彩。为此,我有以下命令和 C 程序来获得相同的结果。
convert one.jpg -alpha off -scale '50x50!' -depth 8 \
'(' -clone 0 -colorspace HSB -channel gb -separate +channel -threshold 50% -compose multiply -composite ')'\
-alpha off -compose copy_opacity -composite sparse-color:-|\
convert -size 50x50 xc: -sparse-color voronoi @- +dither -colors 6 -depth 8 -format %c histogram:info:
执行了两个命令,所以我分开并分别制作。然后我将第一个命令的输出写入文本文件并在第二个命令中读取该文本文件。请看下面两个命令。
/*First Part*/
convert one.jpg -alpha off -scale '50x50!' -depth 8 '(' -clone 0 -colorspace HSB -channel gb -separate +channel -threshold 50% -compose multiply -composite ')' -alpha off -compose copy_opacity -composite sparse-color:-test.txt
/*Second Part*/
convert -size 50x50 xc: -sparse-color voronoi @-test.txt +dither -colors 6 -depth 8 -format %c histogram:info:
使用它我得到了相同的输出。下面是我实现相同的 C 程序。在代码中,第一部分已涵盖。但是我从第一个转换命令和 C 代码得到的结果不一样。我不明白我错过了什么。
#include "MagickWand/studio.h"
#include "MagickWand/MagickWand.h"
void dominantSix(){
/* convert one.jpg -alpha off -scale '50x50!'\
-depth 8 '(' -clone 0 -colorspace HSB -channel gb -separate +channel -threshold 50% -compose multiply -composite ')'\
-alpha off -compose copy_opacity -composite sparse-color:-test.txt */
MagickWand * wand, * wand0, * wand1;
PixelIterator * iteration;
PixelWand ** row;
PixelInfo pixel;
size_t x, y, row_width;
wand = NewMagickWand();
MagickReadImage(wand, "car.jpg");
MagickSetImageAlphaChannel(wand, OffAlphaChannel);
MagickScaleImage(wand, 50, 50);
MagickSetImageDepth(wand,8);
wand0 = CloneMagickWand(wand);
wand1 = CloneMagickWand(wand);
MagickTransformImageColorspace(wand0, HSBColorspace);
MagickSetImageChannelMask(wand0, GreenChannel);
MagickSeparateImage(wand0, GreenChannel);
MagickThresholdImage(wand0, QuantumRange*50/100);
MagickTransformImageColorspace(wand1, HSBColorspace);
MagickSetImageChannelMask(wand1, BlueChannel);
MagickSeparateImage(wand1, BlueChannel);
MagickThresholdImage(wand1, QuantumRange*50/100);
MagickCompositeImage(wand0, wand1, MultiplyCompositeOp, MagickFalse, 0, 0);
wand1 = DestroyMagickWand(wand1);
MagickSetImageAlphaChannel(wand, OffAlphaChannel);
MagickCompositeImage(wand, wand0, CopyAlphaCompositeOp, MagickFalse, 0, 0);
wand0 = DestroyMagickWand(wand0);
size_t height = MagickGetImageHeight(wand);
size_t width = MagickGetImageWidth(wand);
iteration = NewPixelIterator(wand);
FILE *fptr;
fptr = fopen("program.txt", "w");
for (y = 0; y < height; ++ y)
{
row = PixelGetNextIteratorRow(iteration, &row_width);
for (x = 0; x < row_width; ++x)
{
PixelGetMagickColor(row[x], &pixel);
size_t red = pixel.red*255/QuantumRange;
size_t green = pixel.green*255/QuantumRange;
size_t blue = pixel.blue*255/QuantumRange;
char color[30];
sprintf(color, "%zu%s%zu%s%s%zu%s%zu%s%zu%s%s", x, ",", y, ",", "srgb(", red, ",", green, ",", blue, ",", "1) ");
fprintf(fptr,"%s", color);
}
PixelSyncIterator(iteration);
}
fclose(fptr);
PixelSyncIterator(iteration);
iteration = DestroyPixelIterator(iteration);
wand = DestroyMagickWand(wand);
MagickWandTerminus();
}
int main(int argc, char const *argv[]) {
dominantSix();
return 0;
}
下面是源图像和两个文本文件的链接,一个来自 CLI,另一个来自 C。