图片灰度和二值化处理
<?php
function adaptiveThreshold($file){
if(!$ex = getimagesize($file)){
return false;
}
// 打开图片
switch($ex[2]){
case IMAGETYPE_JPEG:
case IMAGETYPE_JPEG2000:
if(!$im = imageCreateFromJpeg($file)){
return false;
}
break;
case IMAGETYPE_PNG:
$im = imageCreateFromPng($file);
break;
case IMAGETYPE_GIF:
$im = imageCreateFromGif($file);
break;
case IMAGETYPE_BMP:
$im = imageCreateFromBmp($file);
break;
default :
return false;
}
$gray = array_fill(0, $ex[1],
array_fill(0, $ex[0], 0)
);
// 转为灰阶图像
foreach($gray as $y => &$row){
foreach($row as $x => &$Y){
$rgb = imagecolorat($im, $x, $y);
// 根据颜色求亮度
$B = $rgb & 255;
$G = ($rgb >> 8) & 255;
$R = ($rgb >> 16) & 255;
$Y = ($R * 19595 + $G * 38469 + $B * 7472) >> 16;
}
}
unset($row, $Y);
// 自动求域值
$back = 127;
do{
$crux = $back;
$s = $b = $l = $I = 0;
foreach($gray as $row){
foreach($row as $Y){
if($Y > 1;
}while($crux != $back);
// 二值化
$bin = $gray;
foreach($bin as &$row){
foreach($row as &$Y){
$Y = $Y $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] <p> </p><p> 以上就是图片灰度和二值化处理的内容,更多相关内容请关注PHP中文网(www.php.cn)! </p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/ai/2302" title="GenStore"><img
src="https://img.php.cn/upload/ai_manual/001/246/273/175764728740860.png" alt="GenStore" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/ai/2302" title="GenStore" class="overflowclass">GenStore</a>
<p class="overflowclass">一款AI开发辅助工具,主要用于AI对话生成在线商店,一个平台满足所有电商需求,适合需要提升相关任务效率的用户。</p>
</div>
<a rel="nofollow" href="/ai/2302" title="GenStore" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><p>
</p>










