2

我需要创建一个 n 阶 Hadamard 矩阵,将其加倍,在每行内随机排列矩阵的元素,然后显示它。到目前为止,我已经完成了所有这些事情。imshow(matrix)当我是一张黑白盒子的漂亮照片时,我最终会得到什么。但我还没有想出如何插入细线来划分每一行。我可以创建类似于左侧第一张图像的图像,但不能创建右侧图像(这些是本文中的图 1 和图 2 )

在此处输入图像描述   在此处输入图像描述

任何帮助或意见将不胜感激。

4

3 回答 3

2

我发现对这类问题使用向量方法(例如patch和)具有不必要的挑战性。rectangle我认为建立一个新形象更直接。这避免了浮点舍入问题和矢量图形出现的其他问题。我下面的解决方案依赖于图像处理工具箱中的一些功能,但简单快捷:

% Create data similarly to @TryHard
H = hadamard(48);
C = (1+[H;-H])/2;
rng(0); % Set seed
C(:) = C(randperm(numel(C))); % For demo, just permute all values, not rows

% Scale image and lines
scl = 10; % Amount to vertically scale each row
pad = 2;  % Number of pixels to add between each row
C = imresize(C,scl,'nearest');
C = blockproc(C,[scl size(C,2)],@(x)[x.data;zeros(pad,size(C,2))]);
C = C(1:end-pad,:); % Remove last line added

% Dispay image
imshow(C)

这会产生这样的图像

           显示图像

和参数可以很容易地调整以获得不同的大小和相对大小sclpad如果需要,您可以imresize(...,'nearest')在添加线条后再次调用以进一步缩放图像。使用blocproc各种选项可能会提高生产线的效率(请参阅帮助)。它也可以用对 and 的调用来代替im2colcol2im如果更混乱的话,它可能会更快。

于 2013-08-05T21:34:14.463 回答
1

我没有尝试代码,但我认为这样的东西应该可以工作:

sizeOfACube = 6;
numberOfRows = 47;    

RGB = imread('image.png');
RGB = imresize(A, [(numRows+numberOfRows) numCols]);


for i=1:1:NumberOfRows 
    RGB(i*6,:,:) = 0;
end

imagesc(RGB);
imwrite(RGB,'newImage.png');

with: sizeOfAcube 二维码上一个立方体的大小。numRows 和 numCols 原始图像的行数和列数。

于 2013-08-05T03:18:12.940 回答
0

一种解决方案是使用补丁,例如如下:

% set up example array
xl = 24; yl = xl;
[X Y] = find(hadamard(xl)==1);
% generate figure
figure, hold on
for ii=1:length(X)
    patch(X(ii) + [0 0 1 1],Y(ii) + [0.1 0.9 0.9 0.1],[1 1 1],'Edgecolor',[1 1 1])
end
axis([0 xl+1 0 yl+1])
axis('square')

patch 命令patch(x,y, color)接受多边形元素的顶点为xy。在此示例中,您可以修改术语[0.1 0.9 0.9 0.1]以设置边界黑线的粗细。

这会产生

在此处输入图像描述

已编辑

对于 OP 提供的特定实例:

H=Hadamard(48); %# now to row-double the matrix 
A=(1+H)/2;
B=(1-H)/2;
C=[A; B]; %# the code below randomly permutes elements within the rows of the matrix 
[nRows,nCols] = size(C); 
[junk,idx] = sort(rand(nRows,nCols),2); %# convert column indices into linear indices 
idx = (idx-1)*nRows + ndgrid(1:nRows,1:nCols); %# rearrange whatever matrix 
E = C; 
E(:) = E(idx);
[X Y] = find(logical(E));
xl = length(X);
yl = length(Y);
figure, hold on
for ii=1:xl
     rectangle('Position',[X(ii) Y(ii)+.2 1 0.8],'facecolor',[1 1 1],'edgecolor',[1 1 1])
end
axis([0 max(X)+1 0 max(Y)+1])
axis('square')
set(gca,'color',[0 0 0])
set(gca,'XTickLabel',[],'YTickLabel',[],'XTick',[],'YTick',[])

此示例使用rectangle而不是补丁来生成尖角。

图片:

在此处输入图像描述

于 2013-08-05T09:54:37.847 回答