How can I implement the nearest neighbor interpolation properly by myself?

3 views (last 30 days)
Hello!
I really need help with my code to implement the nearest neighbor interpolation properly for RGGB image without using any available functions. The script looks as follows:
clear all close all clc
x = imread('Flowers.tiff'); x = double(x)/255; [m, n]=size(x);
r_mask=repmat([1, 0;0, 0],[m/2, n/2]); g_mask=repmat([0, 1;1, 0],[m/2, n/2]); b_mask=repmat([0, 0;0, 1],[m/2, n/2]);
r=x.*r_mask; g=x.*g_mask; b=x.*b_mask;
R=r; G=g; B=b;
for i=2:(m-1) for j=2:(n-1)
%red canal R
if (r(i-1,j)==0 && r(i+1,j)==0) && (r(i,j-1)==0 && r(i,j+1)==0)
if r(i,j)==0
R(i,j)=r(i,j);
end
elseif (r(i,j-1)==0 && r(i,j+1)==0) && (r(i-1,j)~=0 && r(i+1,j)~=0)
R(i,j)=r(i,j);
elseif (r(i-1,j)==0 && r(i+1,j)==0) && (r(i,j-1)~=0 && r(i,j+1)~=0)
R(i,j)=r(i,j);
end
%green canal G
if g(i,j)==0
G(i,j)=g(i,j);
end
%blue canal B
if (b(i-1,j)==0 && b(i+1,j)==0) && (b(i,j-1)==0 && b(i,j+1)==0)
if b(i,j)==0
B(i,j)=b(i,j);
end
elseif (b(i,j-1)==0 && b(i,j+1)==0) && (b(i-1,j)~=0 && b(i+1,j)~=0)
B(i,j)=b(i,j);
elseif (b(i-1,j)==0 && b(i+1,j)==0) && (b(i,j-1)~=0 && b(i,j+1)~=0)
B(i,j)=b(i,j);
end
end
end
y(:,:,1)=R; y(:,:,2)=G; y(:,:,3)=B;
figure(), imshow(y);
The result:
What am I doing wrong?
  4 Comments

Sign in to comment.

Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!