c# getPixel not sellecting all pixels - c#

I have a problem with getting the pixels from an image. I load a image, select a pixel from the image and retrieve it's color and then i generate a matrix indexMatrix[bitmap_height][bitmap_width] which contains 1 or 0 depending if the [x,y] color of the bitmap is the same as the color selected. The problem is that the program doesn't select all the pixels although it should. It only retrieves a part of them ( i am sure the pixels 'forgotten' are the same color as the selected color )
The wierd thing is that if i run my program for the new image ( the one constructed from the matrix ) it returns the same image ( as it should ) but i can't figure out how to fix the problem.
Please Help!!!
Regards,
Alex Badescu
and some code from my project :
bitmap declaration:
m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
Here i calculate the matrix:
int bitmapWidth = m_Bitmap.Width;
int bitmapHeight = m_Bitmap.Height;
indexMatrix = new int[bitmapHeight][];
if (imageIsLoaded && colorIsSelected)
{
for (int i = 0; i < bitmapHeight; i++)
{
indexMatrix[i] = new int[bitmapWidth];
for (int j = 0; j < bitmapWidth; j++)
{
Color temp = m_Bitmap.GetPixel(j, i);
if (temp == selectedColor)
indexMatrix[i][j] = 1;
else indexMatrix[i][j] = 0;
}
}
MessageBox.Show("matrix generated succesfully");
}
matrixIsCalculated = true;
}

There is no obvious failure mode here. Other than that the pixel isn't actually a match with the color. Being off by, say, only one in the Color.B value for example. You cannot see this with the unaided eye.
These kind of very subtle color changes are quite common when the image was resized. An interpolation filter alters the color subtly, even if not strictly needed. Another failure mode is using a compressed image format like JPEG, the compression algorithm changes colors.

Related

Aiming for a more accurate and precise color replacer

What I'm trying to achieve:
In my program, I want to let the user enter any image, then my program resizes it then it gets all of the pixels and checks whether their color matches the color of the pixel at (0, 0). If it does match that pixel then it's going to be replaced with a hardcoded color.
The issue I'm having
My code works when I try to resize and replace the pixel colors with new ones, however when replacing the pixel colors, it usually does not replace some of the pixels, especially those that are closest to the image itself where the colors begin to diverse.
What I tried to fix the problem
I tried declaring the HEX colors as a string and whenever the C letter came, I would tell the program to remove the pixel that string. However, I found that to be very inefficient due to how often it can replace pixels that shouldn't be replaced.
My code
public class PicGen
{
public PicGen(PictureBox pictureBox)
{
Bitmap picBitmap = new(pictureBox.Image);
Bitmap resized = new(picBitmap, new(52, 52));
Color backColorBottom = resized.GetPixel(51, 0);
for (int i = 0; i < resized.Width; i++)
{
for (int j = 0; j < resized.Height; j++)
{
if (resized.GetPixel(i, j) == backColorBottom)
resized.SetPixel(i, j, Color.FromArgb(54, 57, 63));
}
}
Clipboard.SetImage(resized);
}
Example: 1 Notice how the white around the image isn't replaced like the rest of the background? ||
Original 2

Find / replace colors in (i.e. recolor) a picture

I am trying to recreate the Recolor Picture dialog that Microsoft unfortunately discontinued in the transition from Office 2003 to 2007. This was very useful for replacing colors in a picture (see http://www.indezine.com/products/powerpoint/learn/picturesandvisuals/recolor-pictures-ppt2003.html for full description of dialog).
I am mostly interested in doing this for images in the metafile format (EMF or WMF), which tend to have fewer colors than other picture formats, in my experience. The picture below is an example of an enhanced metafile picture pasted from Excel into PowerPoint that appears to contain just 6 colors:
If I was able to use the legacy Office dialog pictured above, I would see my 6 colors on the left in the "Original" column, and I could easily change the blue font (and border) color to black. The problem here is that if I use GetPixel() to programmatically inventory the colors in the image, I get dozens of colors due to anti-aliasing of the fonts, and it isn't practical to show the user all these recoloring options (which would effectively require the user to manually recreate the proper anti-aliasing effect). The snippet of code below illustrates how I have tried to inventory the colors:
Dim listColors as New List(Of Color)
Dim shp as PowerPoint.Shape = [a metafile picture in PowerPoint]
Dim strTemp as String = Path.Combine(Environ("temp"), "temp_img.emf")
shp.Export(strTemp, PowerPoint.PpShapeFormat.ppShapeFormatEMF, 0, 0)
Using bmp As New Bitmap(strTemp)
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
listColors.Add(bmp.GetPixel(x, y))
Next
Next
End Using
I see that there is an optional Palette property for metafiles, which I thought could provide an answer, but an exception is thrown when I try to access it, so that was a dead end. I also see that there are headers for metafile images, but I cannot decipher what to do with them from the limited documentation on the Internet, and I am not even sure that that would get me to the right answer (i.e. 6 colors).
To summarize, part 1 of the question is how to inventory (i.e. identify) the 6 "core" colors in the image above, and part 2 is how to replace one of these 6 colors with another. VB.NET solutions are preferred, although I can probably translate C# code if not too complex.
If needed, you can download the EMF version of the image above at https://www.dropbox.com/s/n03ys3dh9pcd0xu/temp_img.emf?dl=0.
EDIT: To be clear, I am not interested in "computing" the six "core" colors in the image above. I believe, perhaps incorrectly, that these six colors are explicit properties of the image, and my first objective is to figure out how to access them. Indeed, if you simply ungroup the metafile picture twice in PowerPoint, you can loop through the resulting shapes to get these six colors. That would address part 1 of the question, although it seems a bit sloppy, works only for metafiles (which may be fine, actually), and I doubt that is how the legacy Recolor Picture dialog worked. To address part 2, I could regroup the metafile picture shapes after swapping colors but, again, that seems sloppy and modifies the picture in ways other than what is intended. So, how can I explicitly retrieve / modify / set these "core" colors in a [metafile] picture?
So I've had a quick stab at implementing one of my ideas to use a dictionary of colours and what not. The code isn't working properly just yet but I figured I show it here so that you can have a quick look into how it works and develop on it from there.
using (Bitmap bitmap = new Bitmap(#"InputPath"))
{
Dictionary<Color, List<Color>> colourDictionary = new Dictionary<Color, List<Color>>();
int nTolerance = 60;
int nBytesPerPixel = Bitmap.GetPixelFormatSize(bitmap.PixelFormat) / 8;
System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
try
{
int nByteCount = bitmapData.Stride * bitmap.Height;
byte[] _baPixels = new byte[nByteCount];
System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, _baPixels, 0, _baPixels.Length);
int _nStride = bitmapData.Stride;
for (int h = 0; h < bitmap.Height; h++)
{
int nCurrentLine = h * _nStride;
for (int w = 0; w < (bitmap.Width * nBytesPerPixel); w += nBytesPerPixel)
{
int nBlue = _baPixels[nCurrentLine + w];
int nGreen = _baPixels[nCurrentLine + w + 1];
int nRed = _baPixels[nCurrentLine + w + 2];
if (colourDictionary.Keys.Count > 0)
{
Color[] caNearbyColours = colourDictionary.Keys.Select(c => c)
.Where(c => (int)c.B <= (nBlue + nTolerance) && (int)c.B >= (nBlue - nTolerance)
&& (int)c.G <= (nGreen + nTolerance) && (int)c.G >= (nGreen - nTolerance)
&& (int)c.R <= (nRed + nTolerance) && (int)c.R >= (nRed - nTolerance)).ToArray();
if (caNearbyColours.Length > 0)
{
if (!colourDictionary[caNearbyColours.FirstOrDefault()].Any(c => c.R == nRed && c.G == nGreen && c.B == nBlue))
colourDictionary[caNearbyColours.FirstOrDefault()].Add(Color.FromArgb(255, nRed, nGreen, nBlue));
}
else
colourDictionary.Add(Color.FromArgb(255, nRed, nGreen, nBlue), new List<Color>());
}
else
colourDictionary.Add(Color.FromArgb(255, nRed, nGreen, nBlue), new List<Color>());
}
}
}
finally
{
bitmap.UnlockBits(bitmapData);
}
using (Bitmap colourBitmap = new Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat))
{
using (Graphics g = Graphics.FromImage(colourBitmap))
{
for (int h = 0; h < colourBitmap.Height; h++)
{
for (int w = 0; w < colourBitmap.Width; w++)
{
Color colour = bitmap.GetPixel(w, h);
if (!colourDictionary.ContainsKey(colour))
{
Color keyColour = colourDictionary.Keys.FirstOrDefault(k => colourDictionary[k].Any(v => v == colour));
colourBitmap.SetPixel(w, h, keyColour);
}
else
colourBitmap.SetPixel(w, h, colour);
}
}
}
colourBitmap.Save(#"OutputPath", System.Drawing.Imaging.ImageFormat.Png);
}
}
Notice how the top section uses Lockbits for better performance. This can easily be transferred onto the bottom section. Also note that the lock bits code is set to work for images with a bytes per pixel of 3 or 4.
Now onto how the code is attempting to work:
It starts off by looping over the initial image and finding colours. If the colour is found already it skips it, however if the colour is not in the dictionary it will add it and also if the colour has a similar colour in the dictionaries keys (will need changing to look at values too) it will add it to it's values.
This then loops through the pixels in the output image setting them according to the keys in the dictionary, thus creating a 'blocked' image.
Now as I said it isn't working just yet, so here are some improvements needed to be made:
As said above checking the colour to those in the values
Adding lock bits to the bottom code for better performance
Tweaking the nTolerance value for better results
Keep track of counts of colours and at the end of looping set the key to that with the largest count
And of course anything else that I have not thought of

How to Replace a RGB Value on a Texture2D on Monogame?

I'm working with C# and Monogame 3.2.
I'm currently working on a 2D game, such like Starbound, and I need to have the blocks connect to each other and when not, have some fancy borders.
What I am doing is I have a texture that covers the whole 32*32 image, and a custom function named "trimImage" to trim the image so it has fancy borders.
But, I need to find a way to set a transparent pixel at a specific pixel in a Texture2D so I can make that border.
http://i.imgur.com/dvh6sI6.png
See the purple dirt, I bassicly want it to trim the image to something like that, and when other blocks are connecting to it it will connect.
Does anyone know how, or atleast a better way to do this "border" effect? Thanks.
Note: In my trim class I really do have is just some comments, nothing else.
Not sure exactly what you want your border to look like, but you can do a color replacement like this using the Texture2D.GetData() functions:
private Color TRANSPARENT = Color.Transparent;
private Color BAD_COLOR = Color.Purple;
private const int DEVIATION = 10;
public Texture2D trimImage(Texture2D texture)
{
/// Get the data from the original texture and place it in an array
Color[] colorData = new Color[texture.Width * texture.Height];
texture.GetData<Color>(colorData);
/// Loop through the array and change the RGB values you choose
for (int x = 0; x < texture.Width; x++)
{
for (int y = 0; y < texture.Height; y++)
{
Color pixel = colorData[x * texture.Height + y];
/// Check if the color is within the range
if (MathHelper.Distance(pixel.R, BAD_COLOR.R) < DEVIATION &&
MathHelper.Distance(pixel.G, BAD_COLOR.G) < DEVIATION &&
MathHelper.Distance(pixel.B, BAD_COLOR.B) < DEVIATION &&
pixel.A != 0f)
{
/// Make that color transparent
pixel = TRANSPARENT;
}
}
}
/// Put the color array into the new texture and return it
texture.SetData<Color>(colorData);
return texture;
}
Being able to make a different border is just a matter of changing loop parameters and choosing the appropriate pixels. Hope it helps

Search through an image for color range

Context
I'm trying to extract everything that is brown out of a ".bmp" of a slide. Since it's a staining of human tissue the exact color of brown is going to be different between slides.
So thus far I've found a plethora of topics of finding a single color.
So far I haven't found anything on being able to search through a picture with a color range. I know that I could do a Color compare = Color.FromArgb(X,X,X,X) several thousand times with different values, but is there any other way of doing this?
there are to ways on is you read the bmp file binary and you lock for the string but remember 3 bytes are one pixel or
something like this
using System.Drawing;
Bitmap img = new Bitmap("*imagePath*");
for (int i = 0; i < img.GetWidth; i++)
{
for (int j = 0; j < img.GetHeight; j++)
{
Color pixel = img.GetPixel(i,j);
if (pixel == *your color*)
{
//do what you want to if you have found the pixel
}
}
}
you only have to create your color to compare it but this shoudnot be an problem or?
here some links how answer the question to.
How can I read image pixels' values as RGB into 2d array?

Why doesn't c# bitmap setPixel() method work?

I have a method that takes in numbers for a rectangle and "blanks" the pixels (turns them white) for that area. For some reason though when my program searches for non white pixels, its finding the ones i just set to white.
Why is it saying its not white when it is? I have an image that gets cropped and is saved to my hard drive. So I'm able to view the area it says isn't white, but when i open the image, its completely white as can be. So I'm at a lose as to why this isn't working. The program stops on the same pixel every time. It says the R value is 238 and I know that pixel was set to white because i stepped through the debugger and watched the pixel value go into the bmp.SetPixel method.
This is the code for the "blanking" method:
void blankArea(int x, int y, int width, int height)
{
for (int i = y; i <= height+y; ++i)
for (int t = x; t <= width+x; ++t)
{
if (t == w)
break;
bmp.SetPixel(t, i, Color.White);
}
}
this is the code that says that pixel isn't white:
bool allWhiteColumn (int col)
{
for (int i = 5; i < h; ++i)
if (bmp.GetPixel(col - 1, i).R < 248 && bmp.GetPixel(col - 1, i).G < 248)
{
imageBelowColumEnd = bmp.GetPixel(col - 1, i).R;
this.row = i;
return false;
}
return true;
}
Any help at this point would be greatly appreciated. I have no idea why it's saying the R is 238 after I set it to white. Thanks
EDIT- since I post a comment I'll edit instead. Taking the if out, did nothing. Same problem. I can't check for plain white. The program finds images from a scanner. So because a scanners "white" areas are not true white, I can not look for Color.WHITE. I'll try the bitlock though...
EDIT 2- "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." was thrown when i tried to do the example for the locking and unlocking of the bits. It threw it once it tried to copy the bits into the array. Apparently thats not allowed.
Thanks for the answers though.
You could try to use the Bitmap.Lockbits() method before, and Bitmap.Unlockbits() after you have changed the pixels.
Actually, here is an example: http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx
Remove the if (t == w) break; in your first function, whatever is w (which you did not describe). I guess the code breaks and you end up with an incomplete white region.
Would it not make more sense to check for white for starters?
bool allWhiteColumn (int col)
{
for (int i = 5; i < h; ++i)
if (bmp.GetPixel(col - 1, i) != Color.White)
{
imageBelowColumEnd = bmp.GetPixel(col - 1, i).R;
this.row = i;
return false;
}
return true;
}

Categories

Resources