绘制不同颜色的大量正方形

时间:2015-11-30 16:37:10

标签: matlab plot matlab-figure

我正在尝试加速我继承的一些代码,这些代码在图像上围绕定义的坐标绘制框。盒子的数量通常在1000-6000左右。目前它在plot()函数周围使用for循环,该函数通过5个坐标来绘制框。

我正在尝试重做它以使用plot()的单个调用,我理解这是一种更有效的渲染方式。我已经生成了一个x和y坐标数组,我可以传递给绘图,以及一个RGB三元组数组来定义每一行的颜色(每个框都有一个由这种颜色显示的状态)。我可以将它们与'defaultAxesColorOrder'属性一起使用,但我需要它来停止加入每个方块。

我查看了'defaultAxesLineStyleOrder'属性,但据我所知,它仅在一个完整的颜色循环后循环样式,在这种情况下不会起作用。我真正需要的是一种方法,它可以绘制5个点的线条,然后是下一个线条,然后重复。

我有什么方法可以通过不会减慢渲染速度的方式来做到这一点?

编辑:到目前为止,这是新代码:

编辑2:我设法阻止它使用NaN值在方块之间绘制线条以分隔每个方块。我在关于优化线图的页面上从Mathworks文档中获得了技术,我现在无法找到生命中的URL。我已经更新了以下代码。

numObjs = size(MYDATA.objectInformation, 1);
if isfield(MYDATA, 'objectInformation')
    if (numObjs > 0)

        padding = MYDATA.preferences.boxSize/2;

        % Array of coords. 6* the number of objects (takes 5 points to draw
        % a square + NaN gap hack)
        objCoords = zeros(6*numObjs, 2);
        objCoordsIndex = 1;

        % array of line colours for defaultAxesColorOrder
        objColours = zeros(6*numObjs, 3);

        % Add each object to array
        for loop = 1:numObjs

            x_seed = MYDATA.objectInformation{loop, 1};
            y_seed = MYDATA.objectInformation{loop, 2};
            status = MYDATA.objectInformation{loop, 3};

            % set coords to draw bounding box
            objCoords(objCoordsIndex    , 1:2) = [(x_seed - padding), (y_seed - padding)];  % bottom left
            objCoords(objCoordsIndex + 1, 1:2) = [(x_seed - padding), (y_seed + padding)];  % top left
            objCoords(objCoordsIndex + 2, 1:2) = [(x_seed + padding), (y_seed + padding)];  % top right
            objCoords(objCoordsIndex + 3, 1:2) = [(x_seed + padding), (y_seed - padding)];  % bottom right
            objCoords(objCoordsIndex + 4, 1:2) = [(x_seed - padding), (y_seed - padding)];  % bottom left
            objCoords(objCoordsIndex + 5, 1:2) = [NaN, NaN];  % gap!

            % set colour depending on status
            % NOTE: doesn't use preferences due to effort of converting
            % short colour names with line style modifiers to RGB triplets
            if strcmp(status, 'Interesting object') || strcmp(status, 'Interesting object - manually tagged')
                colour = [0 1 0];
            elseif strcmp(status, 'Not interesting')
                colour = [1 0 0];
            else    % If not processed/other status
                colour = [0 0 1];
            end

            for i = 1:5
                objColours(objCoordsIndex + i - 1, 1:3) = colour;
            end

            objCoordsIndex = objCoordsIndex + 5;

        end

        % Define line colours

        % Plot array
        plot(MYDATA.imagePanelHandles.mainAxes, objCoords(:, 1), objCoords(:, 2));
    end
end

