ESPCN处理彩色图像代码

原来仅处理了Y通道,输出的灰度图像。

Super-Resolution/ESPCN at master · wangxuewen99/Super-Resolution · GitHub https://github.com/wangxuewen99/Super-Resolution/tree/master/ESPCN

 

改为处理彩色图像,就简单加了几行代码。

YCbCr分别处理,只对Y进行输入,另两个通道双三插值。

预先不进行降采样了。直接SR。

三倍放大。

 

sr_demo.m

% =========================================================================
% 描述:   一个使用训练好的网络进行超分辨率的应用demo(使用caffe的matlab接口)          
%    
% 参考文献:
%  Shi W, Caballero J, Huszar F, et al. Real-Time Single Image and Video
%  Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network[C]
%  
% 王学文
% wangxuewen@yy.com
% =========================================================================
%% settings
model = './espcn_mat.prototxt';
weights = './snapshot/espcn_iter_10000.caffemodel';
batch = 1;
up_scale = 3;
%% read data
input = imread('../data/test/set14/zebra.bmp');
% input=imread('Infrared/12.bmp');
% if size(input,3)>1
    input2 = rgb2ycbcr(input);
    input = input2(:,:, 1);
    im_l_cb = input2(:,:, 2);
    im_l_cr = input2(:,:, 3);
% end;
input = single(input)/255;
% input = imresize(input, 1/up_scale, 'bicubic');
[height, width, channel] = size(input);

%% use gpu mode
caffe.reset_all(); 
caffe.set_mode_cpu();
% caffe.set_mode_gpu();
% caffe.set_device(0);

% image_new=zeros(height, width, 3);

%% load model using mat_caffe

net = caffe.Net(model,weights,'test');
net.blobs('data').reshape([height width channel batch]); % reshape blob 'data'
net.reshape();
net.blobs('data').set_data(input);
net.forward_prefilled();
output = net.blobs('conv3').get_data();
[output_height, output_width, output_channel] = size(output);
scaled_height = up_scale * output_height;
scaled_width = up_scale * output_width;
im_h = zeros(scaled_height, scaled_width);
    
for m = 1 : up_scale
     for n = 1 : up_scale
         im_h(m:up_scale:scaled_height+m-up_scale,n:up_scale:scaled_width+n-up_scale) = output(:,:,(m-1)*up_scale+n);   
     end
end
im_h = im_h * 255;
[nrow, ncol] = size(im_h);
im_h_cb = imresize(im_l_cb, [nrow, ncol], 'bicubic');
im_h_cr = imresize(im_l_cr, [nrow, ncol], 'bicubic');

im_h_ycbcr = zeros([nrow, ncol, 3]);
im_h_ycbcr(:, :, 1) = im_h;
im_h_ycbcr(:, :, 2) = im_h_cb;
im_h_ycbcr(:, :, 3) = im_h_cr;
im_h = ycbcr2rgb(uint8(im_h_ycbcr));

% im_h = uint8(im_h * 255);


imwrite(im_h,'13_espcn_caffe.bmp');

 

posted @ 2019-03-27 13:22  ostartech  阅读(527)  评论(0编辑  收藏  举报