【问题标题】:Prevent to patch if the area is already filled如果该区域已被填充,请防止修补
【发布时间】:2015-07-02 00:15:53
【问题描述】:

这对我的代码来说是一个相当艰巨的挑战。首先,我放在这里的代码不可运行,因为我使用的是 Excel 表格(但如果人们想尝试使用我的代码,我很乐意通过电子邮件发送给它)。

我有一张 Excel 表格,其中包含我拍摄的显微图像中横截面纤维的数据。信息基本是:location的部分,areaangle的轮换。

由此我计算出方向角 Phi 和 Gamma。之后,我使用 scatter 函数为每个 Phi 角度值绘制不同颜色的点。我在 10 度的范围内使用恒定颜色。这给了我这样的图片:

现在我的目标是计算每个同质区域的面积。所以我寻找一种方法来绘制假设 -10 +10 区域内的所有点(我现在做 20 度,但之后会做 10 度)。我用了一个look,得到了这样的图片:

白色对应于我选择的范围内的点。之后,我使用 MATLAB 中的工具箱将每个点转换为一个像素。所以我会得到一个带有大量白色像素的黑色背景,然后我使用imdilate 来制作圆圈、填充孔并用特定颜色隔离每个区域。最后,我使用边界和补丁函数来创建每个边界并用颜色填充它们。我得到一张这样的照片:

这是我想要的,我可以得到每个区域的面积和总面积(我使用阈值来丢弃小区域)。然后我为每个区域运行几次代码,然后使用imfuse 将它们重新组合在一起,看看它是什么样子。

问题是,它们重叠很多,那是因为我的数据中有一些错误,因此一些蓝点会变成红色等等。 所以我想运行一次代码,然后当我用另一个范围重新运行它时,它会做同样的事情,但在之前已经绘制过一些东西时不考虑值。

我试图做到这一点,在运行一次之后,保存矩阵 bw4 并在绘制黑白图片时添加一个条件,如果 Phi 在我的范围内并且这里没有白色,那么你可以放白色,否则它是黑色的。但这似乎不起作用。

我知道这是一件很复杂的事情,但我会很感激任何想法,并愿意通过电子邮件或其他方式聊天。我现在正在放完整的代码,如果你想在你的计算机上运行它并自己查看,我可以将我的 Excel 表发送给你。

clearvars -except data colheaders bw4
close all
clc
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CHANGE DATA FOR EACH SAMPLE %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

cd 'C:\Users\dkarta\Desktop\Sample 12\12.6'
data=xlsread('Sample12_6res.xlsx');

cd 'C:\Users\dkarta\Documents\MATLAB'

%data=Sample121res; % Data name
imax=length(data); % Numbers of rows in data sheet
y=11900; % Number of pixels in the y on image j
%%

data(:,15)=data(:,9)*pi/180; % Convers Column 9 (angle of rotation) in rads
data(:,16)=y-data(:,6); % Reset the Y coordinate axis to bottom left
delta = 0 : 0.01 : 2*pi; % Angle in paramteric equations 
theta=45*pi/180; % Sample cutting angle in rads

