Grayscale and binarization processing of images
Grayscale and binarization processing of images
& $ Row) {foreach ($ row as $ x =>&$ Y) {$ rgb = imagecolorat ($ im, $ x, $ y ); // calculate the brightness based on the color $ B = $ rgb & 255; $ G = ($ rgb> 8) & 255; $ R = ($ rgb> 16) & 255; $ Y = ($ R * 19595 + $ G * 38469 + $ B * 7472)> 16 ;}} unset ($ row, $ Y ); // automatically calculates the domain value $ back = 127; do {$ crux = $ back; $ s = $ B = $ l = $ I = 0; foreach ($ gray as $ row) {foreach ($ row as $ Y) {if ($ Y <$ crux) {$ s + = $ Y; $ l ++ ;} else {$ B + = $ Y; $ I ++ ;}}$ s = $ l? Floor ($ s/$ l): 0; $ B = $ I? Floor ($ B/$ I): 0; $ back = ($ s + $ B)> 1;} while ($ crux! = $ Back); // binarization $ bin = $ gray; foreach ($ bin as & $ row) {foreach ($ row as & $ Y) {$ Y = $ Y <$ crux? 0: 1 ;}} return array ($ gray, $ bin,) ;}if (empty ($ _ GET ["img"]) {exit ("Please use $ _ SERVER [SCRIPT_NAME]? Img = local/image/path ");} if (! $ Im = adaptiveThreshold ($ _ GET ["img"]) {exit ("error! ") ;}$ Img = imagecreate (count ($ im [0] [0]), count ($ im [0]) * 2 ); $ rgb = array (imagecolorallocate ($ img, 0, 0), imagecolorallocate ($ img, 255,255,255),); $ x = $ y = 0; $ colors = array (0 => $ rgb [0], 255 => $ rgb [1]); foreach ($ im [0] as $ row) {do {if (isset ($ colors [$ row [$ x]) {$ c = $ colors [$ row [$ x];} else {$ c = $ colors [$ row [$ x] = imagecolorallocate ($ img, $ row [$ x], $ row [$ x], $ row [$ x]);} // imagesetpixel ($ Img, $ x, $ y, $ rgb [$ row [$ x] <128? 0: 1]); imagesetpixel ($ img, $ x, $ y, $ c);} while (isset ($ row [++ $ x]); $ x = 0; $ y ++;} foreach ($ im [1] as $ row) {do {imagesetpixel ($ img, $ x, $ y, $ rgb [$ row [$ x]);} while (isset ($ row [++ $ x]); $ x = 0; $ y ++ ;} header ("Content-Type: image/gif"); imagegif ($ img );
The above is the gray and binarization processing content of the image. For more information, see The PHP Chinese website (www.php1.cn )!