Ver Mensaje Individual
  #6 (permalink)  
Antiguo 23/11/2010, 22:01
Avatar de xcorpion
xcorpion
 
Fecha de Ingreso: octubre-2003
Ubicación: m é x i c o
Mensajes: 676
Antigüedad: 20 años, 6 meses
Puntos: 4
Respuesta: ¿como remover bordes negros en capturas de videos?

Hice algunas correcciones al código y algunas mejoras, pero todavia puede optimizarse:

Código PHP:
<?
function trim_borders($path,$path2){

    
/* The pad var is essentially a 'feather' value. Unless one of the RGB values rises
    * above $pad we are saying to continue. You can try different values but with 10 I
    * would still get a decent sized border due to the bleedover of the movie into
    * the hat.
    */
    
$pad 20;
    
$border_y 0;
    
$border_x 0;

    if(!
file_exists($path))    {
        if(
DEBUG_OUT) echo("Error: $path not found.\n");
        return 
FALSE;
    }else{
        
//if(DEBUG_OUT) echo("Opening: $path ...\n");
    
}

    
$im = @imagecreatefromjpeg($path);
    if(!
$im) return FALSE;
    
$height imagesy($im);
    
$width imagesx($im);

    
/* Let's start at 0, 0 and keep going till we hit a color */
    //if(DEBUG_OUT) echo("Image - Height: $height / Width: $width\n");
    /* Border Height(Y) */
    
$spacebetsamp ceil($width/6);
    
$sample1 $spacebetsamp;
    
$sample2 $spacebetsamp*2;
    
$sample3 $spacebetsamp*3;
    
$sample4 $spacebetsamp*4;
    
$sample5 $spacebetsamp*5;

    for(
$i=0$i<$height$i++){
        
$rgb1 imagecolorat($im,$sample1,$i);
        
$r1 = ($rgb1 >> 16) & 0xFF;
        
$g1 = ($rgb1 >> 8) & 0xFF;
        
$b1 $rgb1 0xFF;
        
$rgb2 imagecolorat($im,$sample2,$i);
        
$r2 = ($rgb2 >> 16) & 0xFF;
        
$g2 = ($rgb2 >> 8) & 0xFF;
        
$b2 $rgb2 0xFF;
        
$rgb3 imagecolorat($im,$sample3,$i);
        
$r3 = ($rgb3 >> 16) & 0xFF;
        
$g3 = ($rgb3 >> 8) & 0xFF;
        
$b3 $rgb3 0xFF;
        
$rgb1 imagecolorat($im,$sample4,$i);
        
$r4 = ($rgb4 >> 16) & 0xFF;
        
$g4 = ($rgb4 >> 8) & 0xFF;
        
$b4 $rgb4 0xFF;
        
$rgb5 imagecolorat($im,$sample5,$i);
        
$r5 = ($rgb5 >> 16) & 0xFF;
        
$g5 = ($rgb5 >> 8) & 0xFF;
        
$b5 $rgb5 0xFF;

        
$blackpixels=0;
        if(
$r1 $pad and $g1 $pad and $b1 $pad$blackpixels++;
        if(
$r2 $pad and $g2 $pad and $b2 $pad$blackpixels++;
        if(
$r3 $pad and $g3 $pad and $b3 $pad$blackpixels++;
        if(
$r4 $pad and $g4 $pad and $b4 $pad$blackpixels++;
        if(
$r5 $pad and $g5 $pad and $b5 $pad$blackpixels++;

        if(
$blackpixels != 5){
            
$border_y $i;
            if(
$border_y == $height$border_y 0;
            break;
        }else{
            
//echo "blackpixel: s1($r1,$g1,$b1); s2($r2,$g2,$b2); s3($r3,$g3,$b3); s4($r4,$g4,$b4);s5($r5,$g5,$b5);<br>";
        
}
    }

    
/* Border Width(X) */
    
$spacebetsamp ceil($height/6);
    
$sample1 $spacebetsamp;
    
$sample2 $spacebetsamp*2;
    
$sample3 $spacebetsamp*3;
    
$sample4 $spacebetsamp*4;
    
$sample5 $spacebetsamp*5;

    for(
$i=0$i<$width$i++){
        
$rgb1 imagecolorat($im,$i,$sample1);
        
$r1 = ($rgb1 >> 16) & 0xFF;
        
$g1 = ($rgb1 >> 8) & 0xFF;
        
$b1 $rgb1 0xFF;
        
$rgb2 imagecolorat($im,$i,$sample2);
        
$r2 = ($rgb2 >> 16) & 0xFF;
        
$g2 = ($rgb2 >> 8) & 0xFF;
        
$b2 $rgb2 0xFF;
        
$rgb3 imagecolorat($im,$i,$sample3);
        
$r3 = ($rgb3 >> 16) & 0xFF;
        
$g3 = ($rgb3 >> 8) & 0xFF;
        
$b3 $rgb3 0xFF;
        
$rgb4 imagecolorat($im,$i,$sample4);
        
$r4 = ($rgb4 >> 16) & 0xFF;
        
$g4 = ($rgb4 >> 8) & 0xFF;
        
$b4 $rgb4 0xFF;
        
$rgb5 imagecolorat($im,$i,$sample5);
        
$r5 = ($rgb5 >> 16) & 0xFF;
        
$g5 = ($rgb5 >> 8) & 0xFF;
        
$b5 $rgb5 0xFF;

        
$blackpixels=0;
        if(
$r1 $pad and $g1 $pad and $b1 $pad$blackpixels++;
        if(
$r2 $pad and $g2 $pad and $b2 $pad$blackpixels++;
        if(
$r3 $pad and $g3 $pad and $b3 $pad$blackpixels++;
        if(
$r4 $pad and $g4 $pad and $b4 $pad$blackpixels++;
        if(
$r5 $pad and $g5 $pad and $b5 $pad$blackpixels++;

        if(
$blackpixels != 5){
            
$border_x $i;
            if(
$border_x == $width$border_x 0;
            break;
        }
    }

    
/* I am making the border a multiple of 2 since I am sending these values to FFMpeg */
    
if($border_x != 0){
        
$border_x /= 2;
        
$border_x round($border_x);
        
$border_x *= 2;
    }
    if(
$border_y != 0){
        
$border_y /= 2;
        
$border_y round($border_y);
        
$border_y *= 2;
    }
    
//if(DEBUG_OUT) echo("Border Width: $border_x / Border Height: $border_y\n");

    
if($border_x+$border_y >0){
        if(
$border_x){
            
$sx $border_x+1;
            
$dw $width-($border_x*2);
        }else{
            
$sx =0;
            
$dw =$width;
        }
        if(
$border_y){
            
$sy $border_y+1;
            
$dh $height-($border_y*2);
        }else{
            
$sy =0;
            
$dh $height;
        }
        
$dest imagecreatetruecolor($dw,$dh);
        
// Copy
        
imagecopy($dest$im00$sx$sy$dw$dh);
        
imagejpeg($dest,$path2);
    }

    return array(
$border_x,$border_y);
    }

?>