Foros del Web » Programando para Internet » PHP »

¿como remover bordes negros en capturas de videos?

Estas en el tema de ¿como remover bordes negros en capturas de videos? en el foro de PHP en Foros del Web. Hola, me gustaría saber como puedo remover los bordes negros de las imágenes que son capturas de vídeo (ej. http://i.ytimg.com/vi/U0CGsw6h60k/0.jpg ), todo automáticamente con PHP, ...
  #1 (permalink)  
Antiguo 23/11/2010, 14:37
Avatar de xcorpion  
Fecha de Ingreso: octubre-2003
Ubicación: m é x i c o
Mensajes: 676
Antigüedad: 20 años, 6 meses
Puntos: 4
¿como remover bordes negros en capturas de videos?

Hola, me gustaría saber como puedo remover los bordes negros de las imágenes que son capturas de vídeo (ej. http://i.ytimg.com/vi/U0CGsw6h60k/0.jpg), todo automáticamente con PHP, ¿pueden ayudarme por favor?
  #2 (permalink)  
Antiguo 23/11/2010, 15:16
 
Fecha de Ingreso: agosto-2010
Ubicación: Tenerife
Mensajes: 893
Antigüedad: 13 años, 8 meses
Puntos: 202
Respuesta: ¿como remover bordes negros en capturas de videos?

Podrías recortar la imagen usando una librería o bien con la función imagecopyresampled()

Después si quieres puedes redimensionarla usando otras funciones del GD

Un saludo :)

EDITO: acabo de ver que hay ejemplos de recortar imágenes en las Faqs y en otros post.
__________________
Pensaba que internet era una gran biblioteca de sabiduría, hasta que comprendí que un libro no puede tener mil páginas llenas de faltas de ortografía... :(
  #3 (permalink)  
Antiguo 23/11/2010, 15:53
Avatar de 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?

Gracias por responder, si conocía esa función el problema seria mas bien calcular hasta donde llega el borde negro
  #4 (permalink)  
Antiguo 23/11/2010, 16:05
Avatar de 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?

Se me ocurre que podría tomar pixeles de muestra en cada fila y ver si todas coinciden con un color cercano al negro, en el momento que el patrón desaparece entonces se encuentra una fila de pixeles que no es un borde. Voy a intentar programarlo, si alguien tiene ideas agradeceré mucho que las compartan, saludos.
  #5 (permalink)  
Antiguo 23/11/2010, 16:39
Avatar de 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?

Valla, sin querer queriendo encontre en php.net una función que calcula el tamaño del borde negro, si les sirve aca esta: http://www.php.net/manual/en/function.imagecolorat.php

Código PHP:
<?php
    define
("DEBUG_OUT",TRUE);
    
$border_size find_border_size($path);
    
print_r($border_size);
    
/*
     * $border = max(find_border_size("img.jpg"));
     * $thumb_size_x = 180+(2*$border);
     * $thumb_size_y = 240+(2*$border);
     * $thumb_size = $thumb_size_x . "x" . $thumb_size_y; (String to use in the -s param of ffmpeg)
     * $crop_cmd = "-croptop $border -cropbottom $border -cropright $border -cropleft $border";
     */
    
function find_border_size($path)
    {
        
/* 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) */
        
$center_width ceil($width/2);
        for(
$i=0$i<$height$i++)
        {
            
$rgb imagecolorat($im,$center_width,$i);
            
$r = ($rgb >> 16) & 0xFF;
            
$g = ($rgb >> 8) & 0xFF;
            
$b $rgb 0xFF;
            if(
DEBUG_OUT) echo("Height: ($center_width,$i) R: $r / G: $g / B: $b\n");
            if(
$r >= $pad || $g >= $pad || $b >= $pad)
            {
                
$border_y $i;
                if(
$border_y == $height$border_y 0;
                break;
            }
        }

        
/* Border Width(X) */
        
$center_height ceil($height/2);
        for(
$i=0$i<$width$i++)
        {
            
$rgb imagecolorat($im,$i,$center_height);
            
$r = ($rgb >> 16) & 0xFF;
            
$g = ($rgb >> 8) & 0xFF;
            
$b $rgb 0xFF;
            if(
DEBUG_OUT) echo("Width: ($i,$center_width) R: $r / G: $g / B: $b\n");
            if(
$r >= $pad || $g >= $pad || $b >= $pad)
            {
                
$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");
        return array(
$border_x,$border_y);
    }
?>
  #6 (permalink)  
Antiguo 23/11/2010, 22:01
Avatar de 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);
    }

?>

Etiquetas: bordes, negros, remover, video, capturar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:00.