我正在使用WindowAPI(http://www.mathworks.com/matlabcentral/fileexchange/31437)在matlab中显示黑色全屏。 在屏幕上绘图时,使用line()和矩形()函数调出绘图的速度非常慢。
如何在不经过matlab机制的情况下设置像素值?以窗口的画布为例会很棒。
答案 0 :(得分: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