Login  Register

Re: Cutting a black part around grey paper

Posted by Herbie on Sep 16, 2016; 5:49pm
URL: http://imagej.273.s1.nabble.com/Cutting-a-black-part-around-grey-paper-tp5017192p5017195.html

Richard,

here is an ImageJ-macro that should do what you want...

/////////////////////////////////////////////////////////
setBatchMode( true );
   
run( "Select All" );
projection = getProfile();
diffArray( projection );
extrema = Array.findMaxima( projection, 0 );
x = extrema[0];
extrema = Array.findMinima( projection, 0 );
width = extrema[0] - x;
x++;
   
setKeyDown( "alt" );
projection = getProfile();
diffArray( projection );
extrema = Array.findMaxima( projection, 0 );
y = extrema[0];
extrema = Array.findMinima( projection, 0 );
height = extrema[0] - y;
y++;
   
run("Select None");
makeRectangle( x, y, width, height );
run("Crop");

setBatchMode( false );
exit();

function diffArray( a ) {
    for ( i = 1; i < a.length; i++ ) {
      a[i-1] = a[i] - a[i-1];
    }
    a[a.length-1] = 0;
}
/////////////////////////////////////////////////////////

HTH

Herbie