1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
<?php
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++Fonction binarise image
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function binariser($image,$seuil=128) {
$image = imagecreatefromjpeg($image);
// Récupère la largeur et la hauteur de l'image
$width = imagesx($image);
$height = imagesy($image);
// Parcourt tous les pixels de l'image
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
// Récupère la couleur du pixel à la position (x, y)
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
// Convertit la couleur en niveaux de gris
$gray = round(($r + $g + $b) / 3);
// Binarise le pixel en noir ou blanc en fonction du seuil
if ($gray < $seuil) {
$color = 0; // Noir
} else {
$color = 255; // Blanc
}
// Définit la couleur du pixel binarisé
imagesetpixel($image, $x, $y, imagecolorallocate($image, $color, $color, $color));
}
}
return $image;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++Fonction charger image
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function afficher($image) {
if($image){
// Affiche l'image binarisée dans le navigateur
if(!$_GET["debug"]==1) {header('Content-Type: image/jpeg');}
imagejpeg($image);
imagedestroy($image);
}else{
echo"Pas d'image a afficher";
}
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++Fonction test rectangle
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function rectangle_test($image,$pixel_x,$pixel_y,$rectangle_largeur,$rectangle_hauteur) {
//$rectangle_largeur=75;
//$rectangle_hauteur=15;
// Récupère la largeur et la hauteur de l'image
$width = imagesx($image);
$height = imagesy($image);
// compte combien de pixel blanc se trouve dans le rectangle (contour)
$nb_pixel_blanc_1 = 0; //horizontal premier
$nb_pixel_blanc_2 = 0; //horizontal 2 ieme
$nb_pixel_blanc_3 = 0; //vertical 1 premier
$nb_pixel_blanc_4 = 0; //vertical 2 ieme
//1ere ligne horizontale, y fixe
for($x = $pixel_x; $x < ($rectangle_largeur + $pixel_x); $x++){
$rgb1 = imagecolorat($image, $x, $pixel_y);
if ($rgb1 == 0) { $nb_pixel_blanc_1++; }
}
// 2 ieme ligne horizontale, y fixe
for($x = $pixel_x; $x < ($rectangle_largeur + $pixel_x); $x++){
$rgb2 = imagecolorat($image, $x, ($pixel_y + $rectangle_hauteur));
if($rgb2 == 0){ $nb_pixel_blanc_2++; }
}
//1 ere colonne, x fixe
for($y = $pixel_y; $y < ($rectangle_hauteur + $pixel_y); $y++){
$rgb3 = imagecolorat($image, $pixel_x, $y);
if($rgb3 == 0){ $nb_pixel_blanc_3++; }
}
//2 ieme colonne, x fixe
for($y = $pixel_y; $y < ($rectangle_hauteur + $pixel_y); $y++){
$rgb3 = imagecolorat($image, ($pixel_x + $rectangle_largeur), $y);
if($rgb4 == 0){ $nb_pixel_blanc_4++; }
}
$nb_pixel_blanc = $nb_pixel_blanc_1 + $nb_pixel_blanc_2 + $nb_pixel_blanc_3 + $nb_pixel_blanc_4;
return $nb_pixel_blanc;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++tracer le ou les rectangle
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function tracer_rectangle($image,$x,$y,$rectangle_largeur,$rectangle_hauteur) {
$color = "5CDF00";
$rouge = hexdec("FC"); // canal rouge
$vert = hexdec("2C"); // canal vert
$bleu = hexdec("0E"); // canal bleu
//on créé la couleur1 et on l'attribue à une variable
$couleur= imageColorAllocate($image, $rouge,$vert, $bleu);
//on crée le rectangle
imagerectangle($image,$x,$y,$x + $rectangle_largeur,$y + $rectangle_hauteur,$couleur);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++Detecter rectangle rempli de blanc ou presque tout blanc
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function compte_pixel_vide($image,$pixel_x,$pixel_y,$rectangle_largeur,$rectangle_hauteur) {
for($y=$pixel_y;$y<$pixel_y+$rectangle_hauteur;$y++){
for($x=$pixel_x;$x<$pixel_x+$rectangle_largeur;$x++){
$rgb = imagecolorat($image, $x, $y);
if($rgb == 0){ $nb_pixel_blanc++;}
}
}
return $nb_pixel_blanc;
}
?> |
Partager