准备工作已经差不多了,现在进入正文。
暂时还没找到可用的双三次插值缩放的代码,不过对于SRCNN网络来说,并不包括放大部分,
只要网络输出的图像比输入的图像更清晰,分辨率更高就行。输出和输入大小是相同的。
仿照Matlab做好SRCNN外部框架:
// 超分辨率重建(卷积神经网络(SRCNN)) // // 设定参数:文件名、放大倍数 //c++实现卷积 #define USE_EGE 0 //选择ege (1)或 easyx(0)的开关 #if USE_EGE #include <ege.h> //使用ege库 和 easyx 差不多,这里没有实现 using namespace ege; #else #include <easyx.h>//使用easyx库 #include<conio.h> #endif #include <math.h> #include<iostream> #include<fstream> #include<vector> using namespace std; #define SCREEN_WIDTH 800 //窗口大小 #define SCREEN_HEIGHT 600 IMAGE jpg;//一张原图 void error(char *s) { printf("%s\n",s); getch(); exit(1); } void loadjpg(char * jpgname) { loadimage(&jpg,jpgname);// } //图像剪裁 void modcrop(IMAGE *imgs, int modulo) { //if size(imgs,3)==1 //单通道 int height = imgs->getheight(); int width = imgs->getwidth(); int w = width - width % modulo;//去除零头 int h = height - height % modulo; IMAGE tmp = *imgs;//复制原图像,; //图像剪裁为 imgs->Resize(w,h); SetWorkingImage(&tmp); getimage(imgs, 0, 0, w, h);//拷贝至新图 SetWorkingImage(); //else // tmpsz = size(imgs); // sz = tmpsz(1:2); // sz = sz - mod(sz, modulo); // imgs = imgs(1:sz(1), 1:sz(2),:); //end } IMAGE SRCNN(IMAGE *jpg,int up_scale) { // 双三次插值 // 先将低分辨率图像使用双三次插值放大至目标尺寸(如放大至2倍、3倍、4倍) //im_b = imresize(im_gnd, up_scale, 'bicubic'); IMAGE im_h=*jpg; // ZoomImageBiCubic(&im_h,up_scale); return im_h; } int main() { initgraph(SCREEN_WIDTH, SCREEN_HEIGHT,SHOWCONSOLE);//, INIT_RENDERMANUAL char jpgname[]="6b.jpg";//lena.jpg //载入图片 loadjpg(jpgname); //显示原图 putimage(0, 0, &jpg); // 放大倍率 2,3 或 4 倍 int up_scale = 3; int r = jpg.getwidth()+10; //调整图像大小(与放大率匹配的) modcrop(&jpg, up_scale); int height = jpg.getheight(); int width = jpg.getwidth(); cout<<width<<endl; cout<<height<<endl; //显示调整后的图像 putimage(r, 0, &jpg); // SRCNN 高分辨率重建 IMAGE im_h = SRCNN( &jpg,up_scale); //显示重建图 //figure, imshow(im_h1); title('SRCNN 重建'); height = im_h.getheight(); width = im_h.getwidth(); cout<<width<<endl; cout<<height<<endl; //显示 r+=width+10; putimage(r, 0, &im_h); //保存结果 //imwrite(im_h1, ['SRCNN Reconstruction' '.jpg']); saveimage("SRCNN 重建.jpg", &im_h); getch(); closegraph(); system("pause"); return 0; }
SRCNN只是做了个空函数,后面再填充
效果图: