MATLAB : Aligning RGB Channels
20 views (last 30 days)
Show older comments
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.
please help me with the code....
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/310259/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/310262/image.jpeg)
%Read the image
img = imread('course1image.jpg');
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);
Answers (3)
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.
%Read the image
img = imread('course1image.jpg');
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));
1 Comment
Deema Alyones
on 28 Mar 2022
Edited: DGM
on 1 Jan 2024
I have a question on one of the parts in the code you wrote. What does the numbers inside the parentheses represent and how can I find these values from the image?
B=img(1:341,1:400);
G=img(342:682,1:400);
R=img(683:1023,1:400);
and
g1 = g(146:196, 175:225);
r1 = r(146:196, 175:225);
b1 = b(146:196, 175:225);
Medical Imaging
on 6 Aug 2020
Edited: DGM
on 1 Jan 2024
I hope the below soultion helps:
%Read the image
img = imread('course1image.jpg');
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]);
outpict=cat(3,uint8(shiftr),uint8(g),uint8(shiftb));
imshow(ColorImg_aligned)
0 Comments
Chit La Pyae Myo Hein
on 23 Sep 2020
%Read the image
img = imread('course1image.jpg');
[r,c] = size(img);
B = img(1:r/3,:);
G = img((r/3)+1:(2*r/3),:);
R = img((2*r/3)+1:r,:);
ref_img_region = G;
[rg,cg] = size(ref_img_region);
ref_img_region = ref_img_region(ceil((rg-50)/2) :ceil((rg-50)/2) + 50,ceil((cg-50)/2) :ceil((cg-50)/2) + 50);
%disp(size(ref_img_region));
ref_img_region = double(ref_img_region);
% Naive way
% ColorImg_aligned = cat(3,R,G,B);
% imshow(ColorImg_aligned);
% SSD way
nR = align(G,R);
nB = align(G,B);
ColorImg_aligned = cat(3,nR,G,nB);
imshow(ColorImg_aligned);
function aligned = align(green,red)
[red_row,red_col] = size(red);
[green_row,green_col] = size(green);
% checking SSD for cropped part of the images for faster calculation
cropped_red = red(ceil((red_row-50)/2) : ceil((red_row-50)/2) + 50,ceil((red_col-50)/2) :ceil((red_col-50)/2) + 50);
cropped_green = green(ceil((green_row-50)/2) : ceil((green_row-50)/2) + 50,ceil((green_col-50)/2) :ceil((green_col-50)/2) + 50);
MiN = 9999999999;
r_index = 0;
r_dim = 1;
for i = -10:10
for j = -10:10
ssd = SSD(cropped_green,circshift(cropped_red,[i,j]));
if ssd < MiN
MiN = ssd;
r_index = i;
r_dim = j;
end
end
end
aligned = circshift(red,[r_index,r_dim]);
end
function ssd = SSD(a1,a2)
x = double(a1)-double(a2);
ssd = sum(x(:).^2);
end
0 Comments
See Also
Categories
Find more on Computer Vision with Simulink 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!