4

I have a background image and a vectorfield with an individual color information for each vector which I want to plot over the background image:

% Random background image
Image = rand(100,200);
% Random colors
color1 = rand(30,30);
color2 = rand(30,30);
color3 = rand(30,30);
% Positions
x = 31:60;
y = 31:60;
[X,Y] = meshgrid(x,y);
% Random vectors
DX = 10 * rand(30,30);
DY = 20 * rand(30,30);
% The vector at (X(i,j),Y(i,j)) is supposed
% to have the RGB color [color1(i,j) color2(i,j) color3(i,j)]

% Uniformly colored vector field - works fine
imshow(Image);
hold on;
quiver(X,Y,DX,DY,'color',[0.5 0.75 1]);

% What I would like - does not work
imshow(Image);
hold on;
quiver(X(:),Y(:),DX(:),DY(:),'color',[color1(:) color2(:) color3(:)]);

A simple for-loop leads to an erasure of the background image as noted in: Image gradually erased when overlayed with lines, at least for Matlab version R2012b (8.0.0.783).

Any ideas?

Community
  • 1
  • 1
jolo
  • 423
  • 3
  • 10
  • I had a look at the list of known bugs in R2012b and I couldn't see this particular behaviour listed. However, there seems to be a fair amount of graphics-related bugs when using the `OpenGL` renderer. It seems that a common workaround for these is to use the `zbuffer` renderer instead. Maybe worth a try with the for loop? – am304 Nov 27 '14 at 10:09
  • Using 'zbuffer' (or 'painters') still leads to an erasure of the background image. – jolo Nov 27 '14 at 10:19
  • I'm wondering if you have tried setting your background image in a different way, for example following the one in this [link](https://de.mathworks.com/matlabcentral/answers/96023-how-do-i-add-a-background-image-to-my-gui-or-figure-window) – beesleep Nov 25 '16 at 16:46
  • maybe you can try to refresh the figure too using `refresh(gcf)`... – beesleep Nov 25 '16 at 16:51

1 Answers1

0

The first problem I have with your code is

color1 = rand(30,30);
color2 = rand(30,30);
color3 = rand(30,30);
(...)
quiver(X,Y,DX,DY,'color',[color1 color2 color3]);

Even assuming that quiver can take multiple colors (more on that below) the last argument you are passing is a 30x90 array. I don't see how to expect MATLAB to guess how this breaks into three 30x30 arrays (red, green, blue). Usually, the way to pass color data as RGB matrix is to give a Nx3 vector for N points.

More to the point though, I would not expect quiver to be able to accept multiple color arguments. (I cannot test right now.) See its properties page, in particular the "Color" tab, and compare with the same for scatter properties' "Cdata".

I fail to see a proper fix for the problem, since at the bottom there is the apparent inability in Matlab to plot vectors of different colors with the same call. If plotting the vectors in uniform color and adding a scatter of various colors at the starting points of them is enough for you, go for it... Otherwise I would have a look at the source code of this: it is not doing what you want, but maybe it can be adapted.

Leporello
  • 638
  • 4
  • 12
  • First: You are right about the formulation regarding the concatenated color matrix, but this is worth a comment only! Second: Your other comments are comments as well and no answer - sorry... – matheburg Nov 27 '14 at 11:26
  • @matheburg : I am new to the community, sorry for that. I fully agree that the color matrix thing deserved only a comment. However, I thought that "It cannot be done because (stuff)" is an acceptable answer to "How can I do (stuff)?". See http://meta.stackoverflow.com/questions/261168/is-this-is-not-possible-an-acceptable-answer/ – Leporello Nov 27 '14 at 11:58
  • @Leporello Your answer is not "It cannot be done because (stuff)" but "I fail to see a proper fix for (stuff)". It is hard to believe there is no solution for jolo's problem; and your only argument is "since at the bottom there is the apparent inability in Matlab to (stuff)" - which is no argument actually. btw: I remove the downvote, since this was overreacted especially for a new member :); another btw: An answer (worth for upvotes) would have been something like "I have modified (yourLink) to solve your (stuff)" + Code + Plots – matheburg Nov 27 '14 at 12:42