What I am trying to do is check an image row or column, and if it contains all white pixels then trim that row or column.
I am not sure why, but when I run this code snippet, img.Width in TrimLeft is 1, before subtracting 1 from it.
The actual image width is 175. I end up with an ArgumentException. I have a similar method for trimming the right side, and that works fine.
class ImageHandler
{
public Bitmap img;
public List<int[]> pixels = new List<int[]>();
public ImageHandler(String path)
{
img = new Bitmap(path);
GetPixels();
}
public void TrimLeft()
{
while (CheckColIfWhite(0, 0))
{
Rectangle cropBox = new Rectangle(1, 0, (img.Width-1), img.Height);
Bitmap cropdImg = CropImage(img, cropBox);
img = cropdImg;
}
}
public bool CheckColIfWhite(int colx, int starty)
{
bool allPixelsWhite = false;
int whitePixels = 0;
for (int y = starty; y < img.Height; y++)
{
if (pixels[y][colx] >= 200) { whitePixels++; }
else { return false; }
if (whitePixels == img.Height) { allPixelsWhite = true; }
}
return allPixelsWhite;
}
public void GetPixels()
{
for (int y = 0; y < img.Height; y++)
{
int[] line = new int[img.Width];
for (int x = 0; x < img.Width; x++)
{
line[x] = (int) img.GetPixel(x, y).R;
}
pixels.Add(line);
}
}
public Bitmap CropImage(Bitmap tImg, Rectangle area)
{
Bitmap bmp = new Bitmap(tImg);
Bitmap bmpCrop = bmp.Clone(area, bmp.PixelFormat);
return bmpCrop;
}
}
Your method seems like it will be remarkably inefficient - if the image is 175 pixels wide, and entirely white, you're going to create 175 copies of it, before (probably) failing when trying to create a 0 pixel wide image.
Why not examine each column in turn until you find a non-white column, and then perform a single crop at that time. Untested code, and with other changes hopefully obvious:
public Bitmap CropImage (Bitmap image)
{
int top = 0;
int bottom = image.Height-1;
int left = 0;
int right = image.Width-1;
while(left < right && CheckColIfWhite(image,left))
left++;
if(left==right) return null; //Entirely white
while(CheckColIfWhite(image,right)) //Because left stopped, we know right will also
right--;
while(CheckRowIfWhite(image,top))
top++;
while(CheckRowIfWhite(image,bottom))
bottom--;
return CropImage(image,new Rectangle(left,top,right-left+1,bottom-top+1));
}
(E.g. I'm now passing the image around, I've modified CheckColIfWhite and CheckRowIfWhite to take the image also, and to assume that one parameter is always fixed at 0)
Also, not sure why you're extracting the pixel array beforehand, so I'm putting my re-written CheckColIfWhite too:
public bool CheckColIfWhite(Bitmap image,int colx)
{
for (int y = 0; y < image.Height; y++)
{
if (image.GetPixel(colx,y).R < 200)
return false;
}
return true;
}
Related
I'm coming from Winforms trying to rewrite a program in WPF, and I want to display a certain portion of the whole image, depending on an Id I use for a list, that I load each portion of the whole image in. I was able to do it successfully in Winforms, but I want to perform the same task in WPF using Controls.Image. Heres what I did in Winforms.
PictureBox picBox;
List<Image> tileImageList;
Image FullImage;
public TileFrame(PictureBox pbox)
{
picBox = pbox;
FullImage = picBox.Image; //The source of the picBox is set to the full image on init
tileImageList = new List<Image>();
PopTileList();
}
void PopTileList()
{
const int SIZE = 32;
Bitmap bitFullImage = new Bitmap(FullImage);
for (int y = 0; y < 48; y++)
{
for (int x = 0; x < 64; x++)
{
var portion = bitFullImage.Clone(new Rectangle((x * SIZE), (y * SIZE), SIZE, SIZE), bitFullImage.PixelFormat);
tileImageList.Add(portion);
}
}
picBox.Image = tileImageList[10];//The first image that shows when this is done
}
public void ShowTilePic(int selectedId)
{
picBox.Image = tileImageList[--selectedId];
}
Since the image being displayed will change based on the selected item of a listbox, the tileImageList is crucial for relating the list box selected index and the tileImageList index. Other answers I've searched for seemed to make it much more complicated than what I've done here. Is there a simple way to do this in WPF and in code?
Nvm I figured it out.
List<CroppedBitmap> tileImageList;
Image imageBox;
public TileFrame(MainWindow mWindow)
{
tileImageList = new List<CroppedBitmap>();
imageBox = mWindow.ImageBox;
PopTileList();
}
void PopTileList()
{
const int SIZE = 32;
var bitmapImage = (BitmapSource)imageBox.Source;
for (int y = 0; y < 48; y++)
{
for (int x = 0; x < 64; x++)
{
var portion = new CroppedBitmap(bitmapImage, new Int32Rect((x * SIZE), (y * SIZE), SIZE, SIZE));
tileImageList.Add(portion);
}
}
}
public void ShowTilePic(int selectedId)
{
imageBox.Source = tileImageList[selectedId];
}
It seems, that if image, that is used for icon for Windows shortcuts, doesn't have aspect ratio 1:1, it will look stretched.
Left one is how it actually looks, and right one is how it should look.
I'm creating shortcut and icon programmatically from image, so I want to fix image, so it will have correct aspect ratio, but image will not look stretched. This can be achieved by adding some padding to image.
As for now, I'm simply copying image to new bitmap with correct aspect ratio, but filling new area with transparent pixels
public static Bitmap FixBitmapAspectRatio(Bitmap sourceBitmap)
{
if (sourceBitmap.Width.Equals(sourceBitmap.Height))
return sourceBitmap;
int size;
bool horizontallyOriented;
if (sourceBitmap.Width > sourceBitmap.Height)
{
horizontallyOriented = true;
size = sourceBitmap.Width;
}
else
{
horizontallyOriented = false;
size = sourceBitmap.Height;
}
var sizeDifference = Math.Abs(sourceBitmap.Width - sourceBitmap.Height);
var newBitmap = new Bitmap(size, size);
var transparentColor = Color.FromArgb(0, 0, 0, 0);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (horizontallyOriented)
{
if (i < sizeDifference / 2 || i >= sizeDifference / 2 + sourceBitmap.Height)
{
newBitmap.SetPixel(j, i, transparentColor);
}
else
{
var originalPixel = sourceBitmap.GetPixel(j, i - sizeDifference / 2);
newBitmap.SetPixel(j, i, originalPixel);
}
}
else
{
if (i < sizeDifference / 2 || i >= sizeDifference / 2 + sourceBitmap.Width)
{
newBitmap.SetPixel(i, j, transparentColor);
}
else
{
var originalPixel = sourceBitmap.GetPixel(i - sizeDifference / 2, j);
newBitmap.SetPixel(i, j, originalPixel);
}
}
}
}
return newBitmap;
}
But I don't know, if I'm inventing a wheel. Is there any way to do this by means by standard libraries, or maybe easier way to achieve what I need?
You really don't want to set individual pixels :)
Instead, have a look at the Graphics class, in particular Graphics.FromImage (that's where you paint to) and Graphics.DrawImage (that's how you paint the scaled image).
Basically its my program to search image in another image pixel by pixel with simpliest nested loop.
It works fine for lookin 2x2 in 9x9 image, but it fails to search in 8408 x 8337 resolution (I guess I don't need that much, but I wanted to check how slow it will be)
Error occures when declaring Color 2D array
So I am asking for help because of why, and how to fix this
I suspect that OOM error is caused by that he SizeOfColorType * 8337 is bigger than Int32... but I am not sure if this is correct
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace IMGReach
{
class Program
{
static void Main(string[] args)
{
String basePath = #"C:\Users\Administrator\Desktop\IMGReachImages\1.jpg"; // 8408x8337
String searchPath = #"C:\Users\Administrator\Desktop\IMGReachImages\2.jpg"; // 69 x 56
Bitmap b = new Bitmap(basePath); // base image
Bitmap s = new Bitmap(searchPath); // image to search inside of base image
var x = ImageInImage(b, s);
Console.WriteLine(x[0]); // x coord of found image
Console.WriteLine(x[1]); // y coord of found image
Console.Write("END");
Console.ReadKey();
}
private static int[] ImageInImage(Bitmap source, Bitmap searchIt)
{
// Creates array of colors based on image
Color[,] basePixels = AreaToArray(source, 0, 0, source.Width, source.Height);
Color[,] searchPixels = AreaToArray(searchIt, 0, 0, searchIt.Width, searchIt.Height);
Color hookPixel = searchPixels[0, 0];
for (int xpos = 0; xpos < basePixels.GetLength(0); xpos++)
{
for (int ypos = 0; ypos < basePixels.GetLength(1); ypos++)
{
if (basePixels[xpos, ypos] == hookPixel)
{
Color[,] checkMap = AreaToArray(source, xpos, ypos, searchIt.Width, searchIt.Height);
if (sameArray(checkMap, searchPixels))
return new int[] { xpos, ypos };
}
}
}
return null;
}
private static Color[,] AreaToArray(Bitmap source, int start_x, int start_y, int width, int height)
{
if (start_x < 0)
throw new ArgumentException("Invalid parameter value (less than zero)", "start_x");
if (start_y < 0)
throw new ArgumentException("Invalid parameter value (less than zero)", "start_y");
if (width > source.Width)
throw new ArgumentException("Width parameter is bigger than source width", "width");
if (height > source.Height)
throw new ArgumentException("Height parameter is bigger than source height", "height");
Color[,] pixelSet = new Color[width, height]; // [2,2]
for (int ix = 0; ix < width; ix++ )
for (int iy = 0; iy < height; iy++)
pixelSet[ix, iy] = source.GetPixel(ix + start_x, iy + start_y);
return pixelSet;
}
private static bool sameArray(int[,] a1, int[,] a2)
{
if (a1.GetLength(0) != a2.GetLength(0))
return false;
if (a1.GetLength(1) != a2.GetLength(1))
return false;
for (int i = 0; i < a1.GetLength(0); i++)
{
for (int j = 0; j < a1.GetLength(1); j++)
{
if (a1[i, j] == a2[i, j])
{
continue;
}
else
{
return false;
}
}
}
return true;
}
private static bool sameArray(Color[,] a1, Color[,] a2)
{
if (a1.GetLength(0) != a2.GetLength(0))
return false;
if (a1.GetLength(1) != a2.GetLength(1))
return false;
for (int i = 0; i < a1.GetLength(0); i++)
{
for (int j = 0; j < a1.GetLength(1); j++)
{
if (a1[i, j] == a2[i, j])
{
continue;
}
else
{
return false;
}
}
}
return true;
}
}
}
An image that's 8408 x 8337 contains a little over 70 million pixels. The Color structure is at least 4 bytes in size. So you're trying to allocate a minimum of 280 megabytes. Your system must have 280 megabytes of contiguous memory free. If you're running on a 32-bit system (or running the program in 32 bit mode), it's quite possible that you don't have that memory available. Thus, OutOfMemoryException.
It gets worse. Even if you somehow manage to allocate memory for that image, your ImageInImage method needs to allocate the basePixels array, which will be the same size.
You've probably noticed by now that your program is pretty slow even on small images. Using GetPixel to read the image colors is going to be really slow. You need to look into using Bitmap.LockBits so that you can access the image bits directly. That MSDN topic has a basic example. You'll need to understand a little about the internal bitmap structure if you want to use it effectively, but there are plenty of examples available if you search a bit.
I have tried this code for converting a bitmap to pure black and white - not greyScale, but this gives me a pure black image.
public Bitmap blackwhite(Bitmap source)
{
Bitmap bm = new Bitmap(source.Width,source.Height);
for(int y=0;y<bm.Height;y++)
{
for(int x=0;x<bm.Width;x++)
{
if (source.GetPixel(x, y).GetBrightness() > 0.5f)
{
source.SetPixel(x,y,Color.White);
}
else
{
source.SetPixel(x,y,Color.Black);
}
}
}
return bm;
}
What can cause such a problem? Is there any alternate method to this?
I know this answer is way too late but I just figured it out and hope it helps other people having this problem.
I get the average brightness of the picture and use that as the threshold for setting pixels to black or white. It isn't 100% accurate and definitely isn't optimized for time complexity but it gets the job done.
public static void GetBitmap(string file)
{
using (Bitmap img = new Bitmap(file, true))
{
// Variable for image brightness
double avgBright = 0;
for (int y = 0; y < img.Height; y++)
{
for (int x = 0; x < img.Width; x++)
{
// Get the brightness of this pixel
avgBright += img.GetPixel(x, y).GetBrightness();
}
}
// Get the average brightness and limit it's min / max
avgBright = avgBright / (img.Width * img.Height);
avgBright = avgBright < .3 ? .3 : avgBright;
avgBright = avgBright > .7 ? .7 : avgBright;
// Convert image to black and white based on average brightness
for (int y = 0; y < img.Height; y++)
{
for (int x = 0; x < img.Width; x++)
{
// Set this pixel to black or white based on threshold
if (img.GetPixel(x, y).GetBrightness() > avgBright) img.SetPixel(x, y, Color.White);
else img.SetPixel(x, y, Color.Black);
}
}
// Image is now in black and white
}
public unsafe Bitmap MedianFilter(Bitmap Img)
{
int Size =2;
List<byte> R = new List<byte>();
List<byte> G = new List<byte>();
List<byte> B = new List<byte>();
int ApetureMin = -(Size / 2);
int ApetureMax = (Size / 2);
BitmapData imageData = Img.LockBits(new Rectangle(0, 0, Img.Width, Img.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
byte* start = (byte*)imageData.Scan0.ToPointer ();
for (int x = 0; x < imageData.Width; x++)
{
for (int y = 0; y < imageData.Height; y++)
{
for (int x1 = ApetureMin; x1 < ApetureMax; x1++)
{
int valx = x + x1;
if (valx >= 0 && valx < imageData.Width)
{
for (int y1 = ApetureMin; y1 < ApetureMax; y1++)
{
int valy = y + y1;
if (valy >= 0 && valy < imageData.Height)
{
Color tempColor = Img.GetPixel(valx, valy);// error come from here
R.Add(tempColor.R);
G.Add(tempColor.G);
B.Add(tempColor.B);
}
}
}
}
}
}
R.Sort();
G.Sort();
B.Sort();
Img.UnlockBits(imageData);
return Img;
}
I tried to do this. but i got an error call "Bitmap region is already locked" can anyone help how to solve this. (error position is highlighted)
GetPixel is the slooow way to access the image and doesn't work (as you noticed) anymore if someone else starts messing with the image buffer directly. Why would you want to do that?
Check Using the LockBits method to access image data for some good insight into fast image manipulation.
In this case, use something like this instead:
int pixelSize = 4 /* Check below or the site I linked to and make sure this is correct */
byte* color =(byte *)imageData .Scan0+(y*imageData .Stride) + x * pixelSize;
Note that this gives you the first byte for that pixel. Depending on the color format you are looking at (ARGB? RGB? ..) you need to access the following bytes as well. Seems to suite your usecase anyway, since you just care about byte values, not the Color value.
So, after having some spare minutes, this is what I'd came up with (please take your time to understand and check it, I just made sure it compiles):
public void SomeStuff(Bitmap image)
{
var imageWidth = image.Width;
var imageHeight = image.Height;
var imageData = image.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
var imageByteCount = imageData.Stride*imageData.Height;
var imageBuffer = new byte[imageByteCount];
Marshal.Copy(imageData.Scan0, imageBuffer, 0, imageByteCount);
for (int x = 0; x < imageWidth; x++)
{
for (int y = 0; y < imageHeight; y++)
{
var pixelColor = GetPixel(imageBuffer, imageData.Stride, x, y);
// Do your stuff
}
}
}
private static Color GetPixel(byte[] imageBuffer, int imageStride, int x, int y)
{
int pixelBase = y*imageStride + x*3;
byte blue = imageBuffer[pixelBase];
byte green = imageBuffer[pixelBase + 1];
byte red = imageBuffer[pixelBase + 2];
return Color.FromArgb(red, green, blue);
}
This
Relies on the PixelFormat you used in your sample (regarding both the pixelsize/bytes per pixel and the order of the values). If you change the PixelFormat this will break.
Doesn't need the unsafe keyword. I doubt that it makes a lot of difference, but you are free to use the pointer based access instead, the method would be the same.