Skip to content

Commit

Permalink
Try Asjad variant of Sobel filter
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Aug 26, 2021
1 parent df652c1 commit 9507f36
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 64 deletions.
123 changes: 61 additions & 62 deletions MRIcroGL.lps
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<ComponentName Value="GLForm1"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<TopLine Value="1062"/>
<CursorPos X="77" Y="1064"/>
<TopLine Value="5588"/>
<CursorPos X="27" Y="5602"/>
<UsageCount Value="204"/>
<Loaded Value="True"/>
<LoadedDesigner Value="True"/>
Expand Down Expand Up @@ -151,31 +151,31 @@
<EditorIndex Value="10"/>
<TopLine Value="147"/>
<CursorPos X="20" Y="154"/>
<UsageCount Value="57"/>
<UsageCount Value="58"/>
<Loaded Value="True"/>
</Unit14>
<Unit15>
<Filename Value="slices2D.pas"/>
<EditorIndex Value="5"/>
<TopLine Value="126"/>
<CursorPos X="3" Y="23"/>
<UsageCount Value="45"/>
<UsageCount Value="46"/>
<Loaded Value="True"/>
</Unit15>
<Unit16>
<Filename Value="niftis.pas"/>
<IsVisibleTab Value="True"/>
<EditorIndex Value="1"/>
<TopLine Value="384"/>
<CursorPos Y="399"/>
<UsageCount Value="40"/>
<TopLine Value="369"/>
<CursorPos X="43" Y="396"/>
<UsageCount Value="41"/>
<Loaded Value="True"/>
</Unit16>
<Unit17>
<Filename Value="nifti.pas"/>
<IsVisibleTab Value="True"/>
<EditorIndex Value="8"/>
<TopLine Value="6939"/>
<CursorPos X="25" Y="6967"/>
<TopLine Value="780"/>
<CursorPos X="3" Y="810"/>
<UsageCount Value="107"/>
<Loaded Value="True"/>
</Unit17>
Expand Down Expand Up @@ -204,17 +204,16 @@
<Unit21>
<Filename Value="../Metal-Demos/common/SimdUtils.pas"/>
<EditorIndex Value="9"/>
<TopLine Value="13"/>
<CursorPos X="27" Y="31"/>
<UsageCount Value="54"/>
<CursorPos X="95" Y="7"/>
<UsageCount Value="55"/>
<Loaded Value="True"/>
</Unit21>
<Unit22>
<Filename Value="../Metal-Demos/common/VectorMath.pas"/>
<EditorIndex Value="4"/>
<TopLine Value="14"/>
<CursorPos X="27" Y="27"/>
<UsageCount Value="71"/>
<UsageCount Value="72"/>
<Loaded Value="True"/>
</Unit22>
<Unit23>
Expand Down Expand Up @@ -418,7 +417,7 @@
<EditorIndex Value="2"/>
<TopLine Value="603"/>
<CursorPos X="17" Y="614"/>
<UsageCount Value="19"/>
<UsageCount Value="20"/>
<Loaded Value="True"/>
</Unit51>
</Units>
Expand All @@ -428,123 +427,123 @@
<JumpHistory Count="30" HistoryIndex="29">
<Position1>
<Filename Value="nifti.pas"/>
<Caret Line="8938" Column="27" TopLine="8910"/>
<Caret Line="890" Column="16" TopLine="877"/>
</Position1>
<Position2>
<Filename Value="nifti.pas"/>
<Caret Line="8977" Column="23" TopLine="8949"/>
<Caret Line="899" Column="15" TopLine="877"/>
</Position2>
<Position3>
<Filename Value="nifti.pas"/>
<Caret Line="8986" Column="27" TopLine="8958"/>
<Caret Line="905" Column="110" TopLine="877"/>
</Position3>
<Position4>
<Filename Value="nifti.pas"/>
<Caret Line="8993" Column="10" TopLine="8965"/>
<Caret Line="906" Column="110" TopLine="878"/>
</Position4>
<Position5>
<Filename Value="nifti.pas"/>
<Caret Line="8996" Column="27" TopLine="8968"/>
<Caret Line="890" Column="13" TopLine="874"/>
</Position5>
<Position6>
<Filename Value="nifti.pas"/>
<Caret Line="9002" Column="9" TopLine="8974"/>
<Filename Value="niftis.pas"/>
<Caret Line="8" Column="76"/>
</Position6>
<Position7>
<Filename Value="nifti.pas"/>
<Caret Line="9005" Column="27" TopLine="8977"/>
<Filename Value="niftis.pas"/>
<Caret Line="33" Column="25" TopLine="4"/>
</Position7>
<Position8>
<Filename Value="nifti.pas"/>
<Caret Line="9011" Column="9" TopLine="8983"/>
<Filename Value="niftis.pas"/>
<Caret Line="396" Column="17" TopLine="369"/>
</Position8>
<Position9>
<Filename Value="nifti.pas"/>
<Caret Line="281" Column="9" TopLine="264"/>
<Filename Value="niftis.pas"/>
<Caret Line="397" Column="45" TopLine="369"/>
</Position9>
<Position10>
<Filename Value="nifti.pas"/>
<Caret Line="287" Column="95" TopLine="264"/>
<Caret Line="805" Column="86" TopLine="804"/>
</Position10>
<Position11>
<Filename Value="nifti.pas"/>
<Caret Line="6836" Column="139" TopLine="6808"/>
<Caret Line="2941" Column="31" TopLine="2913"/>
</Position11>
<Position12>
<Filename Value="mainunit.pas"/>
<Caret Line="3161" Column="37" TopLine="3147"/>
<Filename Value="nifti.pas"/>
<Caret Line="5863" Column="57" TopLine="5835"/>
</Position12>
<Position13>
<Filename Value="mainunit.pas"/>
<Caret Line="3179" Column="93" TopLine="3150"/>
<Filename Value="nifti.pas"/>
<Caret Line="5875" Column="44" TopLine="5847"/>
</Position13>
<Position14>
<Filename Value="mainunit.pas"/>
<Caret Line="399" Column="19" TopLine="382"/>
<Filename Value="nifti.pas"/>
<Caret Line="5877" Column="44" TopLine="5849"/>
</Position14>
<Position15>
<Filename Value="nifti.pas"/>
<Caret Line="6858" Column="23" TopLine="6845"/>
<Caret Line="5900" Column="43" TopLine="5872"/>
</Position15>
<Position16>
<Filename Value="nifti.pas"/>
<Caret Line="6860" Column="16" TopLine="6845"/>
<Caret Line="5902" Column="43" TopLine="5874"/>
</Position16>
<Position17>
<Filename Value="nifti.pas"/>
<Caret Line="816" Column="23" TopLine="811"/>
<Caret Line="8410" Column="61" TopLine="8382"/>
</Position17>
<Position18>
<Filename Value="nifti.pas"/>
<Caret Line="1439" Column="15" TopLine="1410"/>
<Caret Line="8426" Column="28" TopLine="8398"/>
</Position18>
<Position19>
<Filename Value="nifti.pas"/>
<Caret Line="1440" Column="51" TopLine="1411"/>
<Caret Line="154" Column="28" TopLine="138"/>
</Position19>
<Position20>
<Filename Value="nifti.pas"/>
<Caret Line="1634" Column="51" TopLine="1606"/>
<Filename Value="mainunit.pas"/>
<Caret Line="8756" Column="36" TopLine="8748"/>
</Position20>
<Position21>
<Filename Value="nifti.pas"/>
<Caret Line="1636" Column="32" TopLine="1608"/>
<Filename Value="mainunit.pas"/>
<Caret Line="8778" Column="34" TopLine="8750"/>
</Position21>
<Position22>
<Filename Value="nifti.pas"/>
<Caret Line="1638" Column="33" TopLine="1610"/>
<Filename Value="mainunit.pas"/>
<Caret Line="5611" Column="19" TopLine="5599"/>
</Position22>
<Position23>
<Filename Value="nifti.pas"/>
<Caret Line="6934" Column="20" TopLine="6907"/>
<Filename Value="mainunit.pas"/>
<Caret Line="5663" Column="22" TopLine="5634"/>
</Position23>
<Position24>
<Filename Value="nifti.pas"/>
<Caret Line="8294" Column="6" TopLine="8270"/>
<Filename Value="mainunit.pas"/>
<Caret Line="5712" Column="23" TopLine="5683"/>
</Position24>
<Position25>
<Filename Value="nifti.pas"/>
<Caret Line="6840" Column="19" TopLine="6834"/>
<Filename Value="mainunit.pas"/>
<Caret Line="5719" Column="29" TopLine="5690"/>
</Position25>
<Position26>
<Filename Value="nifti.pas"/>
<Caret Line="6892" TopLine="6864"/>
<Filename Value="mainunit.pas"/>
<Caret Line="5738" Column="24" TopLine="5709"/>
</Position26>
<Position27>
<Filename Value="nifti.pas"/>
<Caret Line="6924" Column="12" TopLine="6896"/>
<Filename Value="mainunit.pas"/>
<Caret Line="5780" Column="31" TopLine="5752"/>
</Position27>
<Position28>
<Filename Value="nifti.pas"/>
<Caret Line="6944" Column="30" TopLine="6916"/>
<Filename Value="mainunit.pas"/>
<Caret Line="5792" Column="24" TopLine="5763"/>
</Position28>
<Position29>
<Filename Value="nifti.pas"/>
<Caret Line="6950" Column="23" TopLine="6922"/>
<Filename Value="mainunit.pas"/>
<Caret Line="6082" Column="24" TopLine="6053"/>
</Position29>
<Position30>
<Filename Value="nifti.pas"/>
<Caret Line="6959" Column="12" TopLine="6931"/>
<Caret Line="195" Column="28" TopLine="181"/>
</Position30>
</JumpHistory>
<RunParams>
Expand Down
2 changes: 1 addition & 1 deletion MRIcroGL_llvm.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
</CompilerMessages>
<CustomOptions Value="-dxDisableLCLGIF
-dxDisableLCLPNM
-dNoMYPY
-dMYPY
-Clflto
-Clfltonosystem"/>
</Other>
Expand Down
5 changes: 4 additions & 1 deletion mainunit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5602,7 +5602,8 @@ procedure TGLForm1.SaveNIfTIMenuClick(Sender: TObject);
niftiVol: TNIfTI;
fnm : string;
begin
if not vols.Layer(0,niftiVol) then exit;
//if not vols.Layer(0,niftiVol) then exit; //save background layer
if not vols.Layer(LayerList.ItemIndex,niftiVol) then exit; //save selected layer
fnm := NiftiSaveDialogFilename(false, niftiVol.Filename);
if fnm = '' then exit;
niftiVol.SaveFormatBasedOnExt(fnm);
Expand Down Expand Up @@ -8754,6 +8755,8 @@ function versionStr: string;
{$ENDIF}
{$IFDEF CPULLVM}
w := w + ' LLVM';
{$ELSE}
w := w + ' FPC';
{$ENDIF}
w := w + chr(13)+chr(10);
w := w + 'Author: Chris Rorden' +kEOLN;
Expand Down
45 changes: 45 additions & 0 deletions nifti.pas
Original file line number Diff line number Diff line change
Expand Up @@ -803,13 +803,18 @@ function correlR(var x, y: TFloat32s): single;
{$ENDIF} //FASTCORREL2
function TNIfTI.EdgeMap(isSmooth: boolean): TUInt8s;
//https://afni.nimh.nih.gov/pub/dist/doc/htmldoc/programs/3dedge3_sphx.html
// 2dedge3 uses R. Deriche formula https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.476.5736&rep=rep1&type=pdf
//https://en.wikipedia.org/wiki/Sobel_operator#Extension_to_other_dimensions
//optional compile:
// Asjad and M. Deriche: https://www.researchgate.net/publication/283280465_A_new_approach_for_salt_dome_detection_using_a_3D_multidirectional_edge_detector
//{$DEFINE ASJAD}
var
x, y, z, vx, nXYZ, nXY, nX, nY, nZ: int64;
llh, mlh, hlh, lmh, mmh, hmh, lhh, mhh, hhh, //voxel neighbors: slice above (high)
llm, mlm, hlm, lmm, hmm, lhm, mhm, hhm, //voxel neighbors: same slice (middle)
lll, mll, hll, lml, mml, hml, lhl, mhl, hhl, //voxel neighbors: slice below (low)
gx, gy, gz, mn, mx, scale255: single;
{$IFDEF ASJAD}g45, g135: single; {$ENDIF}
vol8: TUInt8s;
vol32: TFloat32s;
begin
Expand Down Expand Up @@ -866,7 +871,47 @@ function TNIfTI.EdgeMap(isSmooth: boolean): TUInt8s;
gx := (hlh + hmh + hhh + hlm + hmm + hhm + hll + hml + hhl) - (llh + lmh + lhh + llm + lmm + lhm + lll + lml + lhl);
gy := (lhh + mhh + hhh + lhm + mhm + hhm + lhl + mhl + hhl) - (llh + mlh + hlh + llm + mlm + hlm + lll + mll + hll);
gz := (llh + mlh + hlh + lmh + mmh + hmh + lhh + mhh + hhh) - (lll + mll + hll + lml + mml + hml + lhl + mhl + hhl);
{$IFDEF ASJAD}
//slice above (high)
llh := vol8[vx - 1 - nX + nXY] * 2;
mlh := vol8[vx - 0 - nX + nXY];
hlh := vol8[vx + 1 - nX + nXY] * 2;
lmh := vol8[vx - 1 - 0 + nXY];
//mmh := vol8[vx - 0 - 0 + nXY] * 0;
hmh := vol8[vx + 1 - 0 + nXY];
lhh := vol8[vx - 1 + nX + nXY] * 2;
mhh := vol8[vx - 0 + nX + nXY];
hhh := vol8[vx + 1 + nX + nXY] * 2;
//same slice (middle)
llm := vol8[vx - 1 - nX + 0] * 4;
mlm := vol8[vx - 0 - nX + 0] * 2;
hlm := vol8[vx + 1 - nX + 0] * 4;
lmm := vol8[vx - 1 - 0 + 0] * 2;
//mmm := vol8[vx - 0 - 0 + 0];
hmm := vol8[vx + 1 - 0 + 0] * 2;
lhm := vol8[vx - 1 + nX + 0] * 4;
mhm := vol8[vx - 0 + nX + 0] * 2;
hhm := vol8[vx + 1 + nX + 0] * 4;
//slice below (low)
lll := vol8[vx - 1 - nX - nXY] * 2;
mll := vol8[vx - 0 - nX - nXY];
hll := vol8[vx + 1 - nX - nXY] * 2;
lml := vol8[vx - 1 - 0 - nXY];
//mml := vol8[vx - 0 - 0 - nXY] * 0;
hml := vol8[vx + 1 - 0 - nXY];
lhl := vol8[vx - 1 + nX - nXY] * 2;
mhl := vol8[vx - 0 + nX - nXY];
hhl := vol8[vx + 1 + nX - nXY] * 2;

