⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dtmri_view.cpp

📁 this a image processing program
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*-------------------------------------------------------------------------------  File        : dtmri_view.cpp  Description : A viewer of Diffusion-Tensor MRI volumes (medical imaging).  Copyright  : David Tschumperle - http://www.greyc.ensicaen.fr/~dtschump/  This software is governed by the CeCILL-C license under French law and  abiding by the rules of distribution of free software.  You can  use,  modify and/ or redistribute the software under the terms of the CeCILL-C  license as circulated by CEA, CNRS and INRIA at the following URL  "http://www.cecill.info".  As a counterpart to the access to the source code and  rights to copy,  modify and redistribute granted by the license, users are provided only  with a limited warranty  and the software's author,  the holder of the  economic rights,  and the successive licensors  have only  limited  liability.  In this respect, the user's attention is drawn to the risks associated  with loading,  using,  modifying and/or developing or reproducing the  software by the user in light of its specific status of free software,  that may mean  that it is complicated to manipulate,  and  that  also  therefore means  that it is reserved for developers  and  experienced  professionals having in-depth computer knowledge. Users are therefore  encouraged to load and test the software's suitability as regards their  requirements in conditions enabling the security of their systems and/or  data to be ensured and,  more generally, to use and operate it in the  same conditions as regards security.  The fact that you are presently reading this means that you have had  knowledge of the CeCILL-C license and that you accept its terms.  ------------------------------------------------------------------------------*/#include "../CImg.h"using namespace cimg_library;// The undef below is necessary when using a non-standard compiler.#ifdef cimg_use_visualcpp6#define std#endif// Compute fractional anisotropy (FA) of a tensor//-------------------------------------------template<typename T> float get_FA(const T& val1, const T& val2, const T& val3) {  const float    l1 = val1>0?val1:0, l2 = val2>0?val2:0, l3 = val3>0?val3:0,    lm = (l1+l2+l3)/3,    tr2 = 2*( l1*l1 + l2*l2 + l3*l3 ),    ll1 = l1-lm,    ll2 = l2-lm,    ll3 = l3-lm;  if (tr2>0) return (float)std::sqrt( 3*(ll1*ll1 + ll2*ll2 + ll3*ll3)/tr2 );  return 0;}// Insert an ellipsoid in a CImg 3D scene//----------------------------------------template<typename t,typename tp,typename tf,typename tc>void insert_ellipsoid(const CImg<t>& tensor,const float X,const float Y,const float Z,const float tfact,		      const float vx, const float vy, const float vz,		      CImgList<tp>& points, CImgList<tf>& faces, CImgList<tc>& colors,		      const unsigned int res1 = 20, const unsigned int res2 = 20) {  // Compute eigen elements  const float l1 = tensor[0], l2 = tensor[1], l3 = tensor[2], fa = get_FA(l1,l2,l3);  CImg<> vec = CImg<>::matrix(tensor[3],tensor[6],tensor[9],			      tensor[4],tensor[7],tensor[10],			      tensor[5],tensor[8],tensor[11]);  const int    r = (int)cimg::min(30+1.5f*cimg::abs(255*fa*tensor[3]),255.0f),    g = (int)cimg::min(30+1.5f*cimg::abs(255*fa*tensor[4]),255.0f),    b = (int)cimg::min(30+1.5f*cimg::abs(255*fa*tensor[5]),255.0f);  // Define mesh points  const unsigned int N0 = points.size;  for (unsigned int v=1; v<res2; v++)    for (unsigned int u=0; u<res1; u++) {      const float	alpha = (float)(u*2*cimg::PI/res1),	beta = (float)(-cimg::PI/2 + v*cimg::PI/res2),	x = (float)(tfact*l1*std::cos(beta)*std::cos(alpha)),	y = (float)(tfact*l2*std::cos(beta)*std::sin(alpha)),	z = (float)(tfact*l3*std::sin(beta));      points.insert((CImg<tp>::vector(X,Y,Z)+vec*CImg<tp>::vector(x,y,z)).mul(CImg<tp>::vector(vx,vy,vz)));    }  const unsigned int N1 = points.size;  points.insert((CImg<tp>::vector(X,Y,Z)+vec*CImg<tp>::vector(0,0,-l3*tfact)));  points.insert((CImg<tp>::vector(X,Y,Z)+vec*CImg<tp>::vector(0,0,l3*tfact)));  points[points.size-2](0)*=vx; points[points.size-2](1)*=vy;  points[points.size-2](2)*=vz;  points[points.size-1](0)*=vx; points[points.size-1](1)*=vy;  points[points.size-1](2)*=vz;  // Define mesh triangles  for (unsigned int vv=0; vv<res2-2; vv++)    for (unsigned int uu=0; uu<res1; uu++) {      const int nv = (vv+1)%(res2-1), nu = (uu+1)%res1;      faces.insert(CImg<tf>::vector(N0+res1*vv+nu,N0+res1*nv+uu,N0+res1*vv+uu));      faces.insert(CImg<tf>::vector(N0+res1*vv+nu,N0+res1*nv+nu,N0+res1*nv+uu));      colors.insert(CImg<tc>::vector(r,g,b));      colors.insert(CImg<tc>::vector(r,g,b));    }  for (unsigned int uu=0; uu<res1; uu++) {    const int nu = (uu+1)%res1;    faces.insert(CImg<tf>::vector(N0+nu,N0+uu,N1));    faces.insert(CImg<tf>::vector(N0+res1*(res2-2)+nu, N1+1,N0+res1*(res2-2)+uu));    colors.insert(CImg<tc>::vector(r,g,b));    colors.insert(CImg<tc>::vector(r,g,b));  }}// Insert a fiber in a CImg 3D scene//-----------------------------------template<typename T,typename te,typename tp, typename tf, typename tc>void insert_fiber(const CImg<T>& fiber, const CImg<te>& eigen, const CImg<tc>& palette,		  const int xm, const int ym, const int zm,		  const float vx, const float vy, const float vz,		  CImgList<tp>& points, CImgList<tf>& primitives, CImgList<tc>& colors) {  const int N0 = points.size;  float x0 = fiber(0,0), y0 = fiber(0,1), z0 = fiber(0,2), fa0 = eigen.linear_pix3d(x0,y0,z0,12);  points.insert(CImg<>::vector(vx*(x0-xm),vy*(y0-ym),vz*(z0-zm)));  for (unsigned int l=1; l<fiber.width; l++) {    float x1 = fiber(l,0), y1 = fiber(l,1), z1 = fiber(l,2), fa1 = eigen.linear_pix3d(x1,y1,z1,12);    points.insert(CImg<tp>::vector(vx*(x1-xm),vy*(y1-ym),vz*(z1-zm)));    primitives.insert(CImg<tf>::vector(N0+l-1,N0+l));    const unsigned char      icol = (unsigned char)(fa0*255),      r = palette(icol,0),      g = palette(icol,1),      b = palette(icol,2);    colors.insert(CImg<unsigned char>::vector(r,g,b));    x0=x1; y0=y1; z0=z1; fa0=fa1;  }}// Compute fiber tracking using 4th-order Runge Kutta integration//-----------------------------------------------------------------template<typename T>CImg<> get_fibertrack(CImg<T>& eigen,		      const int X0, const int Y0, const int Z0, const float lmax=100,		      const float dl = 0.1f, const float FAmin=0.7f, const float cmin=0.5f) {#define align_eigen(i,j,k) \      { T &u = eigen(i,j,k,3), &v = eigen(i,j,k,4), &w = eigen(i,j,k,5); \        if (u*cu+v*cv+w*cw<0) { u=-u; v=-v; w=-w; }}  CImgList<> resf;  // Forward tracking  float normU = 0, normpU = 0, l = 0, X = (float)X0, Y = (float)Y0, Z = (float)Z0;  T    pu = eigen(X0,Y0,Z0,3),    pv = eigen(X0,Y0,Z0,4),    pw = eigen(X0,Y0,Z0,5);  normpU = (float)std::sqrt(pu*pu+pv*pv+pw*pw);  bool stopflag = false;  while (!stopflag) {    if (X<0 || X>eigen.dimx()-1 || Y<0 || Y>eigen.dimy()-1 || Z<0 || Z>eigen.dimz()-1 ||	eigen((int)X,(int)Y,(int)Z,12)<FAmin || l>lmax) stopflag = true;    else {      resf.insert(CImg<>::vector(X,Y,Z));      const int	cx = (int)X, px = (cx-1<0)?0:cx-1, nx = (cx+1>=eigen.dimx())?eigen.dimx()-1:cx+1,	cy = (int)Y, py = (cy-1<0)?0:cy-1, ny = (cy+1>=eigen.dimy())?eigen.dimy()-1:cy+1,	cz = (int)Z, pz = (cz-1<0)?0:cz-1, nz = (cz+1>=eigen.dimz())?eigen.dimz()-1:cz+1;      const T cu = eigen(cx,cy,cz,3), cv = eigen(cx,cy,cz,4), cw = eigen(cx,cy,cz,5);      align_eigen(px,py,pz); align_eigen(cx,py,pz); align_eigen(nx,py,pz);      align_eigen(px,cy,pz); align_eigen(cx,cy,pz); align_eigen(nx,cy,pz);      align_eigen(px,ny,pz); align_eigen(cx,ny,pz); align_eigen(nx,ny,pz);      align_eigen(px,py,cz); align_eigen(cx,py,cz); align_eigen(nx,py,cz);      align_eigen(px,cy,cz);                        align_eigen(nx,cy,cz);      align_eigen(px,ny,cz); align_eigen(cx,ny,cz); align_eigen(nx,ny,cz);      align_eigen(px,py,nz); align_eigen(cx,py,nz); align_eigen(nx,py,nz);      align_eigen(px,cy,nz); align_eigen(cx,cy,nz); align_eigen(nx,cy,nz);      align_eigen(px,ny,nz); align_eigen(cx,ny,nz); align_eigen(nx,ny,nz);      const T	u0 = 0.5f*dl*eigen.linear_pix3d(X,Y,Z,3),	v0 = 0.5f*dl*eigen.linear_pix3d(X,Y,Z,4),	w0 = 0.5f*dl*eigen.linear_pix3d(X,Y,Z,5),	u1 = 0.5f*dl*eigen.linear_pix3d(X+u0,Y+v0,Z+w0,3),	v1 = 0.5f*dl*eigen.linear_pix3d(X+u0,Y+v0,Z+w0,4),	w1 = 0.5f*dl*eigen.linear_pix3d(X+u0,Y+v0,Z+w0,5),	u2 = 0.5f*dl*eigen.linear_pix3d(X+u1,Y+v1,Z+w1,3),	v2 = 0.5f*dl*eigen.linear_pix3d(X+u1,Y+v1,Z+w1,4),	w2 = 0.5f*dl*eigen.linear_pix3d(X+u1,Y+v1,Z+w1,5),	u3 = 0.5f*dl*eigen.linear_pix3d(X+u2,Y+v2,Z+w2,3),	v3 = 0.5f*dl*eigen.linear_pix3d(X+u2,Y+v2,Z+w2,4),	w3 = 0.5f*dl*eigen.linear_pix3d(X+u2,Y+v2,Z+w2,5);      T	u = u0/3 + 2*u1/3 + 2*u2/3 + u3/3,	v = v0/3 + 2*v1/3 + 2*v2/3 + v3/3,	w = w0/3 + 2*w1/3 + 2*w2/3 + w3/3;      if (u*pu+v*pv+w*pw<0) { u=-u; v=-v; w=-w; }      normU = (float)std::sqrt(u*u+v*v+w*w);      const float scal = (u*pu+v*pv+w*pw)/(normU*normpU);      if (scal<cmin) stopflag=true;      X+=(pu=u); Y+=(pv=v); Z+=(pw=w);      normpU = normU;      l+=dl;    }  }  // Backward tracking  l = dl; X = (float)X0; Y = (float)Y0; Z = (float)Z0;  pu = eigen(X0,Y0,Z0,3);  pv = eigen(X0,Y0,Z0,4);  pw = eigen(X0,Y0,Z0,5);  normpU = (float)std::sqrt(pu*pu+pv*pv+pw*pw);  stopflag = false;  while (!stopflag) {    if (X<0 || X>eigen.dimx()-1 || Y<0 || Y>eigen.dimy()-1 || Z<0 || Z>eigen.dimz()-1 ||	eigen((int)X,(int)Y,(int)Z,12)<FAmin || l>lmax) stopflag = true;    else {      const int	cx = (int)X, px = (cx-1<0)?0:cx-1, nx = (cx+1>=eigen.dimx())?eigen.dimx()-1:cx+1,	cy = (int)Y, py = (cy-1<0)?0:cy-1, ny = (cy+1>=eigen.dimy())?eigen.dimy()-1:cy+1,	cz = (int)Z, pz = (cz-1<0)?0:cz-1, nz = (cz+1>=eigen.dimz())?eigen.dimz()-1:cz+1;      const T cu = eigen(cx,cy,cz,3), cv = eigen(cx,cy,cz,4), cw = eigen(cx,cy,cz,5);      align_eigen(px,py,pz); align_eigen(cx,py,pz); align_eigen(nx,py,pz);      align_eigen(px,cy,pz); align_eigen(cx,cy,pz); align_eigen(nx,cy,pz);      align_eigen(px,ny,pz); align_eigen(cx,ny,pz); align_eigen(nx,ny,pz);      align_eigen(px,py,cz); align_eigen(cx,py,cz); align_eigen(nx,py,cz);      align_eigen(px,cy,cz);                        align_eigen(nx,cy,cz);      align_eigen(px,ny,cz); align_eigen(cx,ny,cz); align_eigen(nx,ny,cz);      align_eigen(px,py,nz); align_eigen(cx,py,nz); align_eigen(nx,py,nz);      align_eigen(px,cy,nz); align_eigen(cx,cy,nz); align_eigen(nx,cy,nz);      align_eigen(px,ny,nz); align_eigen(cx,ny,nz); align_eigen(nx,ny,nz);      const T	u0 = 0.5f*dl*eigen.linear_pix3d(X,Y,Z,3),	v0 = 0.5f*dl*eigen.linear_pix3d(X,Y,Z,4),	w0 = 0.5f*dl*eigen.linear_pix3d(X,Y,Z,5),	u1 = 0.5f*dl*eigen.linear_pix3d(X+u0,Y+v0,Z+w0,3),	v1 = 0.5f*dl*eigen.linear_pix3d(X+u0,Y+v0,Z+w0,4),	w1 = 0.5f*dl*eigen.linear_pix3d(X+u0,Y+v0,Z+w0,5),	u2 = 0.5f*dl*eigen.linear_pix3d(X+u1,Y+v1,Z+w1,3),	v2 = 0.5f*dl*eigen.linear_pix3d(X+u1,Y+v1,Z+w1,4),	w2 = 0.5f*dl*eigen.linear_pix3d(X+u1,Y+v1,Z+w1,5),	u3 = 0.5f*dl*eigen.linear_pix3d(X+u2,Y+v2,Z+w2,3),	v3 = 0.5f*dl*eigen.linear_pix3d(X+u2,Y+v2,Z+w2,4),	w3 = 0.5f*dl*eigen.linear_pix3d(X+u2,Y+v2,Z+w2,5);      T	u = u0/3 + 2*u1/3 + 2*u2/3 + u3/3,	v = v0/3 + 2*v1/3 + 2*v2/3 + v3/3,	w = w0/3 + 2*w1/3 + 2*w2/3 + w3/3;      if (u*pu+v*pv+w*pw<0) { u=-u; v=-v; w=-w; }      normU = (float)std::sqrt(u*u+v*v+w*w);      const float scal = (u*pu+v*pv+w*pw)/(normU*normpU);      if (scal<cmin) stopflag=true;      X-=(pu=u); Y-=(pv=v); Z-=(pw=w);      normpU=normU;      l+=dl;      resf.insert(CImg<>::vector(X,Y,Z),0);    }  }  return resf.get_append('x');}// Main procedure//----------------int main(int argc,char **argv) {  // Read and init data  //--------------------  cimg_usage("A viewer of Diffusion-Tensor MRI volumes.");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -