Image processing 在MATLAB中选择矩阵的对角元素
考虑MATLAB中的以下矩阵: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 151617181921 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36373839404142 43444546474849Image processing 在MATLAB中选择矩阵的对角元素,image-processing,matlab,Image Processing,Matlab,考虑MATLAB中的以下矩阵: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 151617181921 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36373839404142 43444546474849 我必须为这样的7 x 7窗口(移动)图像生成方向变异函数。我将使用nlfilter进行处理,但为了开发计算变异函数的函数,我无法决定如何在窗口中选择元素。例如,当我考虑中心值25时,在电子战方向上,我只需要考虑25
我必须为这样的7 x 7窗口(移动)图像生成方向变异函数。我将使用
nlfilter
进行处理,但为了开发计算变异函数的函数,我无法决定如何在窗口中选择元素。例如,当我考虑中心值25时,在电子战方向上,我只需要考虑25, 26, 27和28;在NE方向上,当滞后选择为1时,必须考虑25, 19, 13和07。是否有任何标准命令可以执行此操作?您可以编写一个函数来轻松获取这些元素:
A = [01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49];
c = (size(A)+1)/2;
EW = A(c(1),c(2):end)
NE = diag(A(c(1):-1:1,c(2):end))
只需在函数(最好是m文件)中编写此代码,执行操作并将结果传回
diag
函数返回矩阵的对角元素(或在传递向量时返回对角矩阵)。您也可以这样做:
A = eye(5);
v = A(1:size(A,1)+1:end);
导致
v = [1 1 1 1 1]
这是通用矩阵解决方案(不适用于MATLAB) 假设矩阵AxB=
[01 AA 03 04 05 06 07
08 09 AA 11 12 13 AA
AA 16 17 AA 19 AA 21
22 AA 24 25 AA 27 28
AA 30 AA 32 33 34 35
36 AA 38 AA 40 41 42
43 44 AA 46 AA 48 49];
在这个矩阵中,我们要连续搜索3次AA对角的外观
解决方案:-
第一步
对于整个矩阵,我们必须创建4个独立的for循环,以连续3次搜索AA的外观
我添加了一个方法,用户可以通过该方法搜索所有循环并找到该项
local function check_win( matrx_table)
local counter = 1
local term = "AA"
local D = 1
-- for horizontal check for win---
for i = 1, no_rows, 1 do
for j= 1, no_Columns, 1 do
if((j+1) <= no_Columns) then
if(table_mXn[i][j] == term and table_mXn[i][j+1] == term)then
counter = counter + 1;
else
counter = 1
end
if(counter == 3)then
return counter
end
end
end
end
counter = 1
-- for vertical check for win--
for i = 1, no_Columns, 1 do
for j= no_rows, 1, -1 do
if((j-1) >= 1) then
if(table_mXn[j][i] == term and table_mXn[j-1][i] == term)then
counter = counter + 1;
else
counter = 1
end
if(counter == 3)then
return counter
end
end
end
end
counter = 1
D = 1
-- for diagonol left half check for win in figure loop 1--
for m = 1, no_rows, 1 do
D = 1
for i =m, no_rows,1 do
if(i+1 <= no_rows and D+1 <= no_Columns)then
if(table_mXn[i][D] == term and table_mXn[i+1][D+1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D + 1
end
end
end
counter = 1
D = 1
-- for diagonol right half check for win in figure loop 2--
for m = 1, no_rows, 1 do
D = m
for i =1, no_rows,1 do
if(i+1 <= no_rows and D+1 <= no_Columns)then
if(table_mXn[i][D] == term and table_mXn[i+1][D+1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D + 1
end
end
end
counter = 1
D = 1
-- for diagonol left half check for win in figure loop 3--
for m = 1, no_rows, 1 do
D = no_Columns
for i =m, no_rows,1 do
if(i+1 <= no_rows and D-1 >= 1)then
if(table_mXn[i][D] == term and table_mXn[i+1][D-1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D - 1
end
end
end
counter = 1
D = 1
-- for diagonol left half check for win in figure loop 4--
for m = no_Columns, 1, -1 do
D = m
for i =1, no_rows,1 do
if(i+1 <= no_rows and D-1 >= 1)then
if(table_mXn[i][D] == term and table_mXn[i+1][D-1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D - 1
end
end
end
end
local function check\u win(matrx\u表)
本地计数器=1
本地术语=“AA”
局部D=1
--用于水平检查以获得胜利---
对于i=1,没有行,则为1
对于j=1,无_列,1 do
如果((j+1)=1),则
如果(表_mXn[j][i]==术语和表_mXn[j-1][i]==术语),则
计数器=计数器+1;
其他的
计数器=1
结束
如果(计数器==3),则
返回计数器
结束
结束
结束
结束
计数器=1
D=1
--对于Diagnol左半部分,检查图1循环中的win--
对于m=1,没有行,则为1
D=1
对于i=m,没有行,1 do
如果(i+1)