用图像替换箭袋箭头

时间:2016-03-17 17:20:41

标签: matlab matlab-figure

我有一个圆形格子,在格点上我绘制了标准化的箭头,它们保持相同的大小并根据模拟改变方向,其细节无关紧要。

我的情节看起来像这样

enter image description here

是否可以用jpg / bmp / gif / png图像替换箭袋图中的箭头?或任何其他命令?

理想情况下,它看起来像这样(虽然不一定是箭头)

enter image description here

1 个答案:

答案 0 :(得分:27)

解释

您可以这样做的一种方法是使用surface objecttexture-map as the FaceColor

在MATLAB中,您可以创建一个简单的矩形曲面。您可以将FaceColor设置为texturemap,这将导致分配给CData的值在地面上进行映射。

然后,为了获得透明度,您还可以将FaceAlpha值设置为texturemap并设置AlphaData,并且这些透明度值也将在整个表面范围内进行映射。

要将此应用于您的案例,您需要将CData设置为要用于替换箭头的图像。并且您希望AlphaData与图像数据的大小相同,值为1,您希望它是不透明的,0,您希望它是透明的。这将使它看起来不像您发布的图像,您可以清楚地看到边界框。然后,您将需要绘制其中一个箭头所在的表面,并适当地缩放/定位它。

实施

更新:此代码(ImageQuiver)的更精彩版本现已在Github以及MATLAB File Exchange上提供。

作为我所谈论的内容的演示,我创建了以下功能,基本上就是这样做的。它接受与quiver相同的输入(首先提供图像数据,最后提供可选的AlphaData参数),并在指向所请求方向的所有请求坐标处创建一个曲面,并进行缩放按指定金额。

function h = quiverpic(im, X, Y, dX, dY, scale, alpha)
    % im - RGB or indexed image
    % X - X positions
    % Y - Y positions
    % dX - X direction vector
    % dY - Y direction vector
    % scale - Any scaling (Default = 1)
    % alpha - Transparency (same size as im), if not specified = ~isnan(im)

    h = hggroup();

    if ~exist('scale', 'var')
        % By default there is no scaling
        scale = 1;
    end

    if ~exist('alpha', 'var')
        % By default, any NaN will be transparent
        alpha = ~isnan(im);
    end

    % Determine aspect ratio of the source image
    width_to_height = size(im, 2) / size(im, 1);

    for k = 1:numel(X)
        % Determine angle from displacement vectors
        theta = atan2(dY(k), dX(k));

        % Subtract pi/2 to +y is considered "up"
        theta = theta + pi/2;

        % Setup surface plot boundary
        [xx,yy] = meshgrid([-0.5, 0.5] * width_to_height, [0 1]);

        % Scale depending on magnitude of dX and dY
        this_scale = scale * sqrt(dX(k).^2 + dY(k).^2);

        % Scale X and Y components prior to rotating
        xx = xx .* this_scale;
        yy = yy .* this_scale;

        % Rotate to align with the desired direction
        xdata = xx .* cos(theta) - yy .* sin(theta);
        ydata = xx .* sin(theta) + yy .* cos(theta);

        % Determine what is considered the "anchor" of the graphic.
        % For now this is assumed to be the "bottom-middle"
        xoffset = X(k) - mean(xdata(2,:));
        yoffset = Y(k) - mean(ydata(2,:));

        % Actually plot the surface.
        surf(xdata + xoffset, ...
             ydata  + yoffset, zeros(2), ...
             'Parent', h, ...
             'FaceColor', 'texture', ...
             'EdgeColor', 'none', ...
             'CData', im, ...
             'FaceAlpha', 'texture', ...
             'AlphaData', double(alpha));
    end
end

实施例

我写了一个小测试脚本来说明如何使用它并显示结果。

t = linspace(0, 2*pi, 13);
dX = cos(t(1:end-1));
dY = sin(t(1:end-1));
X = (3 * dX) + 5;
Y = (3 * dY) + 5;
scale = 1;

% Load the MATLAB logo as an example image
png = fullfile(matlabroot,'/toolbox/matlab/icons/matlabicon.gif');
[im, map] = imread(png);
im = ind2rgb(im, map);

% Determine alpha channel based on upper left hand corner pixel
flatim = reshape(im, [], 3);
alpha = ~ismember(flatim, squeeze(im(1,1,:)).', 'rows');
alpha = reshape(alpha, size(im(:,:,1)));

% Plot some things prior to creating the quiverpic object
fig = figure();
hax = axes('Parent', fig);
axis(hax, 'equal');

% Plot a full circle
t = linspace(0, 2*pi, 100);
plot((cos(t) * 3) + 5, (sin(t) * 3) + 5, '-')

hold(hax, 'on')

% Plot markers at all the quiver centers
plot(X, Y, 'o', 'MarkerFaceColor', 'w')

% Plot a random image behind everything to demonstrate transparency
him = imagesc(rand(9));
uistack(him, 'bottom')

axis(hax, 'equal')
colormap(fig, 'gray')
set(hax, 'clim', [-4 4]);

% Now plot the quiverpic
h = quiverpic(im, X, Y, dX, dY, 1, alpha);

axis(hax, 'tight')

结果

enter image description here

荒诞

具有不同矢量和缩放的相同图像

enter image description here

任何宽高比的图像都可以正常使用

enter image description here