以下是旧代码,如果有人想看到它:

    if isfield(MYDATA, 'objectInformation')
    if (size(MYDATA.objectInformation, 1) >= 1)
        for loop = 1:size(MYDATA.objectInformation, 1)

            x_seed = MYDATA.objectInformation{loop, 1};
            y_seed = MYDATA.objectInformation{loop, 2};
            status = MYDATA.objectInformation{loop, 3};

            %form MYDATA.preferences.boxSize/2 pixel wide box around seed
            padding = MYDATA.preferences.boxSize/2;

            x = [(x_seed - padding), (x_seed - padding), (x_seed + padding), (x_seed + padding), (x_seed - padding)];
            y = [(y_seed - padding), (y_seed + padding), (y_seed + padding), (y_seed - padding), (y_seed - padding)];

            if strcmp(status,'Not yet processed')
                plot(MYDATA.imagePanelHandles.mainAxes,x,y,MYDATA.preferences.notProcessedBoxColour)
            elseif strcmp(status, 'Interesting object') || strcmp(status, 'Interesting object - manually tagged')
                plot(MYDATA.imagePanelHandles.mainAxes,x,y,MYDATA.preferences.interestingObjectBoxColour)
            elseif strcmp(status, 'Not interesting')
                plot(MYDATA.imagePanelHandles.mainAxes,x,y,MYDATA.preferences.notInterestingBoxColour)
            else % be cautious and catch anything that has fallen through
                plot(MYDATA.imagePanelHandles.mainAxes,x,y,'-r')
            end
        end
    end

1 个答案:

答案 0 :(得分:1)

我已经通过利用plot()函数为每个坐标获取2D数组的能力解决了这个问题。每列都自动绘制成不同的颜色,因此我所要做的就是创建一个X数组和一个Y坐标数组,其中每行代表一个对象的坐标,每行代表其状态。以下是文档的相关部分:http://uk.mathworks.com/help/matlab/ref/plot.html#btzptin

这是我的最终代码:

numObjs = size(MYDATA.objectInformation, 1);
if isfield(MYDATA, 'objectInformation')
if (numObjs > 0)

    padding = MYDATA.preferences.boxSize/2;

    % Arrays of coords. 6* the number of objects (takes 5 points to draw
    % a square + NaN gap hack)
    objCoordsX = NaN(6*numObjs, 3);
    objCoordsY = NaN(6*numObjs, 3);
    objCoordsIndex = 1;

    % Add each object to array
    for loop = 1:numObjs

        x_seed = MYDATA.objectInformation{loop, 1};
        y_seed = MYDATA.objectInformation{loop, 2};
        status = MYDATA.objectInformation{loop, 3};

        % set column to write to depending on status
        if strcmp(status, 'Interesting object') || strcmp(status, 'Interesting object - manually tagged')
            statusColumn = 1;
        elseif strcmp(status, 'Not interesting')
            statusColumn = 2;
        else    % If not processed/other status
            statusColumn = 3;
        end

        objCoordsX(objCoordsIndex    , statusColumn) = x_seed - padding;  % bottom left
        objCoordsX(objCoordsIndex + 1, statusColumn) = x_seed - padding;  % top left
        objCoordsX(objCoordsIndex + 2, statusColumn) = x_seed + padding;  % top right
        objCoordsX(objCoordsIndex + 3, statusColumn) = x_seed + padding;  % bottom right
        objCoordsX(objCoordsIndex + 4, statusColumn) = x_seed - padding;  % bottom left

        objCoordsY(objCoordsIndex    , statusColumn) = y_seed - padding;  % bottom left
        objCoordsY(objCoordsIndex + 1, statusColumn) = y_seed + padding;  % top left
        objCoordsY(objCoordsIndex + 2, statusColumn) = y_seed + padding;  % top right
        objCoordsY(objCoordsIndex + 3, statusColumn) = y_seed - padding;  % bottom right
        objCoordsY(objCoordsIndex + 4, statusColumn) = y_seed - padding;  % bottom left

        objCoordsIndex = objCoordsIndex + 6;    % 5 coords to draw box + NaN gap in line

    end

    % Plot coords
    plot(MYDATA.imagePanelHandles.mainAxes, objCoordsX, objCoordsY);
end