首页 技术 正文
技术 2022年11月20日
0 收藏 459 点赞 2,409 浏览 1992 个字

最近期末考试考完了,我们也要放寒假了。于是突发奇想,想用PHP写一个答题卡识别程序。已经实现了一些,现分享给大家。

具体的步骤如下:

上传答题卡=>图片二值化(已实现)=>寻找定位点(已实现)=>使用定位点切割掉不要的部分(已实现)=>切割小题=>客观题自动阅卷&主观题切割后交由阅卷老师批改=>统计分数=>生成csv文档

先爆出源码:

 <?php
error_reporting(0);
$fn="./1.jpg";//要识别的答题卡文件名,生产环境中替换为"$fn='./cards/'.$_GET['testno']."/".$_GET['cardno'];"
$m255=200;//图片二值化的阈值
$minx=-1;//定位点坐标,L6同。
$miny=-1;
function gett($res,$i,$j){//求某座标的灰度值
$m255=200;
$rgb = imagecolorat($res,$j,$i); $rgbarray = imagecolorsforindex($res, $rgb);
$r= $rgbarray['red'] * 0.333;
$g= $rgbarray['green'] * 0.333;
$b= $rgbarray['blue'] * 0.333;
$t= round(($r+$g+$b) /$m255);
return $t;
} header('Content-type:image/png');
$res = imagecreatefromjpeg($fn);
$size = getimagesize($fn);
$black = imagecolorallocate($res, 0, 0, 0);
$white = imagecolorallocate($res, 255, 255, 255);
$red=imagecolorallocate($res, 255, 0, 0);
$fl=imagecolorallocate($res, 0, 255, 0);
for($j=0; $j <$size[0]; ++$j)
for($i=0; $i <$size[1]; ++$i)
{
//这一部分代码的Line26-Line35和Line36-Line49借鉴的网上的一位大大的文章,稍作修改,具体是哪位大大的,不太记得了,这一行献给那位大大
{
$rgb = imagecolorat($res,$j,$i);
$rgbarray = imagecolorsforindex($res, $rgb);
$r= $rgbarray['red']*0.333;
$g= $rgbarray['green']*0.333;
$b= $rgbarray['blue']*0.333;
$t= round(($r+$g+$b) /$m255);
if ($t==0)
{
imagesetpixel($res,$j,$i,$black);//原本是$data[$i][$j]=true;因为内存超限,so换成了这个,L47的一样.
if($minx==-1 and $miny==-1 and $i>=10 and $j>=10 and gett($res,$i+5,$j)==0 and gett($res,$i,$j+5)==0){//gett($res,$i+5,$j)==0 and gett($res,$i,$j+5)==0:防止误识别.
//找到左上角的定位点
$minx=$j;
$miny=$i; }
}else{ imagesetpixel($res,$j,$i,$white);
}
}
} //$b=dgcz($res,$miny,$minx,$fl);//这个函数本是用于标记定位点的,后来取消了
imageellipse($res,$minx,$miny,40,40,$red);//标记定位点
//imageline($res,$minx,0,$minx,$size[1],$red);
//imageline($res,$size[0]-$minx,0,$size[0]-$minx,$size[1]-1,$red);
//画切割线 //imageellipse($res,$b-minx,$miny,40,40,$red); $Out = imagecreatetruecolor ($size[0]-2*$minx,$size[1]-2*$miny);//切割后图像存放位置
imagecopy ( $Out,$res , 0,0 ,$minx ,$miny ,$size[0]-2*$minx,$size[1]-2*$miny );//切割
imagepng($Out);
imagedestroy($res);
imagedestroy($Out);
//$fh=fopen("./log.log","a");
//fwrite($fh,"x:".$minx.";y:".$miny."\n");
?>

但效率有点低。需要二十多秒才能完成。

测试图片:

浅谈PHP答题卡识别(一)

执行结果:

浅谈PHP答题卡识别(一)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849