Replace unwanted voxels with nearest neighbor labeled values
Show older comments
Hello Matlab experts,
I am trying to remove unwanted electrode voxels from 3D labeled images by replacing the electrodes with nearest skin, bone or other labeled tissue voxels. I experimented with the function here: https://www.mathworks.com/matlabcentral/fileexchange/24723-nearest_neighbour_3d
But that didn't generate the result I expected. The function takes given points(electrodes) and their minimum distant nearest candidate points(other tissue voxels). It considers Euclidean distance.
%sz is the size of the 3D label=> lab
[cxx, cyy, czz] = ind2sub(sz, find(lab==10|lab==4)); % candidate points from either skin(10) or bones(4) or other tissues
[gxx, gyy, gzz] = ind2sub(sz, find(lab==20)); % given poinst from electrodes(20)
[nxx, nyy, nzz] = ind2sub(sz, compute_nearest_neighbour([cxx,cyy,czz], [gxx,gyy,gzz])); % nearest points
lab(gxx,gyy,gzz) = lab(nxx,nyy,nzz);
Any other ways?
Thanks in advance.
Accepted Answer
More Answers (1)
Image Analyst
on 3 May 2020
0 votes
The position of x and y are swapped, which makes a difference if the volume is not equal lengths in the x and y direction. Remember ind2sub() returns (row, column) which is NOT (x, y) -- it's (y, x).
36 Comments
banikr
on 3 May 2020
darova
on 3 May 2020
I think it should be switched

darova
on 5 May 2020
Can you use bwlabeln to separate each region?
banikr
on 5 May 2020
darova
on 5 May 2020
Can we start from the start? Can you explain more what you are trying to achieve?
Is this correct?

Are you trying to remove electrodes?
banikr
on 5 May 2020
darova
on 5 May 2020
Simply use this
lab(lab==20) = 10;
banikr
on 5 May 2020
darova
on 5 May 2020
Can you explain more? What electrodes you want to fill/remove and how are you detecting them

banikr
on 5 May 2020
darova
on 5 May 2020
You can dilate each electrode and try to merge them with each area/region separately. If there are commond nodes - close region
if any(A1 & A2)
disp('electrode is close to region')
end
See this idea

darova
on 6 May 2020
Try this example

Image Analyst
on 6 May 2020
Edited: Image Analyst
on 6 May 2020
I've been sitting out for a few reasons. (1) darova seems to be helping you fine. (2) it looks like it might take more time that I can afford to devote, especially if I have to invent data. And mostly, (3) I have no data to work with. If you had uploaded a small 3-D image in a .mat file that illustrates the need, I might have done more on this. Any way you could upload a small chunk of your volume, say a 500x500x300 labeled subvolume that has all 3 or 4 classes in it?
darova
on 6 May 2020
I can handle it.
Image Analyst
on 6 May 2020
Yes, but it would be easier if you had data. And start your own answer so you can get credit for it if he Accepts or Votes for your Answer. Let's promote you from a "Rising Star" to an MVP.
banikr
on 6 May 2020
banikr
on 7 May 2020
darova
on 8 May 2020
- Could you explain why did you take the inverted binary?
- I1 = ~im2bw(I);
It's for bwselect and bwlabel. They want regions to be 'ones' not 'zeros'
- And not clear about this line here:
- II = II + j*(B1 | E2);
You are right, i think better be
II = II + j*E1; % fill not dilated electrode with the same value as region
Did it work? Do you have more questions?
banikr
on 13 May 2020
darova
on 13 May 2020
give me permission
banikr
on 13 May 2020
darova
on 13 May 2020
do you smaller file? it's too large 164Mb
banikr
on 13 May 2020
darova
on 13 May 2020
An error occurs
Error using load
Unknown text on line number 1 of ASCII file lab_superior.nii
"\".
What format is it? .nii
banikr
on 13 May 2020
darova
on 13 May 2020
It's because all electrodes merge with BB==1

try
A = BB + II; % eletrodes and brain together
volumeViewer(A)

banikr
on 14 May 2020
darova
on 14 May 2020
- Did you run with the code I mentioned here in Previous comment ?
Of course
- How do I get original label image from that with one less label(20 for electrodes).
You didn't tell that you want original labeled image. Just replace BB with your original image (without electrodes)
darova
on 14 May 2020
Try this
lab = lab_superior; % the data shared
elec = zeros(size(lab));
elec(lab==20)=1; % select electrodes
BB = lab;
BB(BB==20) = 0; % remove electrodes
nb = unique(BB); % unique regions
[EE,ne] = bwlabeln(elec);
II = BB*0;
for i = 1:ne
E1 = EE == i;
E2 = imdilate(E1,strel('sphere', 4));
for j = nb(:)'
B1 = BB == j; % select one region
tmp = B1 & E2; % compare electrode and region
if any(tmp(:)) % if region and electrode are close
II = II+j*E1;
break
end
end
end
A = II + BB;
banikr
on 14 May 2020
darova
on 14 May 2020
Im sorry, forgot about '0'. The script merged electrodes with background (zero value)
nb = unique(BB); % unique regions
nb(nb==0) = []; % remove '0' from list of regions
Original

After processing

banikr
on 15 May 2020
darova
on 15 May 2020
Can you accept the answer?
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!









