1

我正在使用 WindowAPI (http://www.mathworks.com/matlabcentral/fileexchange/31437) 在 matlab 中显示黑色全屏。在屏幕上绘制时,使用 line() 和 rectangle() 函数绘制非常慢。

如何在不通过 matlab 机制的情况下设置像素值?例如,获取窗口的画布会很棒。

4

1 回答 1

3

模仿“画布”的一种方法是使用 MATLAB 图像。这个想法是手动更改其像素并更新'CData'绘制图像的像素。

请注意,您可以使用与屏幕尺寸相同尺寸的图像(图像像素将与屏幕像素一一对应),但更新它会更慢。诀窍是让它保持小,让 MATLAB 将其映射到整个全屏。这样,图像可以被认为具有“胖”像素。当然,图像的分辨率会影响您绘制的标记的大小。

为了说明,请考虑以下实现:

function draw_buffer()
    %# paramters (image width/height and the indexed colormap)
    IMG_W = 50;    %# preferably same aspect ratio as your screen resolution
    IMG_H = 32;
    CMAP = [0 0 0 ; lines(7)];    %# first color is black background

    %# create buffer (image of super-pixels)
    %#  bigger matrix gives better resolution, but slower to update
    %#  indexed image is faster to update than truecolor
    img = ones(IMG_H,IMG_W);

    %# create fullscreen figure
    hFig = figure('Menu','none', 'Pointer','crosshair', 'DoubleBuffer','on');
    WindowAPI(hFig, 'Position','full');

    %# setup axis, and set the colormap
    hAx = axes('Color','k', 'XLim',[0 IMG_W]+0.5, 'YLim',[0 IMG_H]+0.5, ...
        'Units','normalized', 'Position',[0 0 1 1]);
    colormap(hAx, CMAP)

    %# display image (pixels are centered around xdata/ydata)
    hImg = image('XData',1:IMG_W, 'YData',1:IMG_H, ...
        'CData',img, 'CDataMapping','direct');

    %# hook-up mouse button-down event
    set(hFig, 'WindowButtonDownFcn',@mouseDown)


    function mouseDown(o,e)
        %# convert point from axes coordinates to image pixel coordinates
        p = get(hAx,'CurrentPoint');
        x = round(p(1,1)); y = round(p(1,2));

        %# random index in colormap
        clr = randi([2 size(CMAP,1)]);  %# skip first color (black)

        %# mark point inside buffer with specified color
        img(y,x) = clr;

        %# update image
        set(hImg, 'CData',img)
        drawnow
    end
end

截屏

于 2011-09-22T22:29:58.010 回答