I am playing with some basic genetic algorithms to recreate input images by transparent plane figures (similar like here:).
Everything would work great but the speed is terrible. I was looking for a problematic part of my code and I found that function I use - ImageDifference
works very slow for CMYK colors (even if you are comparing CMYK to CMYK), but it is much faster for RGB vs RGB comparison.
The problem is that when I combine figures using Graphics
like this:
Graphics[{Opacity[0.5], Red, Disk[], Green, Disk[{1, 0}]}]
the result image is in CMYK and hence, to make ImageDifference
faster, I have to use ColorConvert
(to RGB) which again is quite slow function.
Is there another way in Mathematica to set that all images created by Graphics
will be automatically in RGB? Or if you have another ideas how to speed this up, please let me know. :)
Small demonstration:
I have some target image. For example:
target = ImageResize[ExampleData[{"TestImage", "Lena"}], 150]
In every step, I also have some combination of polygons which I want to compare to the target image. So lets say, we have (in some very early stage):
k = 5;(*number of points*)
n = 20;(*number of polygons*)
(*opacity, r, g, b, x, y*)
polygons=
Table[{RandomReal[], RandomReal[], RandomReal[], RandomReal[],
RandomReal[{0, 1}, k], RandomReal[{0, 1}, k]} // Flatten, {n}];
example1 = Graphics[
Map[
{
Opacity[#[[1]]],
RGBColor[#[[2]], #[[3]], #[[4]]],
Polygon[{Take[#, {5, 5 + k - 1}], Take[#, {5 + k, 2 k + 4}]} //Transpose]
}
&,
polygons
], ImageSize -> {150, 150}
]
For demonstration lets also set:
example2 = ColorConvert[example1, "RGB"];
Now, the difference in applying ImageData
for those two representations is quite huge:
(example1 // ImageData); // AbsoluteTiming (*-> 0.0584582*)
(example2 // ImageData); // AbsoluteTiming (*-> 0.000384866*)
Same for the ImageDifference
:
ImageDifference[target,example1]; // AbsoluteTiming (*-> 0.0703736*)
ImageDifference[target,example2]; // AbsoluteTiming (*-> 0.000687233*)
Graphics
is not anImage
and doesn't have an intrinsic colour space, unless youRasterize
it or doImage[graphics, ColorSpace -> space]
. How are you finding it to be "in CMYK"? $\endgroup$Opacity[]
directives). Explicitly-set alpha channels slow things down in Mathematica, for some reason... $\endgroup$