PHP 图像处理识别答题卡:提取题目答案和特征
// Step 3: Threshold the image imagefilter($image, IMG_FILTER_CONTRAST, -255);
// Step 4: Convert the image to binary $threshold = 50;
for($x=0;$x<imagesx($image);$x++) { for($y=0;$y<imagesy($image);$y++) { $rgb = imagecolorat($image,$x,$y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF;
$gray = (int)($r+$g+$b)/3;
if($gray>$threshold)
$gray = 255;
else
$gray = 0;
$new_pxl = imagecolorallocate($image,$gray,$gray,$gray);
imagesetpixel($image,$x,$y,$new_pxl);
}
}
// Step 5: Detect edges $new_image = imagecreatetruecolor(imagesx($image), imagesy($image));
for($x=1;$x<imagesx($image)-1;$x++){ for($y=1;$y<imagesy($image)-1;$y++){ $rgb_array = array();
$rgb_array[] = imagecolorat($image,$x-1,$y-1);
$rgb_array[] = imagecolorat($image,$x-1,$y);
$rgb_array[] = imagecolorat($image,$x-1,$y+1);
$rgb_array[] = imagecolorat($image,$x,$y-1);
$rgb_array[] = imagecolorat($image,$x,$y);
$rgb_array[] = imagecolorat($image,$x,$y+1);
$rgb_array[] = imagecolorat($image,$x+1,$y-1);
$rgb_array[] = imagecolorat($image,$x+1,$y);
$rgb_array[] = imagecolorat($image,$x+1,$y+1);
$r_sum = 0;
$g_sum = 0;
$b_sum = 0;
foreach($rgb_array as $rgb){
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$r_sum += $r;
$g_sum += $g;
$b_sum += $b;
}
$r = (int)($r_sum/9);
$g = (int)($g_sum/9);
$b = (int)($b_sum/9);
$new_pxl = imagecolorallocate($new_image,$r,$g,$b);
imagesetpixel($new_image,$x,$y,$new_pxl);
}
}
// Step 6: Detect lines $lines = array();
for($x=0;$x<imagesx($new_image);$x++){ $rgb_array = array();
for($y=1;$y<imagesy($new_image);$y++){
$rgb_array[] = imagecolorat($new_image,$x,$y);
}
$r_sum = 0;
$g_sum = 0;
$b_sum = 0;
foreach($rgb_array as $rgb){
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$r_sum += $r;
$g_sum += $g;
$b_sum += $b;
}
$r = (int)($r_sum/(imagesy($new_image)-1));
$g = (int)($g_sum/(imagesy($new_image)-1));
$b = (int)($b_sum/(imagesy($new_image)-1));
if($r+$g+$b<717){
$lines[] = $x;
}
}
// Step 7: Extract questions and answers $questions = array(); $answers = array();
$start_line = 0;
foreach($lines as $line){ if($start_line == 0) $start_line = $line; else{ $width = $line-$start_line; $question = imagecreatetruecolor($width, imagesy($image)); imagecopy($question, $image, 0, 0, $start_line, 0, $width, imagesy($image));
// Step 8: Extract features
$black_pixels = 0;
for($x=0;$x<imagesx($question);$x++){
for($y=0;$y<imagesy($question);$y++){
$rgb = imagecolorat($question,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if($r+$g+$b<765)
$black_pixels++;
}
}
// Step 9: Classify question and answer
if($black_pixels<=100)
$answers[] = $question;
else
$questions[] = $question;
$start_line = $line;
}
}
// Step 10: Display the results echo 'Questions:'; foreach($questions as $question) imagejpeg($question, 'question.jpg');
echo 'Answers:'; foreach($answers as $answer) imagejpeg($answer, 'answer.jpg');
?>
原文地址: https://www.cveoy.top/t/topic/lj7G 著作权归作者所有。请勿转载和采集!