Matlab 围绕具有相同值的垂直像素组绘制矩形
考虑以下由3个不同区域/值组成的7x5矩阵的可视化:Matlab 围绕具有相同值的垂直像素组绘制矩形,matlab,plot,grouping,visualization,matlab-figure,Matlab,Plot,Grouping,Visualization,Matlab Figure,考虑以下由3个不同区域/值组成的7x5矩阵的可视化: bL = toeplitz( [zeros(1,5) -2*ones(1,2)], [0 -ones(1,4)] ); hF = figure(); hA = axes(hF); imagesc(hA,bL); axis(hA,'image'); set(hA,'XTick',[],'YTick',[]); N = 4; cmap = parula(N); colormap(cmap(1:end-1,:)); 现在,假设我在每列
bL = toeplitz( [zeros(1,5) -2*ones(1,2)], [0 -ones(1,4)] );
hF = figure(); hA = axes(hF);
imagesc(hA,bL); axis(hA,'image'); set(hA,'XTick',[],'YTick',[]);
N = 4; cmap = parula(N); colormap(cmap(1:end-1,:));
现在,假设我在每列中“选择”0个或更多像素,以便:
- 选定的像素只能在绿色区域中选择
- 选定的像素始终是连续的
- 通过指定与3个初始区域不同的常量新值来执行选择
1
):
我的目标是绘制一组包含属于同一列的“选定”(黄色)像素的矩形。对于上述示例,结果应如下所示(分别):
在我看来,要使代码成为通用代码,它应该接受:(1)一个轴句柄,其中应绘制imagesc
;(2) a数据
数组;(3) 在数据
数组中找到的值,表示“选择的”像素;以及可选的封闭像素的颜色
我找到了一些使用patch
和rectangle
(请参阅)来实现这一点的方法,但我想知道是否可以通过更少的函数调用或我没有想到的其他方式来实现这一点。使用以下方法的解决方案:
可以使用以下方法测试上述解决方案:
function q45965920
iSF = toeplitz([ones(1,3) zeros(1,2) -2*ones(1,2)],[1 -ones(1,4)]);
hF = figure(); hA = axes(hF); imagesc(hA,iSF);
axis(hA,'image'); set(hA,'XTick',[],'YTick',[]);
…然后运行markStreaksRect(hA,iSF,1)代码>或markStreaksPatch(hA,iSF,1)代码>产生所需的结果。无环解决方案使用:
以下是一个解决方案,它可以为面片
生成坐标,而不需要循环:
function column_highlight(hA, data, selectionVal)
assert(nargin >= 2);
if (nargin < 3) || isempty(selectionVal)
selectionVal = 1;
end
nCol = size(data, 2);
data = diff([false(1, nCol); (data == selectionVal); false(1, nCol)]);
[r, c] = find(data);
r = reshape(r-0.5, 2, []);
c = c(1:2:end);
X = [c-0.5 c+0.5 c+0.5 c-0.5].';
Y = r([1 1 2 2], :);
patch(hA, 'XData', X, 'YData', Y, 'FaceColor', 'none');
end
function markStreaksPatch(hA, data, selectionVal)
% Check inputs:
assert(nargin >= 2); if nargin < 3 || isempty(selectionVal), selectionVal = 1; end
% Create a mask for "selected" values:
oneMask = data == selectionVal;
% Find the first encountered "selected" element from both the top and the bottom:
[~,I1] = max(oneMask,[],1); [~,I2] = max(flipud(oneMask),[],1);
% Express the "selected" extent as a 2 row vector:
firstLast = [I1; size(oneMask,1)-I2+1].*any(oneMask,1);
% For nonzero extents, plot shifted patches:
for ind1 = find(all(firstLast,1))
[XX,YY] = meshgrid(ind1-0.5 + [0 1], firstLast(1,ind1)-0.5+[0 diff(firstLast(:,ind1))+1]);
patch(hA, XX(:), [YY(1:2) YY(4:-1:3)], 'y', 'FaceAlpha', 0);
end
function q45965920
iSF = toeplitz([ones(1,3) zeros(1,2) -2*ones(1,2)],[1 -ones(1,4)]);
hF = figure(); hA = axes(hF); imagesc(hA,iSF);
axis(hA,'image'); set(hA,'XTick',[],'YTick',[]);
function column_highlight(hA, data, selectionVal)
assert(nargin >= 2);
if (nargin < 3) || isempty(selectionVal)
selectionVal = 1;
end
nCol = size(data, 2);
data = diff([false(1, nCol); (data == selectionVal); false(1, nCol)]);
[r, c] = find(data);
r = reshape(r-0.5, 2, []);
c = c(1:2:end);
X = [c-0.5 c+0.5 c+0.5 c-0.5].';
Y = r([1 1 2 2], :);
patch(hA, 'XData', X, 'YData', Y, 'FaceColor', 'none');
end
function column_highlight(hA, data, selectionVal)
assert(nargin >= 2);
if (nargin < 3) || isempty(selectionVal)
selectionVal = 1;
end
labelMat = bsxfun(@times, (data == selectionVal), 1:size(data, 2));
coords = regionprops(labelMat, 'BoundingBox');
coords = vertcat(coords.BoundingBox);
coords(:, 3:4) = coords(:, 1:2)+coords(:, 3:4);
X = coords(:, [1 3 3 1]).';
Y = coords(:, [4 4 2 2]).';
patch(hA, 'XData', X, 'YData', Y, 'FaceColor', 'none');
end