gpt4 book ai didi

arrays - 距Matlab中的线集合一定距离的表面积,类似于轮廓?

转载 作者:行者123 更新时间:2023-12-02 03:57:03 25 4
gpt4 key购买 nike

亲爱的 stackoverflow 用户,

几年前,我用了几个月的 Mathematica。在几年没有编程之后,我现在做一个研究项目,作为一名学生,我在其中使用 Matlab。我在 stackoverflow 上找到了很多很好的帮助,但现在我遇到了以下问题:

我有一个矩形网格上节点之间连接的数据集,每个节点都可能与其 8 个邻居有连接。我的测量采用 3 x n 矩阵的形式,其中前两个值指定一个节点,第三个值指定它们是否连接,网格的大小是预先确定的。通常有大约十条线路来自至少彼此相邻的两个或三个节点。我的研究项目的目标是计算这组线周围距离 r 处的面积。

到目前为止,我已经能够使用下面的代码绘制线条,为此我使用了 stackoverflow 上的一些代码,这非常有用。但是我无法在一定距离处获得围绕它的等高线(我希望用它来计算该等高线内的面积)。 gplot 函数返回两个向量,每行有两个坐标,我发现很难将其转换为更有用的东西。我尝试在离线一定距离处定义一个值 Z,随着离线的距离而下降,所以我从这些线得到一个斜率。从这个斜率我可以计算等高线。但是,因为这些线只是坐标,所以我不知道如何计算到那条线的距离,而不是它们何时会成为函数。

我真的很茫然。我希望我在这里清楚地发布了我的问题。这是我第二次发布这个问题,我现在已经在代码和图片中添加了注释以更好地解释自己。感谢您提供的任何建议!

到目前为止,xls 文件是我上面提到的 3 x n 矩阵,我还在下面的代码中以矩阵形式编写了它的内容,所以我的问题更容易理解:

%# here i set my data/constants

filename='random.xls'; file=xlsread(filename); y=width; x=length;

%# random.xls looks approximately like this, after xlsread(filename) you get

file=[21 22 1;
21 20 1;
15 16 1;
15 14 1;
15 23 1;
14 22 1;
14 21 1;
22 15 1;
23 14 1;
24 15 1;
6 15 1;
5 14 1;
7 14 1;
8 15 1];

%# predefined width and length, i usually get this from the file

width=8; length=4;

%# here i create my adjaceny matrix in a elegant way user amro posted on stackoverflow
%# however i immediately multiply it by 0, creating a y*x by y*x matrix with all zeroes

[X Y] = meshgrid(1:x,1:y); X = X(:); Y = Y(:);
adjacency = squareform( pdist([X Y], 'chebychev') == 1 ); adjacency=adjacency*0;

%# here i take the matrix "file" for which the first two values are node numbers and
%# the third value designates whether there is a connection between the two nodes to
%# fill in the connections in the adjacencymatrix

[nrows,ncols]=size(file);
for r = 1:nrows
if file(r,3)==1
adjacency(file(r,1),file(r,2))=1;
end
end
adjacency=(adjacency+adjacency.');

%# plots the adjacencymatrix

subplot(121), spy(adjacency)

%# plots the connections and designates the nodes, note that the numbers designating
%# the nodes do not match original data, this is a separate problem i have not solved

[xx yy] = gplot(adjacency, [X Y]);
subplot(122), plot(xx, yy, 'ks-', 'MarkerFaceColor','b')

%# these last lines of code for plotting the numbers of the grid i do not fully
%# understand, in here is the cause for the numbers not matching the original data

axis([0 x+1 0 y+1])
[X Y] = meshgrid(1:x,1:y);
X = reshape(X',[],1) + 0.1; Y = reshape(Y',[],1) + 0.1;
text(X, Y(end:-1:1), cellstr(num2str((1:x*y)')) )
xlabel('length')
ylabel('width')
title(filename)

为了澄清我的问题,我添加了这两张图片:当前地 block http://imgur.com/5uPd4我想知道的区域http://imgur.com/WsIbg

最佳答案

从 Matlab 中的线集合中找到距离为 r 的等值线或等高线内表面积的解决方案,通过图形处理(扩张)进行近似,而不是精确或有效的 awnser!

我做了一个近似值,所以这不是一个精确的 awnser,也不是有效的编码。我的一位研究机器视觉的 friend 建议将线条转换为像素,然后用圆盘对图像进行膨胀,之后像素数就是表面积的度量:

%# constants and variables
minx = min(xx);
miny = min(yy);
maxx = max(xx);
maxy = max(yy);
rangex = maxx - minx;
rangey = maxy - miny;
borderRelNum = sqrt(2);
electrodeToImageScaleFactor = 100;
imsizex = 2*(maxx+borderRelNum)*electrodeToImageScaleFactor+2;
imsizey = 2*(maxy+borderRelNum)*electrodeToImageScaleFactor+2;
im = zeros(imsizex, imsizey);
grayscalevalue = 255;
disksize = round(borderRelNum*electrodeToImageScaleFactor);

%# transformation matrices
centerElectrodeSpace = [1, 0, -(minx + maxx) / 2;
0, 1, -(miny + maxy) / 2;
0, 0, 1 ];

scaleElectrodeToImage = [electrodeToImageScaleFactor , 0, 0;
0, electrodeToImageScaleFactor , 0;
0, 0, 1 ];

centerImageSpace = [ 1, 0, imsizex / 2;
0, 1, imsizey / 2;
0, 0, 1 ];

electrodeToImage = centerImageSpace * scaleElectrodeToImage * centerElectrodeSpace;

%# transformation

for i = 0:(size(xx,1) / 3 - 1)
p1 = [xx(i*3 + 1); yy(i*3 + 1); 1];
p2 = [xx(i*3 + 2); yy(i*3 + 2); 1];

p1im = electrodeToImage * p1
p2im = electrodeToImage * p2

lx = linspace( min( p1im(1), p2im(1) ), max( p1im(1), p2im(1) ), borderRelNum*electrodeToImageScaleFactor )
ly = linspace( min( p1im(2), p2im(2) ), max( p1im(2), p2im(2) ), borderRelNum*electrodeToImageScaleFactor )

index = sub2ind(size(im),round(lx),round(ly));
im(index) = grayscalevalue;
end

%# Now dilate and count pixels
se = strel('disk', disksize, 0);
im = imdilate(im, se);
image(im)
colormap(gray)
sum(sum(im/grayscalevalue))*(1/electrodeToImageScaleFactor^2)

如果有人能够更优雅、高效或更精确地解决我的问题,我仍然会非常感激。但现在就可以了。

-edit- 好吧,这确实非常低效,我的电脑已经在我的数据集(10 个 xls 文件,不多)上处理数字 30 分钟了,现在仍然在文件 1,如果我查看工作空间值,似乎

关于arrays - 距Matlab中的线集合一定距离的表面积,类似于轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12138160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com