- Home /
Compare 2 bitmaps
Hello,
I am trying to create a mini game for a school project. The goal is to trace a line thats drawn on a bitmap in photoshop.
At the end of the game i need t check how good you did this. now i use this code for checking it
for(var xcoord =0; xcoord < 1024; xcoord++)
{
for(var ycoord = 0; ycoord < 768; ycoord++)
{
var col = backdrop.GetPixel(xcoord,ycoord);
var check_color = drawing.GetPixel(xcoord,ycoord);
if(col != Color.white && check_color == Color.red)
{
match++;
}
}
}
But unity crashes if i run the code. Is there a better way to compare the 2 bitmaps??
Answer by Bunny83 · Mar 01, 2011 at 12:56 AM
I can't see anything is wrong here. Yre you sure that this part crashs Unity?
Well just some hints:
- Make sure your variables
backdrop
anddrawing
are really of type Texture2D and they are existing (not null). - Be sure you can read the pixels. Check the isReadable flag. For more information check out the script reference on
Texture3d.GetPixel
. - Maybe show a bit more of your script. You can edit your question at any time to add/correct/modify your information.
That script isn't "optimized" in any way; explicit typing won't do anything at all except make the source code bigger. Type inference already takes care of variable types, since "0" is an int, and GetPixel returns Color.
Well, you're right :D i don't use JS very often. I will remove the script example since it's useless.
Well, C# is the same actually. "var col = backdrop.GetPixel(xcoord,ycoord);" is valid code in both languages that will make "col" be typed as Color.
That's true but i don't know any C# developer that uses the "var" keyword. It just makes the code more cryptic to me. It's one of these things i never would use, like "goto". It's still there but it doesn't help to improve the code. I agree that var can be useful in some cases, especially with some very long generic types but i don't like it ;)
I see var used in C# quite a bit. If the type is obvious from context, it doesn't make the code more cryptic. Like anything, it can be abused, but it's there for a reason. Even goto can, on rare occasions, be appropriate.
Answer by Rolo · Mar 01, 2011 at 01:14 AM
MM i found out that the image wanted to check wasn't the right format. i used Auto 16 bit. bit i needed rgb 24 bit. code works fine now with no more crashes.
New revised code:
if(Input.GetMouseButton(0)) { for(var xcoord=Input.mousePosition.x-5 ;xcoord<Input.mousePosition.x+5 ;xcoord++) { for(var ycoord=Input.mousePosition.y-5 ;ycoord<Input.mousePosition.y+5 ;ycoord++) { var pixcolor = Color.red;
var check_color = backdrop.GetPixel(xcoord,ycoord);
var draw_color = drawing.GetPixel(xcoord,ycoord);
if(check_color != Color.white && draw_color != Color.red)
{
match++;
}
drawing.SetPixel (xcoord, ycoord, pixcolor);
}
}
}
Checking it on the fly now, just before the painted line is drawn om the canvas. work like a charm now :)
Your answer
Follow this Question
Related Questions
Color of texture pixels not pure 2 Answers
Check/compare array elements 1 Answer
Changed array type, different functions? 1 Answer
Letter Ranking System, Checking Higher Rank 1 Answer
Compare Vector3 3 Answers