1 Star 0 Fork 2

cyd/php-opencv

forked from 带刀医生/php-opencv 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
upscale_image_x2_by_dnn_waifu2x.php 1.18 KB
一键复制 编辑 原始数据 按行查看 历史
yangquanliang 提交于 2020-11-23 10:22 . 初始化php-opencv
<?php
use CV\Scalar;
use function CV\{imread, imwrite, cvtColor};
$imgname = 'qing2';
$filetype = '.png';
$imgfileName = $imgname.$filetype;
$src = imread("images/".$imgfileName); // opencv loads image to matrix with BGR order
$src = cvtColor($src, CV\COLOR_BGR2RGB); // convert BGR to RGB
\CV\copyMakeBorder($src, $src, 7, 7, 7, 7, 1); // add borders 7px
//var_export($src);
$blob = \CV\DNN\blobFromImage($src, 1, $src->size(), new Scalar()); // convert image to 4 dimensions matrix
//var_export($blob);
$blob = $blob->divide(255); // convert color values from 0-255 to 0-1
$net = \CV\DNN\readNetFromCaffe('models/waifu2x/scale2.0x_model.prototxt', 'models/waifu2x/scale2.0x_model.caffemodel');
$net->setInput($blob, "");
$r = $net->forward();
$r = $r->divide(1/255); // convert color values from 0-255 to 0-1
//var_export($r);
$mat = new \CV\Mat($r->shape[2], $r->shape[3], CV\CV_32FC3);
for ($i = 0; $i < $r->shape[2]; $i++) {
for ($j = 0; $j < $r->shape[3]; $j++) {
$mat->at($i, $j, 0, $r->atIdx([0,0,$i,$j])); //R
$mat->at($i, $j, 1, $r->atIdx([0,1,$i,$j])); //G
$mat->at($i, $j, 2, $r->atIdx([0,2,$i,$j])); //B
}
}
imwrite("results/".$imgfileName, $mat);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/imagys_88960345/php-opencv.git
git@gitee.com:imagys_88960345/php-opencv.git
imagys_88960345
php-opencv
php-opencv
master

搜索帮助