Imshowpair showing only green channel

% Imshowpair output shows only the green channel of the RGB image in both the cases, for the
% code below, the input image is RGB. What should I do?
% PS: Its a code for a simple translation of the image 1
clear, clc,close all;
img = imread('image1.jpg');
I = imresize(img,[512 512]);
ref = imref2d(size(I));
background = zeros(700);
imshowpair(I,ref,background,imref2d(size(background)))
axis on;
my_translation(I,100,-100,background)
function my_translation(I,x,y,background)
tform = affine2d([1 0 0; 0 1 0; x y 1]);
r = imref2d(size(I));
[out,out_ref] = imwarp(I,r,tform);
figure,imshowpair(out,out_ref,background,imref2d(size(background)))
axis on;
end

 Accepted Answer

This is exactly what is expected for that set of commands.
imshowpair(...,METHOD) displays the differences between images A and B
using the visualization style specified by METHOD. Values of METHOD
can be:
'falsecolor' : Create a composite RGB image showing A and B overlayed
in different color bands. This is the default.
Parameters include:
'ColorChannels' Assign each image to specific color channels in the
output image. This method can only be used with the
'falsecolor' method.
Valid 'ColorChannels' values are:
[R G B] : A three element vector that
specifies which image to assign
to the red, green, and blue
channels. The R, G, and B values
must be 1 (for the first input
image), 2 (for the second input
image), and 0 (for neither image).
'red-cyan' : A shortcut for the vector [1 2 2],
which is suitable for red/cyan
stereo anaglyphs.
'green-magenta' : A shortcut for the vector [2 1 2]
which is a high contrast option
ideal for people with many kinds
of color blindness.
Default value: 'green-magenta'
You did not specify an explicit method, so you get 'falsecolor'. You did not specify 'ColorChannels' so you get the default green-magenta.
The default [2 1 2] vector for green-mageneta means that the first channel (red) is to be taken from image #2, that the second channel (green) is to be taken from image #1, and that the third channel (blue) is to be taken from image #2. Your image #2 is all zeros, so you red and blue channels are to be set to all 0. Your green channel is extracted from the green channel of image #1. Overall result: you see the green channel of the original image. Exactly what the code asks for.
When you use an all-zero second image like you did, it does not make much sense to use anything other than facecolor with selected color channels like you are doing. The only other setting that might make sense is checkerboard, if you wanted to show like a mosaic.
img = imread('flamingos.jpg');
imshowpair(img, 0, 'checkerboard');

7 Comments

Please help me out with an explicit code for this, I wanted to make the background black and hence used zero values. I need to show that the image has translated hence I used those values. I want a RGB image, I dont exactly understand what to do after setting a Colourchannel method. Its throwing some kind of error, pretty new to image processing, cheers!
imshowpair() cannot be used for that purpose. It can be used to position two images against each other, but when you use the 'falsecolor' method (the default), at least one of the color channels must come from the second image.
You can display two images at different locations, if you use image() and pass in the XData and YData parameters, which are coordinates for the centers of the lower left and upper right pixels.
im = imresize(imread('flamingos.jpg'),[80 80]);
BG = zeros(128,128,3,'uint8');
image([.5 128.5], [0.5 128.5], BG); hold on
image([17.5 97.5], [64.5 144.5], im); hold off; ylim auto
img = imread('image1.jpg');
I = imresize(img,[512 512]);
figure,J = imshow(I);
axis on
figure,imshow(my_translation(J,100,-100));
axis on
function output = my_translation(ax,x,y)
xrange = ax.XData;
yrange = ax.YData;
xrangeNew = xrange + x;
yrangeNew = yrange + y;
output = imshow(ax,'XData',xrangeNew,'YData',yrangeNew);
axis on
end
% I have made some progress in the coding however I am unsure of how to
% manipuate the last part inside the function, note that I have to use a
% function. I am sure the problem I am having is a data type operation
% mismatch, could you kindly point out where I am going wrong. Cheers!
I have gotten rid of the background which doesnt make much sense to have as the purpose is served by the axes. Could you please telll me how I should pass the output:
output = image(ax,'XData',xrangeNew,'YData',yrangeNew);
I have tried this as an alternative as well but I cannot call the function back as this data type. I have suceeded in implementing the operation without a function definition.
Your my_translation is returning the handle to the image() object created by the imshow() call in my_translation. You cannot then imshow() the handle to the image()
img = imread('foosball.jpg');
I = imresize(img,[512 512]);
fig1 = figure;
ax1 = axes(fig1);
J = imshow(I, 'Parent', ax1);
axis(ax1, 'on');
fig2 = figure;
ax2 = axes(fig2);
my_translation(ax2, J,100,-100);
axis(ax2, 'on')
function output = my_translation(ax,h,x,y)
xrange = h.XData;
yrange = h.YData;
xrangeNew = xrange + x;
yrangeNew = yrange + y;
cmap = colormap(ancestor(h,'axes'));
output = image('Parent', ax, 'CData', h.CData, 'XData', xrangeNew, 'YData', yrangeNew);
colormap(ax, cmap);
axis(ax, 'on')
ax.YDir = 'reverse';
end
Oh yes I was just reading about how to deal with handle of an image, thank you so much for this.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!