quiver plot on an image

Hello
I am trying to quiverplot on an image, but quiver plot shows up very small on the upper left corner (attached here)
I have to use 'imshow' instead of 'imagesc' becasue I want the image in black and white and colored quiver vectors. When I assigne colormap for 'imagesc' it messes the quiver vectors.
Is there any way to superimpose quiver plot on imshow function?
thank you

 Accepted Answer

Voss
Voss on 1 Mar 2024
Edited: Voss on 1 Mar 2024
"Is there any way to superimpose quiver plot on imshow function?"
Yes.
% read an image:
img = imread('peppers.png');
% imshow the image:
imshow(img)
% make a quiver plot:
hold on
[m,n,~] = size(img);
[x,y] = meshgrid(1:n,1:m);
quiver(x,y,randn(m,n),randn(m,n))
% zoom in to see some details:
xlim([101 115])
ylim([101 110])
It looks like the x and y you use in quiver are incorrect is why your quiver plot is so small.

4 Comments

thanks for the answer, however, for my case
[m n] is [2304 2401]
x and y size is [29 31]
i have a scaling issue.
Here's an example where the image and x and y are the same sizes as yours:
% an image the size of your image:
img = imresize(imread('peppers.png'),[2304 2401]);
size(img)
ans = 1×3
2304 2401 3
figure
im = imshow(img);
By default the XData of a plotted image is [1 size(img,2)] and the YData is [1 size(img,1)], so the pixel coordinates are 1:size(img,2) in the x-dimension and 1:size(img,1) in the y-dimension.
im.XData
ans = 1×2
1 2401
im.YData
ans = 1×2
1 2304
% some arbitrary x and y of the same size as yours:
x = linspace(2,78,31); % x goes from 2 to 78
y = linspace(3,14,29); % y goes from 3 to 14
[x,y] = meshgrid(x,y);
size(x)
ans = 1×2
29 31
You either have to (1) scale your x to span from 1 to size(img,2) and scale your y to span from 1 to size(img,1), or (2) you have to set your image's XData and YData to match the limits of your quiver's x and y.
Option 1: rescale the x and y to the location of the image:
minx = min(x(:));
maxx = max(x(:));
miny = min(y(:));
maxy = max(y(:));
[m,n,~] = size(img);
X = (x-minx)./(maxx-minx).*(n-1)+1;
Y = (y-miny)./(maxy-miny).*(m-1)+1;
Check the min and max of X and Y (these match the image now):
min(X(:))
ans = 1
max(X(:))
ans = 2401
min(Y(:))
ans = 1
max(Y(:))
ans = 2304
% make a quiver plot:
hold on
quiver(X,Y,randn(size(x)),randn(size(x)))
Option 2: adjust the image XData and YData:
% new figure:
figure
% show the image in the new figure:
im = imshow(img);
% now adjust the XData and YData (makes the image tiny in this case):
im.XData = [min(x(:)) max(x(:))];
im.YData = [min(y(:)) max(y(:))];
% make a quiver plot:
hold on
quiver(x,y,randn(size(x)),randn(size(x)))
% the axes limits are still for the image's original
% XData and YData, so adjust those too:
set(gca(),'XLimMode','auto','YLimMode','auto') % setting the limit modes to auto
% xlim(im.XData+[-0.5 0.5]) % alternatively, explicitly set the limits to
% ylim(im.YData+[-0.5 0.5]) % match the new XData and YData
thanks
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Asked:

on 1 Mar 2024

Commented:

on 1 Mar 2024

Community Treasure Hunt

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

Start Hunting!