I have been working to get most high saturation and bright colors. For that I have below command and the C program to get same result.
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:
There are two commands executed so I divided and make it separately. Then I wrote the output of first command into text file and read that text file in second command. Please look at below two command.
/*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:
Using this I got the same output. Below is my C program to achieve the same. In the code the first part has been covered. But the results I got from first convert command and C code are not same. I don't understand what I missed.
#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;
}
Below is the source Image and the links of two text files one is from CLI and other from C.
Edit for eliminating
program.txt
stepIf you're already writing c, you may be able to skip the sparse-color extraction, and apply the colors directly to
MagickSparseColorImage
Original Answer
Hard to tell where/what you need help with. But to generate the
program.txt
data, you should be able to leverage theSPARES-COLOR:
protocol.Or even simpler