有效地在屏幕上设置像素 [英] Set pixels on screen efficiently

查看:86
本文介绍了有效地在屏幕上设置像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如何不通过matlab的机制来设置像素值?例如,获取窗口的画布会很棒.

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

推荐答案

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

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.

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

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

这篇关于有效地在屏幕上设置像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