I am working on my Project in Windows Forms and I have a Problem. In this Project I have to Compare Screenshot with Images from File. I have good Method for Comparison in Internet found and it seems to be working. For example: I have cuted Facebook Logo from site and saved it on Desktop. When I am comparing this Logo with itself(I am making a screenshot of a logo and than compare this screenshot with Logo) this method works correctly(it says that screenshot contains Logo) but when I am making a Screenshot on it's Site and than compare it with Logo, This Method says that Screenshot donn't contains Logo.
I am using this Compare Method:
public static Rectangle searchBitmap(Bitmap smallBmp, Bitmap bigBmp, double tolerance)
{
BitmapData smallData =
smallBmp.LockBits(new Rectangle(0, 0, smallBmp.Width, smallBmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
BitmapData bigData =
bigBmp.LockBits(new Rectangle(0, 0, bigBmp.Width, bigBmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
int smallStride = smallData.Stride;
int bigStride = bigData.Stride;
int bigWidth = bigBmp.Width;
int bigHeight = bigBmp.Height - smallBmp.Height + 1;
int smallWidth = smallBmp.Width * 3;
int smallHeight = smallBmp.Height;
Rectangle location = Rectangle.Empty;
int margin = Convert.ToInt32(255.0 * tolerance);
unsafe
{
byte* pSmall = (byte*)(void*)smallData.Scan0;
byte* pBig = (byte*)(void*)bigData.Scan0;
int smallOffset = smallStride - smallBmp.Width * 3;
int bigOffset = bigStride - bigBmp.Width * 3;
bool matchFound = true;
for (int y = 0; y < bigHeight; y++)
{
for (int x = 0; x < bigWidth; x++)
{
byte* pBigBackup = pBig;
byte* pSmallBackup = pSmall;
//Look for the small picture.
for (int i = 0; i < smallHeight; i++)
{
int j = 0;
matchFound = true;
for (j = 0; j < smallWidth; j++)
{
//With tolerance: pSmall value should be between margins.
int inf = pBig[0] - margin;
int sup = pBig[0] + margin;
if (sup < pSmall[0] || inf > pSmall[0])
{
matchFound = false;
break;
}
pBig++;
pSmall++;
}
if (!matchFound) break;
//We restore the pointers.
pSmall = pSmallBackup;
pBig = pBigBackup;
//Next rows of the small and big pictures.
pSmall += smallStride * (1 + i);
pBig += bigStride * (1 + i);
}
//If match found, we return.
if (matchFound)
{
location.X = x;
location.Y = y;
location.Width = smallBmp.Width;
location.Height = smallBmp.Height;
break;
}
//If no match found, we restore the pointers and continue.
else
{
pBig = pBigBackup;
pSmall = pSmallBackup;
pBig += 3;
}
}
if (matchFound) break;
pBig += bigOffset;
}
}
bigBmp.UnlockBits(bigData);
smallBmp.UnlockBits(smallData);
return location;
}
This method returns a Rectangle "location". If (location.Width == 0 || location.height == 0), it means that Screenshot doesn't contain Image.
What's the problem?
If the screenshot contains any blank space around the border, it will most likely not match with the image (unless the screenshot is the whole screen).
Related
I'd like to figure out how I can add a 1 pixel border in between each frame that my spritesheet creator generates, can anyone help?
The function below takes an array of images and turns them into a table filled with bitmaps.
The program is supposed to take a gif and turn it into multiple spritesheets, so that it can be displayed in a game engine. That part is working fine, but I'd like to add a 1 pixel border in between each frame.
public Bitmap[] combineFrames(Image[] images, int columns)
{
int rows = columns;
int imagesPerSheet = rows * columns;
int sheets = (int)Math.Ceiling((double)images.Length / imagesPerSheet);
Bitmap[] mapTable = new Bitmap[55]; //55 being the max #sheets
for (int x = 0; x < sheets; x++)
{
Bitmap bitmap = new Bitmap(images[0].Width * columns, images[0].Height * rows);
Graphics graphics = Graphics.FromImage(bitmap);
int L = 0;
int remainingNFrames = (images.Length - (imagesPerSheet * x));
if (remainingNFrames < imagesPerSheet)
{
L = remainingNFrames;
}
else
{
L = imagesPerSheet;
}
for (int i = 0; i <= L; i++)
{
int formulizedProduct = imagesPerSheet * (x) + (i);
if (formulizedProduct == images.Length) { break; } //should always be length - 1
Image image = images[formulizedProduct]; //16*x+i
int X = (image.Width * ((i - 1) % columns));
int Y = (image.Height * (int)((double)(i - 1) / columns));
graphics.DrawImage(
image,
X, //I feel like I'd have to adjust this part and the part below, but I'm not really sure.
Y
);
}
mapTable[x] = bitmap;
}
return mapTable;
}
Thanks.
I'm working on a screen sharing app, which runs a loop and grab fast screenshots using GDI methods . example here
Of course I also use a flood fill algorithm to find the changes areas between 2 images (previous screenshot and current).
I use another small trick - I downscale the snapshot resolution in 10, because processing 1920*1080=2073600 pixels very constantly is not very efficient.
However when I find the rectangle bounds - I apply it on the original full size bitmap and I just multiply by 10 the dimension (including top, left, width, height).
This is the scanning code:
unsafe bool ArePixelsEqual(byte* p1, byte* p2, int bytesPerPixel)
{
for (int i = 0; i < bytesPerPixel; ++i)
if (p1[i] != p2[i])
return false;
return true;
}
private unsafe List<Rectangle> CodeImage(Bitmap bmp, Bitmap bmp2)
{
List<Rectangle> rec = new List<Rectangle>();
var bmData1 = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
var bmData2 = bmp2.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp2.PixelFormat);
int bytesPerPixel = 4;
IntPtr scan01 = bmData1.Scan0;
IntPtr scan02 = bmData2.Scan0;
int stride1 = bmData1.Stride;
int stride2 = bmData2.Stride;
int nWidth = bmp.Width;
int nHeight = bmp.Height;
bool[] visited = new bool[nWidth * nHeight];
byte* base1 = (byte*)scan01.ToPointer();
byte* base2 = (byte*)scan02.ToPointer();
for (int y = 0; y < nHeight; y ++)
{
byte* p1 = base1;
byte* p2 = base2;
for (int x = 0; x < nWidth; ++x)
{
if (!ArePixelsEqual(p1, p2, bytesPerPixel) && !(visited[x + nWidth * y]))
{
// fill the different area
int minX = x;
int maxX = x;
int minY = y;
int maxY = y;
var pt = new Point(x, y);
Stack<Point> toBeProcessed = new Stack<Point>();
visited[x + nWidth * y] = true;
toBeProcessed.Push(pt);
while (toBeProcessed.Count > 0)
{
var process = toBeProcessed.Pop();
var ptr1 = (byte*)scan01.ToPointer() + process.Y * stride1 + process.X * bytesPerPixel;
var ptr2 = (byte*)scan02.ToPointer() + process.Y * stride2 + process.X * bytesPerPixel;
//Check pixel equality
if (ArePixelsEqual(ptr1, ptr2, bytesPerPixel))
continue;
//This pixel is different
//Update the rectangle
if (process.X < minX) minX = process.X;
if (process.X > maxX) maxX = process.X;
if (process.Y < minY) minY = process.Y;
if (process.Y > maxY) maxY = process.Y;
Point n; int idx;
//Put neighbors in stack
if (process.X - 1 >= 0)
{
n = new Point(process.X - 1, process.Y); idx = n.X + nWidth * n.Y;
if (!visited[idx]) { visited[idx] = true; toBeProcessed.Push(n); }
}
if (process.X + 1 < nWidth)
{
n = new Point(process.X + 1, process.Y); idx = n.X + nWidth * n.Y;
if (!visited[idx]) { visited[idx] = true; toBeProcessed.Push(n); }
}
if (process.Y - 1 >= 0)
{
n = new Point(process.X, process.Y - 1); idx = n.X + nWidth * n.Y;
if (!visited[idx]) { visited[idx] = true; toBeProcessed.Push(n); }
}
if (process.Y + 1 < nHeight)
{
n = new Point(process.X, process.Y + 1); idx = n.X + nWidth * n.Y;
if (!visited[idx]) { visited[idx] = true; toBeProcessed.Push(n); }
}
}
//finaly set a rectangle.
Rectangle r = new Rectangle(minX * 10, minY * 10, (maxX - minX + 1) * 10, (maxY - minY + 1) * 10);
rec.Add(r);
//got the rectangle now i'll do whatever i want with that.
//notify i scaled everything by x10 becuse i want to apply the changes on the originl 1920x1080 image.
}
p1 += bytesPerPixel;
p2 += bytesPerPixel;
}
base1 += stride1;
base2 += stride2;
}
bmp.UnlockBits(bmData1);
bmp2.UnlockBits(bmData2);
return rec;
}
This is my call:
private void Start()
{
full1 = GetDesktopImage();//the first,intial screen.
while (true)
{
full2 = GetDesktopImage();
a = new Bitmap(full1, 192, 108);//resizing for faster processing the images.
b = new Bitmap(full2, 192, 108); // resizing for faster processing the images.
CodeImage(a, b);
count++; // counter for the performance.
full1 = full2; // assign old to current bitmap.
}
}
However, after all the tricks and techniques I used, the algorithm runs quite slow... on my machine - Intel i5 4670k 3.4ghz - it runs only 20 times (at the maximum! It might get lower)! It maybe sounds fast (don't forget I have to send each changed area over the network after), but I'm looking to achieve more processed image per second. I think the main bottleneck is in the resizing of the 2 images - but I just thought it would be even faster after resizing - because it would have to loop through less pixels... 192*108=200,000 only..
I would appreciate any help, any improvement. Thanks.
i want to compare pixel of an image with all pixel of second image and then next pixel with all pixel of second image;
i am using this code in which i am comparing pixel (converted in byte) one pixel of one image with second pixel of second but i do not want this approach. Please reply fast.
Thanks in advance.
public static double GetDifferentPercentageSneller(ref Bitmap bmp1, ref Bitmap bmp2)
{
//if (bmp1 == null || bmp2 == null)
// return 100.0;
//if (bmp1.Size != bmp2.Size)
// return 100.0;
//if (bmp1.PixelFormat != bmp2.PixelFormat)
// return 100.0;
int iMismatch = 0;
int iMatch = 0;
unsafe
{
BitmapData data1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), ImageLockMode.ReadOnly, bmp1.PixelFormat);
BitmapData data2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadOnly, bmp2.PixelFormat);
int pixelBytes = 0;
switch (data1.PixelFormat)
{
case PixelFormat.Format32bppArgb:
pixelBytes = 4;
break;
case PixelFormat.Format24bppRgb:
pixelBytes = 3;
break;
default:
throw new Exception("Bitmap format not supported");
}
int paddingBytes = data1.Stride % pixelBytes;
byte* location1 = (byte*)data1.Scan0;
byte* location2 = (byte*)data2.Scan0;
for (int y = 0; y < data1.Height; ++y)
{
for (int x = 0; x < data1.Width; ++x)
{
if (*location1 == *location2)
{
iMatch++;
}
else
{
iMismatch++;
}
location1 += pixelBytes;
location2 += pixelBytes;
}
location1 += paddingBytes;
location2 += paddingBytes;
}
bmp1.UnlockBits(data1);
bmp2.UnlockBits(data2);
}
double percent = (double)iMatch/ (double)(iMismatch + iMatch);
return percent * 100.0;
}
You have to always compare the LARGER image (both x, y) with the SMALLER. Although I don't know what your are exactly after, you can do it simply like this.
BitmapImage Image1 = new BitmapImage(ImageStream);
BitmapImage Image2 = new BitmapImage(ImageStream);
int X = Image1.Width > Image2.Width ? Image2.Width : Image1.Width;
int Y = Image1.Hieght > Image2.Height ? Image2.Heigth : Image1.Height;
for(int x = 0; x < X; x++){
for(int y = 0; y < Y; y++){
Color color1 = Image1.GetPixel(x, y);
Color color2 = Image2.GetPixel(x, y);
// Do comparison here
}
}
The C# software I'm involved with writing has a component that involves the reading of barcodes from scanned documents. The PDFs themselves are opened using PDFSharp.
Unfortunately we're encountering an issue with the process when it involves Flate Decoding of PDFs. Basically, all we get is a bunch of fuzz, which means there is no barcode to check and the document is not recognised.
Our code (which we shamelessly "borrowed" from another Stack Overflow case!) is as follows:
private FileInfo ExportAsPngImage(PdfDictionary image, string sourceFileName, ref int count)
{
//This code basically comes from http://forum.pdfsharp.net/viewtopic.php?f=2&t=2338#p6755
//and http://stackoverflow.com/questions/10024908/how-to-extract-flatedecoded-images-from-pdf-with-pdfsharp
string tempFile = string.Format("{0}_Image{1}.png", sourceFileName, count);
int width = image.Elements.GetInteger(PdfImage.Keys.Width);
int height = image.Elements.GetInteger(PdfImage.Keys.Height);
int bitsPerComponent = image.Elements.GetInteger(PdfImage.Keys.BitsPerComponent);
var pixelFormat = new PixelFormat();
switch (bitsPerComponent)
{
case 1:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
break;
case 24:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
break;
default:
throw new Exception("Unknown pixel format " + bitsPerComponent);
}
var fd = new FlateDecode();
byte[] decodedBytes = fd.Decode(image.Stream.Value);
byte[] resultBytes = null;
int newWidth = width;
int alignment = 4;
if (newWidth % alignment != 0)
//Image data in BMP files always starts at a DWORD boundary, in PDF it starts at a BYTE boundary.
//Most images have a width that is a multiple of 4, so there is no problem with them.
//You must copy the image data line by line and start each line at the DWORD boundary.
{
while (newWidth % alignment != 0)
{
newWidth++;
}
var copy_dword_boundary = new byte[height, newWidth];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < newWidth; x++)
{
if (x <= width && (x + (y * width) < decodedBytes.Length))
// while not at end of line, take orignal array
copy_dword_boundary[y, x] = decodedBytes[x + (y * width)];
else //fill new array with ending 0
copy_dword_boundary[y, x] = 0;
}
}
resultBytes = new byte[newWidth * height];
int counter = 0;
for (int x = 0; x < copy_dword_boundary.GetLength(0); x++)
{
for (int y = 0; y < copy_dword_boundary.GetLength(1); y++)
{ //put 2dim array back in 1dim array
resultBytes[counter] = copy_dword_boundary[x, y];
counter++;
}
}
}
else
{
resultBytes = new byte[decodedBytes.Length];
decodedBytes.CopyTo(resultBytes, 0);
}
//Create a new bitmap and shove the bytes into it
var bitmap = new Bitmap(newWidth, height, pixelFormat);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
int length = (int)Math.Ceiling(width * bitsPerComponent / 8.0);
for (int i = 0; i < height; i++)
{
int offset = i * length;
int scanOffset = i * bitmapData.Stride;
Marshal.Copy(resultBytes, offset, new IntPtr(bitmapData.Scan0.ToInt32() + scanOffset), length);
}
bitmap.UnlockBits(bitmapData);
//Now save the bitmap to memory
using (var fs = new FileStream(String.Format(tempFile, count++), FileMode.Create, FileAccess.Write))
{
bitmap.Save(fs, ImageFormat.Png);
}
return new FileInfo(tempFile);
}
Unfortunately, all we get out of it is this http://i.stack.imgur.com/FwatQ.png
Any ideas on where we're going wrong, or suggestions for things we might try would be greatly appreciated.
Cheers
Thanks for the suggestions guys. One of the other developers managed to crack it - it was (as Jongware suggested) a JPEG, but it was actually zipped as well! Once unzipped it could be processed and recognised as normal.
I am creating a program that can print out the x- & y- Coordinates from a certain pixel. There is a function like 'GetPixel', this will however get the RGB codes from a given coordinate. What I want is just the vice versa, so I have already the RGB codes and now I'm doing a threshold through my Image to know whether it contains a Color pixel that I desired or not.
This is my code:
So firstly I will upload an image:
public BitmapImage bitmap;
public void hochladen_Click(object sender, EventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".bmp";
dlg.Filter = "BMP Files (*.bmp)|*.bmp";
// Get the selected file name and display in a TextBox
if (dlg.ShowDialog() == true)
{
// Open document
string filename = dlg.FileName;
bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(filename);
bitmap.EndInit();
image.Source = bitmap;
}
}
Then when I click a button in my application, it should do a threshold from my Image, and I am going to detect a red Point (R = 255, G = B = 0)
public Color c;
private void detektieren_Click(object sender, RoutedEventArgs e)
{
double x = bitmap.Width;
double y = bitmap.Height;
bl.Content = x + "x" + y;
So from this Point on, it shouldn't be difficult to find the coordinate:
for (int i = 0; i < x; i++)
{
for (int j = 0; i < j; j++)
{
if (c.R == 255 && c.G == 0 && c.B == 0)
{
//
}
}
}
}
Anyone has idea? Thanks in advance.
Finding pixels matching a RGB value of course may return many pixels, try the following code to get all the pixels represented by Point structure:
public Point[] GetPixelsFromRGB(byte[] rgbData, int stride, Color colorToFind){
int k = stride/4;
return rgbData.Select((x,i)=>new{x,i})
.GroupBy(a=>a.i/4,(key,a)=>a.ToArray())
.Where(g=>g[0].x == colorToFind.Red &&
g[1].x == colorToFind.Green &&
g[2].x == colorToFind.Blue && g[3].x == 255)
.Select(g=> new Point(g[0].i%k, g[0].i / k)).ToArray();
}
//Use this method to get the rgbData
int stride = bitmap.PixelWidth * 4;
byte[] rgbData = new byte[stride * bitmap.PixelHeight];
bitmap.CopyPixels(rgbData, stride, 0);
//then call the method above:
var pixels = GetPixelsFromRGB(rgbData, stride, Colors.Red);
Note that the code above has not been tested, I just typed directly into the answer editor.
After a Little bit modification, it works. So this is my code:
public void detektieren_Click(object sender, RoutedEventArgs e)
{
for (i = 0; i < bitmap.Height; i++)
{
for (j = 0; j < bitmap.Width; j++)
{
stride = bitmap.PixelWidth * (bitmap.Format.BitsPerPixel / 8);
data = new byte[stride * bitmap.PixelHeight];
bitmap.CopyPixels(data, stride, 0);
index = i * stride + 4 * j;
byte A = data[index + 3];
byte R = data[index + 2];
byte G = data[index + 1];
byte B = data[index];
// Create a writer and open the file:
StreamWriter Messdaten;
if (!File.Exists("C:/Users/.../Messdaten.csv"))
{
Messdaten = new StreamWriter("C:/Users/.../Messdaten.csv");
}
else
{
Messdaten = File.AppendText("C:/Users/.../Messdaten.csv");
}
// Write to the file:
Messdaten.WriteLine(index + ";" + A + ";" + R + ";" + G + ";" + B);
// Close the stream:
Messdaten.Close();
if (Convert.ToInt32(R) == 0 && Convert.ToInt32(G) == 0 && Convert.ToInt32(B) == 255)
{
// Create a writer and open the file:
StreamWriter Messdaten2;
if (!File.Exists("C:/Users/.../Messdaten2.csv"))
{
Messdaten2 = new StreamWriter("C:/Users/.../Messdaten2.csv");
}
else
{
Messdaten2 = File.AppendText("C:/Users/.../Messdaten2.csv");
}
// Write to the file:
Messdaten2.WriteLine(index + ";" + i + ";" + j);
// Close the stream:
Messdaten2.Close();
}
}
}
}
In the first Excel file (Messdaten.csv), all RGB values from a each single Pixel will be shown. In the second one (Messdaten2.csv) it will Show all Pixels that -let's take for instance- have a value A=0,R=0,G=0,B=255 (= Blue).
Now, how do I create a sum & mean of Pixel i and Pixel j (they're set of values) ? Tried to do this:
if (Convert.ToInt32(R) == 0 && Convert.ToInt32(G) == 0 && Convert.ToInt32(B) == 255)
{
int x_sum = 0; int y_sum = 0;
int x_count = 0; int y_count = 0;
int x_mw = 0; int y_mw = 0;
x_sum = x_sum + i;
x_count++;
y_sum = y_sum + j;
y_count++;
x_mw = x_sum / x_count;
y_mw = y_sum / y_count;
}
But why it didn't work? The x_sum and y_sum only Show the last value of Pixel i and j, and the x_count and y_count (as presumed) show only the value of 1. What did I wrong?