开发者

Set pixels on screen efficiently

I am using WindowAPI (http://www.mathworks.com/matlabcentral/fileexchange/31437) to show a black full screen in matlab. When drawing on screen, turns out drawing using line() and rectangle() functions is开发者_开发百科 extremely slow.

How can I set values of pixels without going through matlab's mechanism? Getting the window's canvas for example would be great.


One way to imitate a "canvas" is by using a MATLAB image. The idea is to manually change its pixels and update the 'CData' of the plotted image.

Note that you can use an image with the same dimensions as your screen size (image pixels will correspond to screen pixels one-to-one), but updating it would be slower. The trick is to keep it small and let MATLAB map it to the entire fullscreen. That way the image can be thought of as having "fat" pixels. Of course the resolution of the image is going to affect the size of the marker you draw.

To illustrate, consider the following implementation:

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

Set pixels on screen efficiently

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