How does this code work?

clear; close all;
%% settings
folder = 'test img';
scale = 4;
%% generate data
filepaths = dir(fullfile(folder,'*.bmp'));
for i = 1 : length(filepaths)
im_gt = imread(fullfile(folder,filepaths(i).name));
im_gt = modcrop(im_gt, scale);
im_gt = double(im_gt);
im_gt_ycbcr = rgb2ycbcr(im_gt / 255.0);
im_gt_y = im_gt_ycbcr(:,:,1) * 255.0;
im_l_ycbcr = imresize(im_gt_ycbcr, 1/scale, 'bicubic');
im_b_ycbcr = imresize(im_l_ycbcr, scale, 'bicubic');
im_l_y = im_l_ycbcr(:,:,1) * 255.0;
im_l = ycbcr2rgb(im_l_ycbcr) * 255.0;
im_b_y = im_b_ycbcr(:,:,1) * 255.0;
im_b = ycbcr2rgb(im_b_ycbcr) * 255.0;
filename = sprintf('test_img-output/%s.mat',filepaths(i).name);
save(filename, 'im_gt_y', 'im_b_y', 'im_gt', 'im_b', 'im_l_ycbcr', 'im_l_y', 'im_l');
end

5 Comments

We have no reason to believe that it works. "Works" requires that it performs the function it was designed to do, but as there is no documentation, we only have two choicesb
  1. to say that it does not work; or
  2. to say that whatever it does defines what it is intended to do, like spilling a bunch of paint on a canvas and then saying that whatever results is what the painting was intended to be.
yes it works but what does it exactly do and how that is my question
What does it exactly do, is whatever, exactly, it does. It does it by making the exact sequence of calls that it makes.
You are asking us to read code without context or documentation or comments, and figure out the purpose of the code. In the absence of context and so on, the purpose of the code is just to be code, just like the purpose of this famous painting is just to be whatever it is.
the main code is to enhance the image to improve the resolution. the github link of the project is https://github.com/twtygqyy/pytorch-LapSRN. here when i run the demo it takes my image as the ground truth, detoriates it and then performs the action and improves the resolution whereas what i want it to do is take my input as the input and perform the required action. so i wasnt able to understand the purpose of this code, hence the question.
clear; close all;
%% settings
folder = 'test img';
scale = 4;
%% generate data
filepaths = dir(fullfile(folder,'*.bmp'));
for i = 1 : length(filepaths)
im_gt = imread(fullfile(folder,filepaths(i).name));
im_gt = modcrop(im_gt, scale);
im_gt = double(im_gt);
im_gt_ycbcr = rgb2ycbcr(im_gt / 255.0); %convert the gt to red blue form
%imshow(im_gt_ycbcr)
im_gt_y = im_gt_ycbcr(:,:,1) * 255.0; %white image
%imshow(im_gt_y)
im_l_ycbcr = imresize(im_gt_ycbcr, 1/scale, 'bicubic');%decrease the quality of the image and reduce the scale
%imshow(im_l_ycbcr)
im_b_ycbcr = imresize(im_l_ycbcr, scale, 'bicubic'); %further decrease the size of the image but increase the scale
%imshow(im_b_ycbcr)
im_l_y = im_l_ycbcr(:,:,1) * 255.0; %white image
%imshow(im_l_y);
im_l = ycbcr2rgb(im_l_ycbcr) * 255.0; %get the rgb image from the above white image
%imshow(im_l);
im_b_y = im_b_ycbcr(:,:,1) * 255.0; %again white image
%imshow(im_b_y);
im_b = ycbcr2rgb(im_b_ycbcr) * 255.0; %get the rgb from the above white image
%imshow(im_b);
filename = sprintf('test_img-output/%s.mat',filepaths(i).name);
imshow(im_gt_y)
save(filename, 'im_gt_y', 'im_b_y', 'im_gt', 'im_b', 'im_l_ycbcr', 'im_l_y', 'im_l');
end
im_l_y = im_l_ycbcr(:,:,1) * 255.0; %white image
Not really white image, but rather luminance image. Close enough to grayscale for your purposes.
%imshow(im_l_y);
im_l = ycbcr2rgb(im_l_ycbcr) * 255.0; %get the rgb image from the above white image
No, we identified the single pane white image as being the luminance image stored into im_l_y, but this line is working with im_l_cbcr which is 3 pane. This is a bug in the implementation.
Which is why documentation and comments are really important. The first version did whatever it actually did, and that defined what it was intended to do.

Sign in to comment.

Answers (0)

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Asked:

on 29 Feb 2020

Commented:

on 29 Feb 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!