- Home /
How to pixelate an image texture for faster pixel color recognition?
I'm currently using GetPixel() for scanning small image files for later use of the color array. I pick the contents of the array and make an average of the colors to kind of simulate what the pixelate or mosaic effect would do, but I would need to apply this on much larger pictures and that would take forever. Is there a script I could adapt to correctly achieve the pixelation?
Thanks a lot.
Answer by robertbu · Jun 05, 2013 at 04:30 PM
You can use Texture2D.GetPixels32 to get all the pixels and then do your calculations directly on the pixel array. There will be a substantial performance improvement over repeated GetPixel() calls.
even when i do GetPixels() or GetPixels32() it tales pretty long... i was starting to ask myself maybe i need to take an average from several close pixels. anyone has an idea how to do that?
Hello there. For color recongnition I ended up writing this function
function Update () {
if (webcamTexture.isPlaying) {
//Calculate grid cells
var Xplus : int = 600;
var Yplus :int = 118;
for (var gr = 0; gr<30; gr++){
gridCubes[gr] = pixCheck(Xplus+calibGrid,720-Yplus-calibGrid,calibGrid,calibGrid);
if (Xplus<1100)
Xplus+=110;
else {
Xplus=600;
Yplus+=110;
}
//Check for cubes on grid
for (var fos = 0; fos<30; fos++) {
if (gridCubes[fos].r<0.2 && gridCubes[fos].g<0.2 && gridCubes[fos].b<0.2)
{bCubeCount++;gridCubez[fos]=true;}
if (gridCubes[fos].r>0.4 && gridCubes[fos].g>0.6 && gridCubes[fos].b>0.6)
{wCubeCount++;gridCubez[fos]=true;}
newCustomPiece[cubeCount] = Instantiate(createe,Vector3(putin.position.x,putin.position.y+posY[iz]
GameObject.Find("Arbol").GetComponent(Comp).newCustomPiece = newCustomPiece[0];
}
//Return color from specified space
function pixCheck(x:int, y:int, bwidth:int, bheight:int) : Color {
data = webcamTexture.GetPixels(x,y,bwidth,bheight);
var r : float = 0; var g : float = 0 ; var b : float = 0; var divider = data.length;
for( var i=0; i<data.length; i++) {
r += data[i].r;
g += data[i].g;
b += data[i].b;
}
var newColor = new Color(r/divider,g/divider,b/divider,1);
return newColor;
You basically send the coords of the area of pixels you want to average and it will create arrays of r,g,b that will be averaged and returned as a single color. It checks all at once with one call, so it's much more faster.
Edit: For a project I was working on, I was recognizing wooden black & white cubes on a grid surface. I averaged the specified spaces to check for black or whites and it then created a 3D piece made of cubes for the game based on the cubes placed on the grid. I simplified the code a bit, it was too long.
$$anonymous$$y bad. The code was a bit long so I didn't add it. I shortened it and edited it a little bit. Hope it's useful.
here's what i finally used, after i updated Chrizonic's code. thanks man
private Color GetAverageColor(List data) { float r = 0; float g = 0; float b = 0; int divider = data.Count;
for (int i = 0; i < divider; i++)
{
r += data[i].r;
g += data[i].g;
b += data[i].b;
}
return new Color(r / divider, g / divider, b / divider, 1);
}
Answer by shacharoz · Jul 14, 2013 at 08:17 AM
here's what i finally used, after i updated Chrizonic's code. thanks man
private Color GetAverageColor(List data) { float r = 0; float g = 0; float b = 0; int divider = data.Count;
for (int i = 0; i < divider; i++)
{
r += data[i].r;
g += data[i].g;
b += data[i].b;
}
return new Color(r / divider, g / divider, b / divider, 1);
}