g45 := (hmh + mlh + hlh + hmm + mlm + hlm + hml + mll + hll) - (lhh + mhh + lmh + lhm + mhm + lmm + lhl + mhl + lml);
g135 := (lmh + llh + mlh + lmm + llm + mlm + lml + lll +mll) - (mhh + hhh + hmh + mhm + hhm + hmm + mhl + hhl + hml);


vol32[vx] += sqrt( sqr(gx) + sqr(gy) + sqr(gz) + sqr(g45) + sqr(g135));
{$ELSE}
vol32[vx] += sqrt( sqr(gx) + sqr(gy) + sqr(gz));
{$ENDIF}

end;
//scale 0..1
mn := infinity;
Expand Down
1 change: 1 addition & 0 deletions niftis.pas
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ function TNIfTIs.AddEdgeLayer(Idx: integer; backColor: TRGBA): boolean;
if length(img8) < 1 then exit;
hdr := niis[Idx].Header;
hdr.intent_code := kNIFTI_INTENT_NONE;
hdr.descrip := 'Sobel MRIcroGL'+kVers;
prefix := 'edge_';
niis[fNumLayers] := TNIfTI.Create(prefix+niis[Idx].shortname, backColor, niis[Idx].Mat, niis[Idx].Dim, fInterpolateOverlays, hdr, TFloat32s(img8), true);
fNumLayers := fNumLayers + 1;
Expand Down

0 comments on commit 9507f36

Please sign in to comment.