Set to one particular voxels of 3D volumes

2 次查看(过去 30 天)
I have a set of 3D volumes with multiple regions. I would like to set to one the regions with particular numbers and all the rest of voxels to zero. I have the following code to do that but I wonder if there is a better way to write those lines of code.
tissue(tissue ~=24 & tissue ~=31 & tissue ~=32 & tissue ~=33 & tissue ~=37 & tissue ~=38 & tissue ~=39 & tissue ~=40 & tissue ~=41 & ...
tissue ~=42 &tissue ~=43 & tissue ~=44 & tissue ~=48 & tissue ~=49 & tissue ~=54 & tissue ~=55 & tissue ~=56 & tissue ~=57 & ...
tissue ~=58 & tissue ~=59 & tissue ~=60 & tissue ~=61 & tissue ~=62 & tissue ~=63 & tissue ~=64 & tissue ~=65 & tissue ~=66 & ...
tissue ~=67 & tissue ~=70 & tissue ~=72 & tissue ~=73 & tissue ~=74 & tissue ~=76 & tissue ~=77 & tissue ~=81 & ...
tissue ~=82 & tissue ~=83 & tissue ~=84 & tissue ~=85 & tissue ~=86 & ...
tissue ~=87 & tissue ~=89 & tissue ~=90 & tissue ~=91 & tissue ~=92 & tissue ~=93 & tissue ~=94 & tissue ~=96 & ...
tissue ~=97 & tissue ~=101 & tissue ~=102 & tissue ~=103 & tissue ~=104 & tissue ~=105 & tissue ~=106 & tissue ~=107 & ...
tissue ~=108 & tissue ~=109 & tissue ~=110 & tissue ~=113 & tissue ~=114 & tissue ~=115 & tissue ~=116 & tissue ~=117 & ...
tissue ~=118 & tissue ~=119 & tissue ~=120 & tissue ~=121 & tissue ~=122 & tissue ~=123 & tissue ~=124 & tissue ~=125 & ...
tissue ~=126 & tissue ~=129 & tissue ~=130 & tissue ~=133 & tissue ~=134 & tissue ~=135 & tissue ~=136 & tissue ~=137 & ...
tissue ~=138 & tissue ~=139 & tissue ~=140 & tissue ~=141 & tissue ~=142 & tissue ~=143 & tissue ~=144 & tissue ~=145 & ...
tissue ~=146 & tissue ~=147 & tissue ~=148 & tissue ~=149 & tissue ~=150 & tissue ~=151 & tissue ~=152 & tissue ~=153 & ...
tissue ~=154 & tissue ~=155 & tissue ~=156 & tissue ~=157 & tissue ~=158 & tissue ~=161 & tissue ~=162 & tissue ~=163 & ...
tissue ~=164 & tissue ~=165 & tissue ~=166 & tissue ~=167 & tissue ~=168 & tissue ~=169 & tissue ~=170 & tissue ~=171 & ...
tissue ~=172 & tissue ~=173 & tissue ~=174 & tissue ~=175 & tissue ~=176 & tissue ~=177 & tissue ~=178 & tissue ~=179 & ...
tissue ~=180 & tissue ~=181 & tissue ~=182 & tissue ~=183 & tissue ~=184 & tissue ~=185 & tissue ~=186 & tissue ~=187 & ...
tissue ~=188 & tissue ~=191 & tissue ~=192 & tissue ~=193 & tissue ~=194 & tissue ~=195 & tissue ~=196 & tissue ~=197 & ...
tissue ~=198 & tissue ~=199 & tissue ~=200 & tissue ~=201 & tissue ~=202 & tissue ~=203 & tissue ~=204 & tissue ~=205 & ...
tissue ~=206 & tissue ~=207 & tissue ~=208)=0;
tissue(tissue ~= 0) = 1;

回答(1 个)

Maneet Kaur Bagga
Maneet Kaur Bagga 2023-8-31
Hi @Demy,
As per my understanding, based on the given code the voxels are classified into two groups, the one with the given set of indices are classofoed as 0 and all the other as 1s.
To optimize the code provided, you may first create an array of voxels which you want to classify as 0. Assuming the voxel array name as regions_zero and the given matrice is tissue, ismember(A,B) function can be used which will return an array containing the logical indexing if the voxel is found in both the tissue matrix and the regions_zero matrix. Using the logical indexing we can group the two regions using the below code:
% Assuming you have a 3D volume named 'tissue' and you want to set specific regions to zero and the rest to one
% Define the regions you want to set to zero
regions_zero = [24, 31, 32, 33, 37, 38, 39, 40, 41, 42, 43, 44, 48, 49, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, ...
66, 67, 70, 72, 73, 74, 76, 77, 81, 82, 83, 84, 85, 86, 87, 89, 90, 91, 92, 93, 94, 96, 97, 101, 102, 103, 104, ...
105, 106, 107, 108, 109, 110, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 129, 130, 133, ...
134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, ...
157, 158, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, ...
182, 183, 184, 185, 186, 187, 188, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, ...
207, 208];
% Set the specified regions to zero and the rest to one
tissue(~ismember(tissue, regions_zero)) = 1;
tissue(tissue ~= 1) = 0;
You can refer to the following documentation to know more about the ismember() function:
Thank You!
Maneet Bagga

类别

Help CenterFile Exchange 中查找有关 3-D Volumetric Image Processing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by