上传一张情侣头像,我们会识别出两张个人的头像并展示给您。
process.php
php
'没有上传图像或图像上传失败。']);exit;
}$image = $_FILES['image'];
$filename = $image['tmp_name'];// 使用 OpenCV 加载图像
$image = cvLoadImage($filename);// 将图像缩放到 256x256,使用边缘检测
$image = cvResize($image, cvSize(256, 256));
$edges = cvCanny($image, 100, 200);// 使用轮廓检测识别头像
$contours = cvFindContours($edges);// 存储头像图像的数组
$headshots = [];// 遍历轮廓并裁剪出头像
foreach ($contours as $contour) {// 计算轮廓的矩形边界$rect = cvBoundingRect($contour);// 确保矩形边界在图像内if ($rect['x'] >= 0 && $rect['y'] >= 0 && $rect['x'] + $rect['width'] <= $image->width &&$rect['y'] + $rect['height'] <= $image->height) {// 裁剪出头像图像$headshot = cvGetSubRect($image, $rect);// 调整图像大小以适合 128x128$headshot = cvResize($headshot, cvSize(128, 128));// 将图像添加到数组中$headshots[] = $headshot;}
}// 将头像图像保存到文件中
$headshots_filenames = [];
foreach ($headshots as $headshot) {$filename = uniqid() . '.jpg';cvSaveImage($filename, $headshot);$headshots_filenames[] = $filename;
}// 清除资源
cvReleaseImage($image);
cvReleaseImage($edges);// 返回头像图像的文件名
echo json_encode(['results' => $headshots_filenames]);