简体   繁体   English

滑动窗口功能内的操作(​​用于图像降噪)

[英]Manipulations within sliding window functions (for image denoising)

I'm working on image de-noising, trying to keep continuous lines (with some leniency) in my matrix and removing random non-zero elements which are not really connected to the lines I'm after. 我正在对图像进行降噪处理,试图在矩阵中保持连续的行(有一定的宽容度),并删除与我后面的行没有真正联系的随机非零元素。 The problem I have is detailed below: 我的问题在下面详述:

I have tried to write a sliding window function that will search through row r of my 2-D matrix and look for non-zero digits. 我尝试编写一个滑动窗口函数,该函数将搜索我的二维矩阵的r行并查找非零数字。
For each non-zero digit, the function should look for the presence of a non-zero element within the 2x2 neighbourhood (2 numbers either side of the non-zero element) of row r and of row r+1 (and r-1 if it exists). 对于每个非零数字,函数应在行r和行r+1 (和r-1 )的2x2邻域(非零元素的任一侧为2个数字)中查找非零元素的存在。 (如果存在)。
If the neighbourhood is empty, that original element in row r should be zeroed, otherwise all non-zero elements within the window should be kept. 如果邻居为空,则应将第r行中的原始元素清零,否则应保留窗口中的所有非零元素。
To complicate things, I am also trying to make the sliding window circular, so that when it reaches the end of the row, it doesn't get compressed, but rather includes numbers from the start of the row in the neighbourhood. 为了使事情复杂化,我还尝试使滑动窗口成为圆形,以便当它到达行的末尾时,它不会被压缩,而是在邻域中从行的开始处包含数字。

For all rows, I can also only keep a "connected" element, if it is in some way connected to row 1, so continuous lines down my matrix cannot start in row 2. 对于所有行,如果以某种方式连接到第1行,我也只能保留一个“ connected”元素,因此矩阵下方的连续行不能从第2行开始。

I've written this with for -loops in Matlab, and ran into problems when trying to manipulate values within the window. 我已经在Matlab中使用for -loops编写了此代码,并且在尝试在窗口中操作值时遇到了问题。 Aside from that, my function is really slow. 除此之外,我的功能真的很慢。 Is there a way to do this in Matlab or Python without using for -loops, and without any pre-installed toolboxes? 有没有一种方法可以在Matlab或Python for执行,而无需使用-loops且没有任何预安装的工具箱?

Sample Matlab Code: "test" is a binary matrix (but not all matrices I'll look at will be binary). Matlab代码示例:“ test”是一个二进制矩阵(但并非我将要研究的所有矩阵都是二进制)。 It contains 5 rows. 它包含5行。 This is dummy code I wrote for row 1. The window manipulation (line 4 of the code) doesn't work, but hopefully this will give you some idea of what I'm trying to do. 这是我为第1行编写的虚拟代码。窗口操作(代码的第4行)不起作用,但希望这能使您对我要执行的操作有所了解。 This code also omits the edges of the matrix as I wasn't sure the best way to make this circular. 这段代码也省略了矩阵的边缘,因为我不确定使这种圆形成为最佳方法。

n=2 % size of neighborhood to look at
p=size(test,2) % length of row

for ii=1+n:p-n;
    if test(1,ii)==1;
        if sum(test(2,(ii-2:ii+2))+test(1,(ii-2:ii+2)))>=2;
           test(2,(ii-2:ii+2))=test(2,(ii-2:ii+2))*2;
        else;
           test(1,ii)=0;
        end;
    end;
end

var=test(2,:);
var(var==1)=0;
var(var>=2)=1;
test(2,:)=var;

Example matrix ("test"): (zeros replaced with '.' for better visualization)

. . . 1 . . 1 . . . . 1 1 1 . . . . . . . 
1 . . . 1 . 1 . . . 1 . . . . . . . . 1 .
. . . . . 1 1 1 . . . 1 . . . . . . 1 . .
. . 1 . . . . . 1 . . . . . . . . . . 1 .
. . . . . . . . . 1 . . . . . 1 . . . . .

Desired output: (zeros replaced with '.' for better visualization)

. . . 1 . . 1 . . . . 1 1 1 . . . . . . .
. . . . 1 . 1 . . . 1 . . . . . . . . . .
. . . . . 1 1 1 . . . 1 . . . . . . . . .
. . . . . . . . 1 . . . . . . . . . . . .
. . . . . . . . . 1 . . . . . . . . . . .

Thanks for your help! 谢谢你的帮助!

Here's an outline to do what I think you're trying to do: 以下是执行我认为您要执行的操作的概述:

Step 1: 第1步:

Find all of the matrix positions that have non-zero elements in their 3x5 neighborhood by summing the values of the neighbors. 通过求和邻居的值,找出在其3x5邻域中具有非零元素的所有矩阵位置。 We don't want to include the current element just yet, hence the zero in the middle. 我们现在还不想包括当前元素,因此中间不包含零。 Input matrix is test . 输入矩阵为test

A(A > 0) = 1;

Step 2: 第2步:

A now contains values > 1, so change them all to 1, leaving the zero values alone. 现在, A包含值> 1,因此将它们全部更改为1,仅保留零值。

 A(A > 0) = 1; 

Step 3: 第三步:

Now that you've identified all of elements with valid neighborhoods, combine test with A by element-wise multiplication leaving only those elements that are both non-zero in test and have non-zero neighbors. 现在,您已经确定了所有具有有效邻域的元素,将testA通过逐元素乘法相结合,仅保留那些在test既非零具有非零邻居的元素。

nCols = size(test, 2);
paddedTest = [zeros(1,nCols);...
              test(:, nCols-1:nCols), test, test(:,1:2);...
              zeros(1,nCols)];

Step 0: 步骤0:

Padding - The above should work as is, but pads both rows and columns with zeros. 填充-上面的命令应按原样工作,但用零填充行和列。 As I understand them, your padding requirements are that you want circular rows and the columns padded with zeros. 据我了解,您的填充要求是您希望圆行和列用零填充。

 nCols = size(test, 2); paddedTest = [zeros(1,nCols);... test(:, nCols-1:nCols), test, test(:,1:2);... zeros(1,nCols)]; 

This obviously changes the size of the matrix you're working on by doing your own padding, so you need to change the convolution parameters in order to return a matrix the same size as test . 显然,这可以通过执行自己的填充来更改正在处理的矩阵的大小,因此您需要更改卷积参数才能返回与test大小相同的矩阵。

 A = conv2(paddedTest, fil, 'valid'); 

I don't have Matlab available right now to test this, but hopefully others will correct any egregious errors I've made. 我目前没有可用的Matlab对此进行测试,但希望其他人可以纠正我犯的任何严重错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM