# MATLAB : Aligning RGB Channels

143 views (last 30 days)
Rahul Mishra on 5 Jun 2020
Answered: Medical Imaging on 6 Aug 2020
Align the 3 images from earlier project to get a clearer color image. This project needs to be done using SUM of SQUARED DIFFERENCES APPROACH.
B=img(1:340,1:400);
G=img(341:682,1:400);
R=img(683:1023,1:400);
b=im2double(B);
g=im2double(G);
r=im2double(R);
shiftr=circshift(r,[-10,10]);
shiftb=circshift(b,[-5,5]);
ColorImg_aligned=cat(3,shiftr,g,shiftb);

Thyagharajan K K on 12 Jun 2020
I have solved the problem which passes first 5 tests. To solve the last part, I need the following clarifications.
What is the meaning of this instruction already set in the problem? should we move 51x51 green block on red channel to a maximum of 10 positions left and 10 position right to find matching
shiftr=circshift(r,[-10,10]);
if 51x51 g pixel block finds a match in r should we extend the size of the channels. The problem is not clear. It is difficult to understand the aim and methodology expected to follow. In the video r and b channels are moved completely on g channel but in the problem 51x51 pixel block is introduced for matching. The problem can be defined with more clarity.
B=img(1:341,1:400);
G=img(342:682,1:400);
R=img(683:1023,1:400);
b=double(B);
g=double(G);
r=double(R);
%take a block of size 50x50 pixels
g1 = g(146:196, 175:225);
r1 = r(146:196, 175:225);
b1 = b(146:196, 175:225);
ref_img_region = g1;
shiftr=circshift(r,[-10,10]);
shiftb=circshift(b,[-5,5]);
ColorImg_aligned=cat(3,uint8(shiftr),uint8(g),uint8(shiftb));

Medical Imaging on 6 Aug 2020
I hope the below soultion helps:
B=img(1:341,1:400);
G=img(342:682,1:400);
R=img(683:1023,1:400);
b=double(B);
g=double(G);
r=double(R);
b1=double(b(146:196,175:225));
g1=double(g(146:196,175:225));
r1=double(r(146:196,175:225));
ref_img_region = double(g1);
ref_img_region = double(ref_img_region);
error = inf;
for i = -10:10
for j = -10:10
shiftr1=circshift(r1,[i,j]);
temp1 = sum(sum((double(g1) - double(shiftr1)) .^ 2));
if temp1 < error
error = temp1;
shiftr_row = i;
shiftr_col = j;
end
end
end
error = inf;
for i = -10:10
for j = -10:10
shiftb2=circshift(b1,[i,j]);
temp2 = sum(sum((double(g1) - double(shiftb2)) .^ 2));
if temp2 < error
error = temp2;
shiftb_row = i;
shiftb_col = j;
end
end
end
shiftr=circshift(r,[shiftr_row,shiftr_col]);
shiftb=circshift(b,[shiftb_row,shiftb_col]);
ColorImg_aligned=cat(3,uint8(shiftr),uint8(g),uint8(shiftb));
imshow(ColorImg_aligned)