Hello,
I am trying to create different versions of an image, each one with a particular set of pixels blackened out. In the following code I have my lightSourceNumber set to 2, and I want to obtain 2^2 images: the original one, one that has pixels of set 1 blackned out, one that has pixels of set 2 blackned out and one that has all pixels of those 2 sets blackned out. These sets are subsets of the complete set of pixels and their cardinality is relatively low with respect to the superset's.
I begin with this:
```
MagickImage[] result = new MagickImage[numberOfEnvMaps];
for(int i = 0; i < result.Length; i++)
{
result[i] = new MagickImage(original);
}
```
for initialization, then I compute the sets I want and store them in a array with components (x,y,L), each component is in [0,1] range.
To black out pixels I use this for loop (note: I am using Accord.MachineLearning library for k-means):
```
KMeansClusterCollection lightClusters = kmeansEuclidean.Clusters;
for (int i = 0; i < result.Length; i++)
{
Console.WriteLine("Image" + i+".");
for (int j = 0; j < lightSourceNumber; j++)
{
int num = i & (int)Math.Pow(2, j);
if (num > 0) //if j-th bit is set to 1 i black-out pixels of that set
{
Console.WriteLine("Delete pixels set " + j + " from Image " + i+".");
ObscurePixels(ref result[i], ref brightestPixelsSet, lightClusters, j);
}
}
result[i].Write(@"../../OccludedImages/EnvMap_" + i + ".jpg");
}
```
And ObscurePixels is:
```
protected void ObscurePixels(ref MagickImage image, ref double[][] pixelSet, KMeansClusterCollection clusters, int index)
{
float[] black = { 0f, 0f, 0f }; //image has 3 channels
PixelCollection pixelsOfTheImage = image.GetPixels();
int count = 0;
foreach(double[] pixel in pixelSet)
{
if (clusters.Nearest(pixel) == index)
{
count++;
int x = (int)Math.Round(pixel[X_COMP] * image.Width);
int y = (int)Math.Round(pixel[Y_COMP] * image.Height);
pixelsOfTheImage.Set(x,y,black);
}
}
Console.WriteLine("Blackned " + count + " pixels of "+image.Width*image.Height + " belonging to pixels' set " + index + ".");
}
```
I get correct results for the first and the last image (the one without modifications and the one with pixels of all subsets, which are not all the pixels of the image, blackned out), while I get completely black images for those one I try to change only pixels from one subset. What freaks me the most is that if there was a bug in my code, shouldn't it happen for the last image aswell?
I am trying to create different versions of an image, each one with a particular set of pixels blackened out. In the following code I have my lightSourceNumber set to 2, and I want to obtain 2^2 images: the original one, one that has pixels of set 1 blackned out, one that has pixels of set 2 blackned out and one that has all pixels of those 2 sets blackned out. These sets are subsets of the complete set of pixels and their cardinality is relatively low with respect to the superset's.
I begin with this:
```
MagickImage[] result = new MagickImage[numberOfEnvMaps];
for(int i = 0; i < result.Length; i++)
{
result[i] = new MagickImage(original);
}
```
for initialization, then I compute the sets I want and store them in a array with components (x,y,L), each component is in [0,1] range.
To black out pixels I use this for loop (note: I am using Accord.MachineLearning library for k-means):
```
KMeansClusterCollection lightClusters = kmeansEuclidean.Clusters;
for (int i = 0; i < result.Length; i++)
{
Console.WriteLine("Image" + i+".");
for (int j = 0; j < lightSourceNumber; j++)
{
int num = i & (int)Math.Pow(2, j);
if (num > 0) //if j-th bit is set to 1 i black-out pixels of that set
{
Console.WriteLine("Delete pixels set " + j + " from Image " + i+".");
ObscurePixels(ref result[i], ref brightestPixelsSet, lightClusters, j);
}
}
result[i].Write(@"../../OccludedImages/EnvMap_" + i + ".jpg");
}
```
And ObscurePixels is:
```
protected void ObscurePixels(ref MagickImage image, ref double[][] pixelSet, KMeansClusterCollection clusters, int index)
{
float[] black = { 0f, 0f, 0f }; //image has 3 channels
PixelCollection pixelsOfTheImage = image.GetPixels();
int count = 0;
foreach(double[] pixel in pixelSet)
{
if (clusters.Nearest(pixel) == index)
{
count++;
int x = (int)Math.Round(pixel[X_COMP] * image.Width);
int y = (int)Math.Round(pixel[Y_COMP] * image.Height);
pixelsOfTheImage.Set(x,y,black);
}
}
Console.WriteLine("Blackned " + count + " pixels of "+image.Width*image.Height + " belonging to pixels' set " + index + ".");
}
```
I get correct results for the first and the last image (the one without modifications and the one with pixels of all subsets, which are not all the pixels of the image, blackned out), while I get completely black images for those one I try to change only pixels from one subset. What freaks me the most is that if there was a bug in my code, shouldn't it happen for the last image aswell?