Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Need help with optimize script(like autocrop) and sampleimage

Explorer ,
Feb 24, 2014 Feb 24, 2014

Hi guys, i working on script(something like autocrop script), i already done trimming precomp but this process to find rgb pixels is to slow(autocrop work insanity fast compared with my script), need your help with optimization

i creat precomp and then in loop sherch on it rgb....

code

there are a loop code with process of search rgb:

var xmin = myComp.width, xmax = 0, ymin = myComp.height, ymax = 0;

               for(var y=1; y<=myComp.height; y++)

               {

                     for(var x=1; x<=myComp.width; x++)

                                        {

                                            myControlNull.property("Transform").property("Position").setValue([x,y]);   //send to AE expression x y coordinates

                                             if(out_checker_one_zero.slider.value==1) //get back 1 if sampleimage find rgb

                                            {

                                                if (x < xmin)  //checking for finding xmin

                                                    {

                                                            xmin = x;

                                                    }

                                                if (x > xmax) //checking for finding xmax

                                                    {

                                                            xmax = x;

                                                    }

                                                if (y < ymin) //checking for finding ymin

                                                    {

                                                            ymin = y;

                                                    }

                                                if (y > ymax)  //checking for finding ymax

                                                    {

                                                            ymax = y;

                                                    }

                                            }

                                            progBar1.value=x;

                                            progBar2.value=y;

                                            progressBar_palette.update();

                                            //return  progressBar_palette;

                                        }

               }

                alert("maxX "+ xmax+ "   minX " + xmin + "   maxY "  + ymax + "  minY "+ ymin+"\r");

expression

//expression set on slider control

                                           /*

sourceLayer = thisComp.layer("Black Solid 2_TRIM_SCRIPT");

area = [0.5,0.5];

NullsamplePoint=thisComp.layer("ZS_TRIM_COMP").transform.position;

color= sourceLayer.sampleImage(NullsamplePoint,area);

a=effect("Checker")("Slider");

if((parseInt(255*color[0]))>0)

{a=1;}

else if(parseInt(255*color[1])>0)

{a=1;}

else if(parseInt(255*color[2])>0)

{a=1;}

else if(parseInt(255*color[3])>0)

{a=1;}

else

{a=0;}*/

when i get this data i can crop my precomp to this x y coordinates, then i want render this comp and past image back to myComp and with replace my precomp (where search rgb )

TOPICS
Scripting
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 24, 2014 Feb 24, 2014

Hi Zipkiev,

I can't tell you about autocrop, but I wrote a similar function and had the same speed issue at the beginning.

The trick is to check RGBA for every n pixels, then if you hit the value you expect, check for n/2, etc...

The bigger n at the beginning, the faster. But make sure n is not bigger than your image's width.

So let's say you set n = 100, and you look for an alpha > 0.

1_check RGBA at x = 0, x = 100, x = 200... until alpha > 0 (let's say we find it for x = 300);

2_check again at x = 250; if (alpha > 0) check for x = 225 else check for x = 275...

and so on...

This type of function have a name that I can't remember :-), it's way more efficient than sampling every pixel.

Cheers,

François

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 26, 2014 Feb 26, 2014

thx for reply françois leroy, good idea, but its is only one method?  also i want to ask in final checking state u are used check on integer(for example x=560 y=570) pixels or float(x=560.12 y=570.08), and what radius in expression on sampleimage(i used [0.5 0.5], i think for more accuracy i need used 0.1 0.1?) u are using? just want find ideal performance/accuracy

also try ask, for now i move my sample area by coordinate null object position, but maybe  point control  give me more speed? u try it?

PS  can i some how set anchor point for changing size of my precomp? because it always change size from center and my layers inside this precomp always somewhere outside  precomp

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 26, 2014 Feb 26, 2014

Hi Zipkiev,

as François said, scanning your layer pixel by pixel is definitely not optimal.

This is necessary if the comp is 8 bit per channel because in 8bpc sampleImage is not very precise, but you can turn the comp in 16/32 bps and then scan with much larger bands with no error, then recursively rescan with smaller and smaller bands in smaller regions. You can get it to be approx 10x faster quite easily.

But i think the long execution time mostly comes from how your script is designed.

The temporary sampleImage expression should do absolutely everything, including the loop(s).

You don't need at all that null object, nor move it 1 million times.

1 million times .setValue[x,y] === "horror".

Together with sampleImage + the progressbar increments + window.update(), how much does that take to process ?

You could do something like this:

0- drop the comp into another comp, call it tempComp, same spec, but 16/32 bpc.

1- add a text layer to tempComp, and in the sourceText property put a box expression that returns the alpha box of 'comp' (seen as a layer of 'tempComp'), in the form xmin, xmax, ymin, ymax (string)

2- harvest the result (read the textLayer.sourceText.value)

3- delete tempComp

>>> you don't need the null, nor the .setValue, nor the progressbars.

If you don't know what a box expression may look like, have a look at this topic: http://www.aenhancers.com/viewtopic.php?f=6&t=931

It scans the layer pixel by pixel, so it can be improved, but i'm pretty sure doing this will still greatly improve the speed of your script.

Xavier.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 26, 2014 Feb 26, 2014

Hi!

My goal wasn't exactly the same than yours, so the method for you can be different.

I wrote my function to deal with large pictures, made in photoshop, with a layer called 'frame", with black outside and 0 alpha in the center. And I know before that the black outside is smaller than the empty alpha center.

So the fastest way for me was to scan from left to rigth until I find a null alpha. Then scan from right to left, Then the same from Top to bottom...

I used integers (I scale down only, so no need for real accuracy), and kept a radius of [.5,.5].

And yes, point control will definitely be faster...

For the 'composition's anchor point', I used a simple trick: create a null, set its position to the Top/Left corner of your crop' rectangle, parent every layer to it, crop, then set null's position to [0,0] and remove the null.

Of course, if you deal with layers that are already parented, you'll have to store your parenting architecture somewhere and re-apply it.

Xavier wrote: "The temporary sampleImage expression should do absolutely everything, including the loop(s)."

It's true, the less steps, the faster... but the expression can be trickier to write. 😉

Cheers,

François

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 01, 2014 Mar 01, 2014
LATEST

guys, thx for reply to all , try to do something with your advice...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines