最近在看 字符画 很流行, 就是一种将RGB 信息 转坏为灰度 按指定的比例显示不同的字符
效果
讲一下大致思虑
有2 种方案
第一种是 将图片扩大5倍 得到一个比自己大5 倍的图片
因为每个像素点是1px * 1px 一个字符是5px 所有 扩大5 倍
第二种 是 池化, 取最大数据进行 最池化 步频为2 字符串偏移的话用0 代替
第一种方案
","!",":","–",";","."];
$erzhiArray = array();
// echo json_encode($NN_RBG);die();
$im = imagecreatetruecolor($imagesize[0]*5,$imagesize[1]*5); //生成真彩图片
$black = imagecolorallocate($im,220,230,250);//设置颜色
$init_x = 35;
$init_y = 70;
$num = 1;
$R = imagecolorallocate($im,0,0,0);//设置颜色
imagefill($im,10,1,$black) ; //填充 // 从左上角开始填充灰色//背景
$RGB = imagecolorallocate($im,0,0,0);//设置颜色
$xx = 0;
$yy = 0;
foreach ($grayArray as $key => $value) {
$xx = $xx+6;
foreach ($value as $keys => $values) {
$yy = $yy+6;
// $RGB = imagecolorallocate($im,$RGBS[$key][$keys]['red'],$RGBS[$key][$keys]['green'],$RGBS[$key][$keys]['blue']);//设置颜色
imagettftext($im,7,0,$keys+$yy,$key+ $xx,$RGB,"ztc.ttf", $ico[$values]); //字体设置部分li
}
$yy = 0;
}
header("Content-type:image/png");//png格式
imagepng($im);//输出
imagedestroy($im);//释放内存
?>
放大后
因为是 *5 所以回比原图像大5 倍
第二种 我封装了个类 里面包括 卷积 神经网络 二值化 卷积核 RGBA 和一些平均算法
self::$imageinfo,'size'=>self::$imagesize);
}
public function Average($imageinfo){//平均值
foreach ($imageinfo as $key => $value) {
foreach ($value as $keys => $values) {
// $values['red'];
}
}
}
public function A($imageinfo,$imagesize=array(),$type=false,$ox=false,$type_xo=array(0,1)){//灰度
$temp =array();
$arr = array();
$num = 0;
$n_x = 0;
$n_num = 0;
if ($type) {
for($h=0; $h < $imagesize['1'] ; $h++) {
for ($w=0; $w < $imagesize['0'] ; $w++) {
self::$temp = imagecolorat($imageinfo,$w,$h);//获得某像素的颜色索引值
$arr = imagecolorsforindex($imageinfo,self::$temp);
$num = ($arr['red']+$arr['green']+$arr['blue'])/3;
if ($ox) {
if ($num>=127) {
$temp[$h][$w]= array('red'=>$type_xo[0],'green'=>$type_xo[0],'blue'=>$type_xo[0]);
}else{
$temp[$h][$w]= array('red'=>$type_xo[1],'green'=>$type_xo[1],'blue'=>$type_xo[1]);
}
}else{
$temp[$h][$w]= array('red'=>$num,'green'=>$num,'blue'=>$num);
}
}
}
}else{
$temp_arr = array();
foreach ($imageinfo as $key => $value) {
foreach ($value as $keys => $values) {
$num = ($values['red']+$values['green']+$values['blue'])/3;
// $num = $values['red']*0.3+$values['green']*0.59+$values['blue']*0.11;
$temp_arr[$key][$keys] = $num;
$n_num+=$num;
$n_x++;
}
}
$n_num = $n_num/$n_x;
self::$Average = $n_num ;
foreach ($imageinfo as $key => $value) {
foreach ($value as $keys => $values) {
// $num = ($values['red']+$values['green']+$values['blue'])/3;
$num = $temp_arr[$key][$keys];
// $num = $values['red']*0.3+$values['green']*0.59+$values['blue']*0.11;
// $n_num+=$num;
if ($ox) {
if ($num>=$n_num ) {
$temp[$key][$keys]= array('red'=>$type_xo[0],'green'=>$type_xo[0],'blue'=>$type_xo[0]);
}else{
$temp[$key][$keys]= array('red'=>$type_xo[1],'green'=>$type_xo[1],'blue'=>$type_xo[1]);
}
}else{
$temp[$key][$keys]= array('red'=>$num,'green'=>$num,'blue'=>$num);
}
}
}
}
return $temp;
}
private function __construct() {
// $file=''
// if (!file_exists($file)) {
// echo 'error file?';
// return 'error file?';
// }
// self::$imagesize = getimagesize($file);//获取文件大小
// self::$imageinfo = imagecreatefromjpeg($file);//获取图片
self::$kernel = array([-1, 0, 1, -2, 0, 2, -1, 0, 1],//0.Sobel算子 纵向边缘检测
[-1, -1, -1, -1, 9, -1, -1, -1, -1],//1.强化锐化滤波器Sharpness Filter
[-1, -1, -1, 0, 0, 0, 1, 1, 1],//2.// Prewitt算子 横向边缘检测
[-1, 0, 1, -1, 0, 1, -1, 0, 1],//3.// Prewitt算子 纵向边缘检测
[-2, -2, 0, -2, 6, 0, 0, 0, 0],//4.浮雕
[ 1, -1, 0, 0, 1, 1, 1, -1, 1],//5.多层卷积卷积核
[-1, 1, -1, -1, 1, 0, 0, -1, -1],//6.多层卷积卷积核
[ 1, -1, 1, 1,-1, 0, 1, 1, -1],//7.多层卷积卷积核
[ 1, 1, 0, -1, 0, 1, 1, -1, -1],//8.多层卷积卷积核
[ 1, 1, 0, 0,-1, 0, 1, -1, 0],//9.多层卷积卷积核
[ 0, 0, -1, -1,-1, 0, 0, -1, 0],//10.多层卷积卷积核
[ 0, 0, 0, 0, 7, 0, 0, 0, 0],//11.图暗部细节增强
[ 4, 0, 0, 0, 0, 0, 0, 0, -4],//12.边缘强化
[ 0, -1, 0, -1, 2, -1, 0, -1, 0],//13.锐化滤波器S
[ 1, 2.18, 1, 2.18, 4.77, 2.18, 1, 2.18, 1],//13.高斯滤波 /16
[ 1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 2, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1],//12.高斯模糊
[1,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,
0,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,1,] //4.卷积核 9X9 运动模糊
);
// $this->RGBA();
}
private function __clone() {//防止克隆
}
static function Obj($File){
if(!(self::$instance instanceof self)){
self::$instance = new self($File);
}
return self::$instance;
}
public function Fast_con($RGBA,$kernel,$type=true,$con=1,$size=array()){//快速卷积
$sqrt = sqrt(count($kernel));
$love = floor($sqrt/2);
$temps = array();
if ($size) {
$imagesize = $size;
}else{
$imagesize = self::$imagesize;
}
if (!$type) {
foreach ($RGBA as $X => $value) {
foreach ($value as $Y => $values) {
if ($X<$love || $X>($imagesize['1']-$love-1)) {//边界处理 top
if ($Y<$love || $Y>($imagesize['0']-$love-1)) { //卷积核左边处理
$temps[$X][$Y]=$RGBA[$X][$Y];
}else{
$temps[$X][$Y]=$RGBA[$X][$Y];
}
}else{
if ($Y<$love || $Y>($imagesize['0']-$love-1)) { //卷积核左边处理
$temps[$X][$Y]=$RGBA[$X][$Y];
}else{
$temp = 0;
$num = 0;
$tempa =array();
for ($i= $X-$love; $i <($X-$love)+$sqrt ; $i++) {
for ($s= $Y-$love; $s <($Y-$love)+$sqrt ; $s++) {
$temp+= $RGBA[$i][$s]['red']*$kernel[$num];
++$num;
}
}
$temp = abs($temp)/$con;
if ($temp>=255) {
$temp = 255;
}
$temps[$X][$Y]= array('blue'=>$temp,'green'=>$temp,'red'=>$temp);
}
}
}
}
}else {
foreach ($RGBA as $X => $value) {
foreach ($value as $Y => $values) {
if ($X<$love || $X>($imagesize['1']-$love-1)) {//边界处理 top
if ($Y<$love || $Y>($imagesize['0']-$love-1)) { //卷积核左边处理
$temps[$X][$Y]=$RGBA[$X][$Y];
}else{
$temps[$X][$Y]=$RGBA[$X][$Y];
}
}else{
if ($Y<$love || $Y>($imagesize['0']-$love-1)) { //卷积核左边处理
$temps[$X][$Y]=$RGBA[$X][$Y];
}else{
$temp = 0;
$temp1 = 0;
$temp2 = 0;
$num = 0;
$tempa =array();
for ($i= $X-$love; $i <($X-$love)+$sqrt ; $i++) {
for ($s= $Y-$love; $s <($Y-$love)+$sqrt ; $s++) {
$temp+= $RGBA[$i][$s]['red']*$kernel[$num];
$temp1+= $RGBA[$i][$s]['green']*$kernel[$num];
$temp2+= $RGBA[$i][$s]['blue']*$kernel[$num];
++$num;
}
}
$temp = abs($temp)/$con;
if ($temp>=255) {
$temp = 255;
}
$tempa['red'] = $temp;
$temp = abs($temp1)/$con;
if ($temp>=255) {
$temp = 255;
}
$tempa['green'] = $temp;
$temp = abs($temp2)/$con;
if ($temp>=255) {
$temp = 255;
}
$tempa['blue'] = $temp;
$temps[$X][$Y]=$tempa;
}
}
}
}
}
return $temps;
}
public function cons($RGBA,$kernel,$size=array()){//多层卷积
$sqrt = sqrt(count($kernel['red']));
$love = floor($sqrt/2);
$temps = array();
if ($size) {
$imagesize = $size;
}else{
$imagesize = self::$imagesize;
}
foreach ($RGBA as $X => $value) {
foreach ($value as $Y => $values) {
if ($X<$love || $X>($imagesize['1']-$love-1)) {//边界处理 top
if ($Y<$love || $Y>($imagesize['0']-$love-1)) { //卷积核左边处理
$temps[$X][$Y]=$RGBA[$X][$Y];
}else{
$temps[$X][$Y]=$RGBA[$X][$Y];
}
}else{
if ($Y<$love || $Y>($imagesize['0']-$love-1)) { //卷积核左边处理
$temps[$X][$Y]=$RGBA[$X][$Y];
}else{
$temp = 0;
$temp1 = 0;
$temp2 = 0;
$num = 0;
$tempa =array();
for ($i= $X-$love; $i <($X-$love)+$sqrt ; $i++) {
for ($s= $Y-$love; $s <($Y-$love)+$sqrt ; $s++) {
$temp+= $RGBA[$i][$s]['red']*$kernel['red'][$num];
$temp1+= $RGBA[$i][$s]['green']*$kernel['green'][$num];
$temp2+= $RGBA[$i][$s]['blue']*$kernel['blue'][$num];
++$num;
}
}
$temp = abs($temp);
if ($temp>=255) {
$temp = 255;
}
$tempa['red'] = $temp;
$temp = abs($temp1);
if ($temp>=255) {
$temp = 255;
}
$tempa['green'] = $temp;
$temp = abs($temp2);
if ($temp>=255) {
$temp = 255;
}
$tempa['blue'] = $temp;
$temps[$X][$Y]=$tempa;
}
}
}
}
return $temps;
}
public function Ikeka($RGB,$size=1,$imagesize,$type=true){//池化
$temps = array();
$x = 0;
$y = 0;
$max = 0;
$max1 = 0;
$max2 = 0;
$ys = 0;
for ($i=0; $i < $imagesize[1] ; $i++) {
// $x++;
if ($i<=$imagesize[1]-$size-1) {
for ($s=0; $s < $imagesize[0]-$size; $s++) {
// echo "
";
if ($s<=$imagesize[0]-$size-1) {
for ($ii=$i; $ii <= $i+$size ; $ii++) {
for ($is=$s; $is <= $s+$size ; $is++) {
if ($max < $RGB[$ii][$is]['red']) {
$max = $RGB[$ii][$is]['red'];
}
if ($max1 < $RGB[$ii][$is]['green']) {
$max1 = $RGB[$ii][$is]['green'];
}
if ($max2 < $RGB[$ii][$is]['blue']) {
$max2 = $RGB[$ii][$is]['blue'];
}
}
}
if ($type) {
$temps['rgb'][$x][$y]=array('red'=>$max,'green'=>$max1,'blue'=>$max2);
}else{
$temps['rgb'][$i][$s]=array('red'=>$max,'green'=>$max1,'blue'=>$max2);
}
$max = 0;
$max1 = 0;
$max2 = 0;
$s+=$size;
$y++;
}
}
$x++;
$ys=$y;
$y=0;
$i+=$size;
}
}
$temps['size'] = array($ys,$x);
return $temps;
}
public function OX_01($RGB,$imagesize,$size=2,$nums=1.8){//深度二值化自适应
$temps = array();
$x = 0;
$y = 0;
$max = 0;
$min = 0;
$num = 0;
$ys = 0;
for ($i=0; $i < $imagesize[1] ; $i++) {
// $x++;
if ($i<=$imagesize[1]-$size-1) {
for ($s=0; $s < $imagesize[0]-$size; $s++) {
// echo "
";
if ($s<=$imagesize[0]-$size-1) {
for ($ii=$i; $ii <= $i+$size ; $ii++) {
for ($is=$s; $is <= $s+$size ; $is++) {
$num += $RGB[$ii][$is]['red'];
if ($max < $RGB[$ii][$is]['red']) {
$max = $RGB[$ii][$is]['red'];
}
if ($min > $RGB[$ii][$is]['red']) {
$min = $RGB[$ii][$is]['red'];
}
}
}
$ys = (( $min+$max)/2)*$nums;
for ($ii=$i; $ii <= $i+$size ; $ii++) {
for ($is=$s; $is <= $s+$size ; $is++) {
if ($RGB[$ii][$is]['red']<=$ys) {
$temps[$ii][$is]=array('red'=>0,'green'=>0,'blue'=>0);
}else{
$temps[$ii][$is]=array('red'=>255,'green'=>255,'blue'=>255);
}
}
}
$max = 0;
$min = 0;
$num = 0;
$s+=$size;
$y++;
}
}
$x++;
$y=0;
$i+=$size;
}
}
return $temps;
}
public function OX_02($RGB,$imagesize,$size=0){
$temps = array();
$x = 0;
$y = 0;
$max = 0;
$min = 255;
$num = 0;
$ys = 0;
for ($i=0; $i < $imagesize[1] ; $i++) {
// $x++;
if ($i<=$imagesize[1]-$size-1) {
for ($s=0; $s < $imagesize[0]-$size; $s++) {
// echo "
";
if ($s<=$imagesize[0]-$size-1) {
for ($ii=$i; $ii <= $i+$size ; $ii++) {
for ($is=$s; $is <= $s+$size ; $is++) {
$num += $RGB[$ii][$is]['red'];
if ($max < $RGB[$ii][$is]['red']) {
$max = $RGB[$ii][$is]['red'];
}
if ($min > $RGB[$ii][$is]['red']) {
$min = $RGB[$ii][$is]['red'];
}
}
}
// echo $min;die();
$ys = ($max)/2;
for ($ii=$i; $ii <= $i+$size ; $ii++) {
for ($is=$s; $is <= $s+$size ; $is++) {
if ($RGB[$ii][$is]['red']>=$ys+40 ) {
$temps[$ii][$is]=array('red'=>0,'green'=>0,'blue'=>0);
}else{
$temps[$ii][$is]=array('red'=>255,'green'=>255,'blue'=>255);
}
}
}
$max = 0;
$min = 255;
$num = 0;
$s+=$size;
$y++;
}
}
$x++;
$y=0;
$i+=$size;
}
}
return $temps;
}
public function OX_fd(){
}
}
// $Obj = ImageFd::Obj("10.jpg");
// print_r($Obj::$kernel[0]);
// $Obj::RGBA(true);
// $data = json_decode(file_get_contents("face"),true);
// $data = $Obj->Fast_con($Obj::$RGBA,$Obj::$kernel[0],5,1,$data['size']);
// print_r(count( $data));
// $data = $Obj->Ikeka($Obj::$RGBA,1,$Obj::$imagesize,false)['rgb'];
// $data = $Obj->cons($data['rgb'],array('red'=>$Obj::$kernel[2],'green'=>$Obj::$kernel[0],'blue'=>$Obj::$kernel[3]),$data['size']);
// die();
// $data = $Obj->cons($Obj::$RGBA,array('red'=>$Obj::$kernel[7],'green'=>$Obj::$kernel[7],'blue'=>$Obj::$kernel[7]));
// $data = $Obj->cons($Obj::$RGBA,array('red'=>$Obj::$kernel[0],'green'=>$Obj::$kernel[2],'blue'=>$Obj::$kernel[3]));
// $data = $Obj->Fast_con($data,$Obj::$kernel[11],true,3);
// $data = $Obj->cons($data,array('red'=>$Obj::$kernel[0],'green'=>$Obj::$kernel[2],'blue'=>$Obj::$kernel[3]));
// print_r($Obj->RGBA(true));die();
// $data = $Obj->Fast_con($Obj::$RGBA,$Obj::$kernel[12]);
// $data = $Obj->OX_02($data,$Obj::$imagesize);
// $data = $Obj->A($Obj::$RGBA,$Obj::$imagesize,false,true,array(255,0));
// $data = $Obj->Fast_con($data,$Obj::$kernel[13],true,16);
// $data = $Obj->Fast_con($data,$Obj::$kernel[0],false);
// $data = $Obj->A($data);
// $im = imagecreatetruecolor(800,800); //生成真彩图片
// $black = imagecolorallocate($im,220,230,250);//设置颜色
// imagefill($im,10,1,$black) ;
// foreach ($data as $key => $value) {
// foreach ($value as $keys => $values) {
// $R = imagecolorallocate($im,$values['red'],$values['green'],$values['blue']);//设置颜色
// imagesetpixel($im,$keys,$key, $R);//绘制图案
// }
// }
// foreach ($data as $key => $value) {
// foreach ($value as $keys => $values) {
// $R = imagecolorallocate($im,$values['red'],$values['red'],$values['red']);//设置颜色
// imagesetpixel($im,$keys+180,$key, $R);//绘制图案
// }
// }
// foreach ($data as $key => $value) {
// foreach ($value as $keys => $values) {
// $R = imagecolorallocate($im,$values['green'],$values['green'],$values['green']);//设置颜色
// imagesetpixel($im,$keys+360,$key, $R);//绘制图案
// }
// }
// foreach ($data as $key => $value) {
// foreach ($value as $keys => $values) {
// $R = imagecolorallocate($im,$values['blue'],$values['blue'],$values['blue']);//设置颜色
// imagesetpixel($im,$keys+540,$key, $R);//绘制图案
// }
// }
// header("Content-type:image/png");//png格式
// imagepng($im);//输出
// imagedestroy($im);//释放内存
?>