如何在灰度图像的每一行之间添加细水平线? [英] How can thin horizontal lines be added between each row in a grayscale image?

查看:123
本文介绍了如何在灰度图像的每一行之间添加细水平线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个n阶Hadamard矩阵,将其加倍,在每行内随机置换矩阵的元素,然后显示它。到目前为止,我已经完成了所有这些事情。当我 imshow(矩阵)时,我最终得到的是黑白框的漂亮图片。 但我还没想出如何插入一条细线来划分每一行。我可以创建类似左边第一张图像的东西,但不能创建右边的图像(这些是图1和2来自和 col2im ,如果比较混乱,可能会更快。


I need to create an nth-order Hadamard matrix, row double it, within each row randomly permute the elements of the matrix, and then display it. So far, I have accomplished all of these things. What I end up with when I imshow(matrix) is a nice picture of black and white boxes. But I haven't figured out how to insert a fine line to divide each row. I can create something like the first image on the left, but not the image on the right (these are Figures 1 and 2 from this paper)

 

Any help or comments would be thoroughly appreciated.

解决方案

I've found using vector approaches (e.g., patch and rectangle) for this sort of problem unnecessarily challenging. I think that it's more straightforward to build a new image. This avoids floating-point rounding issues and other things that crop up with vector graphics. My solution below relies on some functions in the Image Processing Toolbox, but is simple and fast:

% 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)

This results in an image like this

           

The scl and pad parameters can be easily adjusted to obtain different sizes and relative sizes. You can call imresize(...,'nearest') again after adding the lines to further scale the image if desired. The blocproc line could potentially be made more efficient with various options (see the help). It could also be replaced by calls to im2col and col2im, which possibly could be faster, if messier.

这篇关于如何在灰度图像的每一行之间添加细水平线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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