%AA=[data(:,5)' data(:,16)' phi']
% Define colors
beta=acos(data(1:imax,8)./data(1:imax,7));%./acos(0);
phi=atan(sin(beta).*cos(data(1:imax,15))./(sin(theta)*sin(beta).*sin(data(1:imax,15))+cos(theta)*cos(beta)))/(pi/2);
phi2=phi/2+1/2; % Scales in plane angle phi between 0 and 1
gamma=atan((cos(theta)*sin(beta).*sin(data(1:imax,15))-sin(theta)*cos(beta))./...
    (sin(theta)*sin(beta).*sin(data(1:imax,15))+cos(theta)*cos(beta)))/(pi/2);
gamma2=gamma+1/2; % Scales out of plane angle gamma between 0 and 1 
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% MESHGRID AND COLOURMAP %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1=data(1:imax,5);
y1=data(1:imax,16);
z1=phi*90;
z2=gamma*90;
n=300;
%Create regular grid across data space
[X,Y] = meshgrid(linspace(min(x1),max(x1),n), linspace(min(y1),max(y1),n));

% Creating a colormap with 10 degree constant colors
map4=[0 0 1;0 1/3 1;0 2/3 1; 0 1 1;0 1 2/3;0 1 1/3;0 1 0;1/3 1 0;2/3 1 0;1 1 0;1 0.75 0;1 0.5 0;1 0.25 0;1 0 0;0.75 0 0.25;0.5 0 0.5;0.25 0 0.75; 0 0 1];
Colormap4=colormap(map4);
h=colorbar;
caxis([-90 90])
set(h, 'YTick', [-90:10:90])
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% PLOT USING SCATTER - ISOLATE SOME REGIONS %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a=-10; % Lower boundary for angle interval
b=10; % Upper boundary for angle interval
c=z1>a & z1 < b;
c=c.*1;
%j=1;

y1=(y1-min(y1)+1);
y2=max(y1)-y1+1;
[X1,Y1]=meshgrid(1:500,1:500);
griddata(x1,y2,c,X1,Y1);
clear c1


for i=1:imax
    if z1(i)< b && z1(i)> a %&& bw4(round(y1(i)),round(x1(i))) == 0
        c(i) = 1;
        c1(round(y2(i)),round(x1(i)))=1;
    else
        c(i)= 0;
        c1(round(y2(i)),round(x1(i)))=0;
    end

end
C=[c c c];

%c(find(c==0)) = NaN;
%contourf(X,Y,griddata(x1,y1,c,X,Y),100,'EdgeColor', 'None')
figure(1), scatter(x1,y1,3,z1,'filled');
axis equal
axis ([0 8000 0 12000]) 
axis off
figure(2), scatter(x1,y1,3,C,'filled');
axis equal
axis ([0 8000 0 12000]) 
axis off


se=strel('disk',50,8);
bw2=imdilate(c1,se);
bw4=bwlabel(bw2);
bw3=imfill(bw4,'holes');
max(bw4(:));
figure(3),imshow(c1,'InitialMagnification', 10);
figure(4), imshow(bw2,'InitialMagnification', 10);
figure(5), imshow(bw3,'InitialMagnification', 10);
figure(6),imshow(label2rgb(bw4),'InitialMagnification', 10);

k=ones(max(bw4(:)),1);
clear bw5
for i=1:length(x1)
    if bw3(round(y2(i)),round(x1(i))) ~= 0
        m=bw3(round(y2(i)),round(x1(i)));
        bw5{m}(k(m),1)=x1(i); bw5{m}(k(m),2)=y2(i);
        k(m)=k(m)+1;
    end
end

figure(7), imshow(~c1,'InitialMagnification', 10);
hold on 
for i=1:max(bw4(:))
    %scatter(bw5{i}(:,1),bw5{i}(:,2))
    j = boundary(bw5{i}(:,1),bw5{i}(:,2),0.5);
    %poly=convhull(bw5{i}(:,1),bw5{i}(:,2));
    %plot(bw5{i}(poly,1),bw5{i}(poly,2)), title('convhull')
    if polyarea(bw5{i}(j,1),bw5{i}(j,2))> 10^5;
        patch(bw5{i}(j,1),bw5{i}(j,2),'r'), title('boundary')
        indexminy(i)=find(min(bw5{i}(:,2)) == bw5{i}(:,2));
        indexminx(i)=find(min(bw5{i}(:,1)) == bw5{i}(:,1));
        indexmaxy(i)=find(max(bw5{i}(:,2)) == bw5{i}(:,2));
        indexmaxx(i)=find(max(bw5{i}(:,1)) == bw5{i}(:,1));
        %xmin = bw5{i}(indexminx); xmax = bw5{i}(indexmaxx); 
        %ymin = bw5{i}(indexminy); ymax = bw5{i}(indexmaxy);
        str=[(indexminx(i)+indexmaxx(i))/2,(indexminy(i)+indexmaxy(i))/2,'Region no.',num2str(i)];
        text((min(x1(i))+max(x1(i)))/2,(min(y1(i))+max(y1(i)))/2,str)
        polya(i)=polyarea(bw5{i}(j,1),bw5{i}(j,2));
    end
end
spolya=sum(polya(:))
print -dpng -r500 B

只是为了给你看更多我融合它们时的图片:

当我融合时:


正如你所看到的,它们重叠,这是我不想要的,所以我希望我创建的每个图像都“知道”我在之前的运行中正在做什么,这样它就不会重叠。我想获得每个区域的百分比面积,如果它们重叠,我无法使用样本的实际总面积,结果是错误的。

【问题讨论】:

  • 我已经盯着第一张图片看了大约 10 分钟。 +1。
  • 大声笑非常感谢你。我真的不知道该问谁说实话。我不太擅长 Matlab,通过这个项目(对于我的硕士),我学到了很多我不知道存在的函数。我只是希望这是有道理的,并且有人能够指导我。你可以在我的代码中看到我已经做了一些 cmets 哦,我要添加什么来放置这个额外的约束。
  • @DorianKartalovski - 这当然是一个有趣的问题。您能否上传您的数据以便我们重建您的问题?我有一些想法想尝试一下。
  • 我要做的是在我的谷歌驱动器上上传我的 excel 表并将超链接放在这里,因为大约有 10 万行。 drive.google.com/file/d/0B_cS79c9GkkOSTUtMEdvRHhlN2c/…
  • 我想你会在这里得到更好的答案

标签: excel matlab image-processing patch boundary


【解决方案1】:

我没有我的 matlab 工作,但这是你需要做的。

在第一次运行时,制作一个与您的图像大小相等的零数组

already_taken = zeros(size(bw3));

然后在每次运行时,您可以填充此迭代所占用的区域。因此,在代码的末尾,将输出保存到 png 中,将其读回类似于

this_png = rgb2gray(imread(current_png_path))>threshold;

通过进行一些阈值处理将其转换为逻辑数组,并将这些值添加到已获取的值中。所以在代码的最后,做一个

already_taken = already_taken | this_png; % You might need to check if you need a single | or a double ||

所以现在你有一张已经拍摄的像素的图像,请确定我不允许 bw2 首先采用这些值

bw2(already_taken) = 0;

在代码的最后,当我想写我的 png 时,我的智能边界创建可能再次进入了 already_taken 区域,所以我必须再次进行检查。据我了解,此边界是根据您的 bw5 创建的。因此,无论您在哪里填写此矩阵,都请尝试像我在上面对 bw2 所做的那样进行类似的检查。

我希望这会有所帮助。

【讨论】:

  • 嘿,感谢您的帮助,我一直在努力改进它,我想我已经在一些帮助下弄清楚了。我明天会更新问题
猜你喜欢
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多