void processImage(V3DPluginCallback2 &callback) { v3dhandle curwin = callback.currentImageWindow(); if (!curwin) { QMessageBox::information(0, "", "You don't have any image open in the main window."); return; } Image4DSimple* p4DImage = callback.getImage(curwin); if (!p4DImage) { QMessageBox::information(0, "", "The image pointer is invalid. Ensure your data is valid and try again!"); return; } unsigned char* data1d = p4DImage->getRawData(); V3DLONG N = p4DImage->getXDim(); V3DLONG M = p4DImage->getYDim(); V3DLONG P = p4DImage->getZDim(); ImagePixelType pixeltype = p4DImage->getDatatype(); // display new this was copied from plug_watershed and is substantially different // from ZZs local enhancement code. Image4DSimple new4DImage; new4DImage.setData((unsigned char *)data1d,N, M, P, 1, pixeltype); v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, &new4DImage); callback.setImageName(newwin, QString("Local_adaptive_enhancement_result")); callback.updateImageWindow(newwin); }
void IBioformatIOPlugin::domenu(const QString &menu_name, V3DPluginCallback2 &callback, QWidget *parent) { if (menu_name == tr("load an image using Bioformats Java library")) { // input QString m_FileName = QFileDialog::getOpenFileName(parent, QObject::tr("Open An Image"), QDir::currentPath(), QObject::tr("Image File (*.*)")); if(m_FileName.isEmpty()) { printf("\nError: Your image does not exist!\n"); return; } // temp QString baseName = QFileInfo(m_FileName).baseName(); QString tmpfile = QDir::tempPath().append("/").append(baseName).append(".tif"); // QFile tmpqfile(tmpfile); if (tmpqfile.exists()) system(qPrintable(QString("rm -f \"%1\"").arg(tmpfile))); //look for loci_tools.jar QString lociDir = ("loci_tools.jar"); if (!QFile(lociDir).exists()) { printf("loci_tools.jar is not in current directory, search v3d app path.\n"); lociDir = getAppPath().append("/loci_tools.jar"); printf(qPrintable(lociDir)); printf("\n"); if (!QFile(lociDir).exists()) { v3d_msg("Cannot find loci_tools.jar, please download it and make sure it is put under the Vaa3D executable folder, parallel to the Vaa3D executable and the plugins folder."); return; } } #if defined(Q_OS_WIN32) QSettings settings("HHMI", "Vaa3D"); QString fileOpenName = settings.value("JavaPath").toString(); if(fileOpenName.isEmpty()) { fileOpenName = QFileDialog::getOpenFileName(0, QObject::tr("Open Java Executable File"), "", QObject::tr("Supported file (*.exe)" )); if(fileOpenName.isEmpty()) return; settings.setValue("JavaPath", fileOpenName); } QString cmd_loci = QString("\"\"%1\" -cp \"%2\" loci.formats.tools.ImageConverter \"%3\" \"%4\"\"").arg(fileOpenName.toStdString().c_str()).arg(lociDir.toStdString().c_str()).arg(m_FileName.toStdString().c_str()).arg(tmpfile.toStdString().c_str()); #else QString cmd_loci = QString("java -cp \"%1\" loci.formats.tools.ImageConverter \"%2\" \"%3\"").arg(lociDir.toStdString().c_str()).arg(m_FileName.toStdString().c_str()).arg(tmpfile.toStdString().c_str()); #endif v3d_msg(cmd_loci, 0); system(qPrintable(cmd_loci)); if (!tmpqfile.exists()) { v3d_msg("The temprary file does not exist. The conversion of format using Bioformats has failed. Please sue another way to convert and load using Vaa3D.\n"); return; } // load V3DLONG sz_relative[4]; int datatype_relative = 0; unsigned char* relative1d = 0; if (simple_loadimage_wrapper(callback, const_cast<char *>(tmpfile.toStdString().c_str()), relative1d, sz_relative, datatype_relative)!=true) { fprintf (stderr, "Error happens in reading the subject file [%s]. Exit. \n",tmpfile.toStdString().c_str()); return; } // visualize Image4DSimple p4DImage; if(datatype_relative == V3D_UINT8) { p4DImage.setData((unsigned char*)relative1d, sz_relative[0], sz_relative[1], sz_relative[2], sz_relative[3], V3D_UINT8); } else if(datatype_relative == V3D_UINT16) { p4DImage.setData((unsigned char*)relative1d, sz_relative[0], sz_relative[1], sz_relative[2], sz_relative[3], V3D_UINT16); } else if(datatype_relative == V3D_FLOAT32) { p4DImage.setData((unsigned char*)relative1d, sz_relative[0], sz_relative[1], sz_relative[2], sz_relative[3], V3D_FLOAT32); } else { printf("\nError: The program only supports UINT8, UINT16, and FLOAT32 datatype.\n"); return; } v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, &p4DImage); callback.setImageName(newwin, tmpfile.toStdString().c_str()); callback.updateImageWindow(newwin); } else if (menu_name == tr("click me if you are unhappy with the loading result...")) { v3d_msg("This program is designed to use a system call to invoke the LOCI Bioformats Image IO Java library loci_tools.jar" " to load an image. It first calls bioformats library to generate a temporary 3D TIF file on your harddrive" " and then uses Vaa3D to load that temporary file. Therefore, if you see some wrong loading result using this plugin, it is" " likely that you will get the same thing if you run the bioformats library directly. Of course, you may find a newer" " version of the loci_tools.jar at the LOCI website http://loci.wisc.edu/bio-formats/downloads; we encourage you" " to copy the latest version to the Vaa3D executable folder and try if it would fix your problem.", 1); return; } else if (menu_name == tr("About")) { QMessageBox::information(parent, "Version info", QString("Simple image reading using Bioformats library %1 (2011-2012) developed by Yang Yu, Yinan Wan, and Hanchuan Peng. (Janelia Research Farm Campus, HHMI)").arg(getPluginVersion()).append("\n")); return; } }
void processImage(V3DPluginCallback2 &callback, QWidget *parent, unsigned int rotateflag) { v3dhandle curwin = callback.currentImageWindow(); if (!curwin) { QMessageBox::information(0, "", "You don't have any image open in the main window."); return; } Image4DSimple* image = callback.getImage(curwin); if (!image) { QMessageBox::information(0, "", "The image pointer is invalid. Ensure your data is valid and try again!"); return; } unsigned char* data1d = image->getRawData(); V3DLONG szx = image->getXDim(), szy = image->getYDim(), szz = image->getZDim(), szc = image->getCDim(); // V3DLONG szchan = image->getTotalUnitNumberPerChannel(), szplane = image->getTotalUnitNumberPerPlane(); // V3DLONG N = image->getTotalBytes(); // V3DLONG i,j,k,c; if (!data1d || szx<=0 || szy<=0 || szz<=0 || szc<=0) { throw("Your data to the plugin is invalid. Check the program."); return; } ImagePixelType pixeltype = image->getDatatype(); V3DLONG in_sz[4]; in_sz[0]=szx; in_sz[1]=szy; in_sz[2]=szz; in_sz[3]=szc; unsigned char* outimg=0; rotateimage(data1d, in_sz, pixeltype, rotateflag, outimg); // image->setData(outimg, szy, szx, szz, szc, image->getDatatype()); // display Image4DSimple * new4DImage = new Image4DSimple(); switch(rotateflag) { case 1: new4DImage->setData((unsigned char*)outimg, szy, szx, szz, szc, image->getDatatype()); break; case 2: new4DImage->setData((unsigned char*)outimg, szy, szx, szz, szc, image->getDatatype()); break; case 3: new4DImage->setData((unsigned char*)outimg, szx, szy, szz, szc, image->getDatatype()); break; default: break; } v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, new4DImage); callback.setImageName(newwin, title); callback.updateImageWindow(newwin); }
bool anisodiff_func(V3DPluginCallback2 &callback, QWidget *parent, input_PARA &PARA, bool bmenu) { unsigned char* p_img_input = 0; V3DLONG sz_img_input[4]; if(bmenu) { v3dhandle curwin = callback.currentImageWindow(); if (!curwin) { QMessageBox::information(0, "", "You don't have any image open in the main window."); return false; } Image4DSimple* p4DImage = callback.getImage(curwin); if (!p4DImage) { QMessageBox::information(0, "", "The image pointer is invalid. Ensure your data is valid and try again!"); return false; } if(p4DImage->getDatatype()!=V3D_UINT8) { QMessageBox::information(0, "", "Please convert the image to be UINT8 and try again!"); return false; } if(p4DImage->getCDim()!=1) { QMessageBox::information(0, "", "The input image is not one channel image!"); return false; } p_img_input = p4DImage->getRawData(); sz_img_input[0] = p4DImage->getXDim(); sz_img_input[1] = p4DImage->getYDim(); sz_img_input[2] = p4DImage->getZDim(); sz_img_input[3] = 1; } else { int datatype = 0; if (!simple_loadimage_wrapper(callback,PARA.inimg_file.toStdString().c_str(), p_img_input, sz_img_input, datatype)) { fprintf (stderr, "Error happens in reading the subject file [%s]. Exit. \n",PARA.inimg_file.toStdString().c_str()); return false; } if(PARA.channel < 1 || PARA.channel > sz_img_input[3]) { fprintf (stderr, "Invalid channel number. \n"); return false; } if(datatype !=1) { fprintf (stderr, "Please convert the image to be UINT8 and try again!\n"); return false; } } //----------------------------------------------------------------------------------------- printf("1. Find the bounding box and crop image. \n"); long l_boundbox_min[3],l_boundbox_max[3];//xyz V3DLONG sz_img_crop[4]; long l_npixels_crop; unsigned char *p_img8u_crop=0; { //find bounding box unsigned char ***p_img8u_3d=0; if(!new3dpointer(p_img8u_3d,sz_img_input[0],sz_img_input[1],sz_img_input[2],p_img_input)) { printf("ERROR: Fail to allocate memory for the 4d pointer of image.\n"); if(p_img8u_3d) {delete3dpointer(p_img8u_3d,sz_img_input[0],sz_img_input[1],sz_img_input[2]);} return false; } l_boundbox_min[0]=sz_img_input[0]; l_boundbox_min[1]=sz_img_input[1]; l_boundbox_min[2]=sz_img_input[2]; l_boundbox_max[0]=0; l_boundbox_max[1]=0; l_boundbox_max[2]=0; for(long X=0;X<sz_img_input[0];X++) for(long Y=0;Y<sz_img_input[1];Y++) for(long Z=0;Z<sz_img_input[2];Z++) if(p_img8u_3d[Z][Y][X]>0.1) { if(l_boundbox_min[0]>X) l_boundbox_min[0]=X; if(l_boundbox_max[0]<X) l_boundbox_max[0]=X; if(l_boundbox_min[1]>Y) l_boundbox_min[1]=Y; if(l_boundbox_max[1]<Y) l_boundbox_max[1]=Y; if(l_boundbox_min[2]>Z) l_boundbox_min[2]=Z; if(l_boundbox_max[2]<Z) l_boundbox_max[2]=Z; } printf(">>boundingbox: x[%ld~%ld],y[%ld~%ld],z[%ld~%ld]\n",l_boundbox_min[0],l_boundbox_max[0], l_boundbox_min[1],l_boundbox_max[1], l_boundbox_min[2],l_boundbox_max[2]); //crop image sz_img_crop[0]=l_boundbox_max[0]-l_boundbox_min[0]+1; sz_img_crop[1]=l_boundbox_max[1]-l_boundbox_min[1]+1; sz_img_crop[2]=l_boundbox_max[2]-l_boundbox_min[2]+1; sz_img_crop[3]=1; l_npixels_crop=sz_img_crop[0]*sz_img_crop[1]*sz_img_crop[2]; p_img8u_crop=new(std::nothrow) unsigned char[l_npixels_crop](); if(!p_img8u_crop) { printf("ERROR: Fail to allocate memory for p_img32f_crop!\n"); if(p_img8u_3d) {delete3dpointer(p_img8u_3d,sz_img_input[0],sz_img_input[1],sz_img_input[2]);} return false; } unsigned char *p_tmp=p_img8u_crop; for(long Z=0;Z<sz_img_crop[2];Z++) for(long Y=0;Y<sz_img_crop[1];Y++) for(long X=0;X<sz_img_crop[0];X++) { *p_tmp = p_img8u_3d[Z+l_boundbox_min[2]][Y+l_boundbox_min[1]][X+l_boundbox_min[0]]; p_tmp++; } if(p_img8u_3d) {delete3dpointer(p_img8u_3d,sz_img_input[0],sz_img_input[1],sz_img_input[2]);} } //saveImage("d:/SVN/Vaa3D_source_code/v3d_external/released_plugins/v3d_plugins/anisodiffusion_littlequick/crop.raw",p_img8u_crop,sz_img_crop,1); //----------------------------------------------------------------------------------------- //convert image data type to float printf("2. Convert image data to float and scale to [0~255]. \n"); float *p_img32f_crop=0; { p_img32f_crop=new(std::nothrow) float[l_npixels_crop](); if(!p_img32f_crop) { printf("ERROR: Fail to allocate memory for p_img32f_crop!\n"); if(p_img8u_crop) {delete []p_img8u_crop; p_img8u_crop=0;} if(p_img32f_crop) {delete []p_img32f_crop; p_img32f_crop=0;} return false; } //find the maximal intensity value float d_maxintensity_input=0.0; for(long i=0;i<l_npixels_crop;i++) if(p_img8u_crop[i]>d_maxintensity_input) d_maxintensity_input=p_img8u_crop[i]; //convert and rescale for(long i=0;i<l_npixels_crop;i++) p_img32f_crop[i]=p_img8u_crop[i]/d_maxintensity_input*255.0; printf(">>d_maxintensity=%.2f\n",d_maxintensity_input); //free input image to save memory //if(p_img_input) {delete []p_img_input; p_img_input=0;} if(p_img8u_crop) {delete []p_img8u_crop; p_img8u_crop=0;} } //----------------------------------------------------------------------------------------- //do anisotropic diffusion printf("3. Do anisotropic diffusion... \n"); float *p_img32f_crop_output=0; if(!q_AnisoDiff3D(p_img32f_crop,sz_img_crop,p_img32f_crop_output)) { printf("ERROR: q_AnisoDiff3D() return false!\n"); if(p_img8u_crop) {delete []p_img8u_crop; p_img8u_crop=0;} if(p_img32f_crop) {delete []p_img32f_crop; p_img32f_crop=0;} if(p_img32f_crop_output) {delete []p_img32f_crop_output; p_img32f_crop_output=0;} return false; } if(p_img32f_crop) {delete []p_img32f_crop; p_img32f_crop=0;} //----------------------------------------------------------------------------------------- printf("4. Reconstruct processed crop image back to original size. \n"); unsigned char *p_img8u_output=0; long l_npixels=sz_img_input[0]*sz_img_input[1]*sz_img_input[2]*sz_img_input[3]; { p_img8u_output=new(std::nothrow) unsigned char[l_npixels](); if(!p_img8u_output) { printf("ERROR: Fail to allocate memory for p_img8u_output!\n"); if(p_img32f_crop_output) {delete []p_img32f_crop_output; p_img32f_crop_output=0;} return false; } //copy original image data to output image for(long i=0;i<l_npixels;i++) p_img8u_output[i]=p_img_input[i]; unsigned char ***p_img8u_3d=0; if(!new3dpointer(p_img8u_3d,sz_img_input[0],sz_img_input[1],sz_img_input[2],p_img8u_output)) { printf("ERROR: Fail to allocate memory for the 4d pointer of image.\n"); if(p_img8u_output) {delete []p_img8u_output; p_img8u_output=0;} if(p_img32f_crop_output) {delete []p_img32f_crop_output; p_img32f_crop_output=0;} return false; } float *p_tmp=p_img32f_crop_output; for(long Z=0;Z<sz_img_crop[2];Z++) for(long Y=0;Y<sz_img_crop[1];Y++) for(long X=0;X<sz_img_crop[0];X++) { p_img8u_3d[Z+l_boundbox_min[2]][Y+l_boundbox_min[1]][X+l_boundbox_min[0]]=(unsigned char)(*p_tmp); p_tmp++; } if(p_img8u_3d) {delete3dpointer(p_img8u_3d,sz_img_input[0],sz_img_input[1],sz_img_input[2]);} if(p_img32f_crop_output) {delete []p_img32f_crop_output; p_img32f_crop_output=0;} } //----------------------------------------------------------------------------------------- //save or display if(bmenu) { printf("5. Display the processed image in Vaa3D. \n"); //push result image back to v3d v3dhandle newwin=callback.newImageWindow("output"); Image4DSimple img4D_output; img4D_output.setData(p_img8u_output,sz_img_input[0],sz_img_input[1],sz_img_input[2],1,V3D_UINT8); callback.setImage(newwin,&img4D_output); callback.updateImageWindow(newwin); callback.open3DWindow(newwin); } else { printf("5. Save the processed image to file. \n"); QString str_outimg_filename = PARA.inimg_file + "_anisodiff.raw"; saveImage(qPrintable(str_outimg_filename),p_img8u_output,sz_img_input,1); if(p_img8u_output) {delete []p_img8u_output; p_img8u_output=0;} } printf(">>Program complete success!\n"); return true; }
void GVFplugin::domenu(const QString &menu_name, V3DPluginCallback2 &callback, QWidget *parent) { if (menu_name == tr("Gradient vector flow based Segmentation")) { // the GVF function wants a pointer to a Vol3DSimple, which I haven't seen before. // this code below generates it (take from plugin_FL_cellseg) // check what's up with the current window: is there a valid image pointer? v3dhandle curwin = callback.currentImageWindow(); if (!curwin) { v3d_msg("Please open an image."); return; } Image4DSimple* subject = callback.getImage(curwin); QString m_InputFileName = callback.getImageName(curwin); if (!subject) { QMessageBox::information(0, "", QObject::tr("No image is open.")); return; } if (subject->getDatatype()!=V3D_UINT8) { QMessageBox::information(0, "", QObject::tr("This demo program only supports 8-bit data. Your current image data type is not supported.")); return; } V3DLONG sz0 = subject->getXDim(); V3DLONG sz1 = subject->getYDim(); V3DLONG sz2 = subject->getZDim(); V3DLONG sz3 = subject->getCDim(); Image4DProxy<Image4DSimple> pSub(subject); V3DLONG channelsz = sz0*sz1*sz2; float *pLabel = 0; unsigned char *pData = 0; gvfsegPara segpara; // set these fields one at a time: segpara.diffusionIteration= 5; segpara.fusionThreshold = 10; segpara.minRegion = 10; segpara.sigma = 3; // doesn't seem to be used in the actual function? //input parameters bool ok1; int c=1; if (sz3>1) //only need to ask if more than one channel { c = QInputDialog::getInteger(parent, "Channel", "Choose channel for segmentation:", 1, 1, sz3, 1, &ok1); c = c-1; //channels are indexed to 0 in Image4DSimple->getRawDataAtChannel if (!ok1) return; } // read in parameters segpara.diffusionIteration = QInputDialog::getInteger(parent, "Diffusion Iterations", "Choose Number of Diffusion Iterations:", 5, 1, 10, 1, &ok1); if (!ok1) return; segpara.fusionThreshold = QInputDialog::getInteger(parent, "Fusion Threshold", "Choose Fusion Threshold :", 2, 1, 10, 1, &ok1); if (!ok1) return; segpara.minRegion= QInputDialog::getInteger(parent, "Minimum Region", "Choose Minimum Region Size (voxels):", 10, 1, 1000, 1, &ok1); if (!ok1) return; // allocate memory for the images Vol3DSimple <unsigned char> * tmp_inimg = 0; Vol3DSimple <USHORTINT16> * tmp_outimg = 0; try { tmp_inimg = new Vol3DSimple <unsigned char> (sz0, sz1, sz2); tmp_outimg = new Vol3DSimple <USHORTINT16> (sz0, sz1, sz2); } catch (...) { v3d_msg("Unable to allocate memory for processing."); if (tmp_inimg) {delete tmp_inimg; tmp_inimg=0;} if (tmp_outimg) {delete tmp_outimg; tmp_outimg=0;} return; } //copy image data into our new memory memcpy((void *)tmp_inimg->getData1dHandle(), (void *)subject->getRawDataAtChannel(c), sz0*sz1*sz2); //now do computation //bool b_res = gvfCellSeg(img3d, outimg3d, segpara); bool b_res = gvfCellSeg(tmp_inimg, tmp_outimg, segpara); // clear out temporary space if (tmp_inimg) {delete tmp_inimg; tmp_inimg=0;} if (!b_res) { v3d_msg("image segmentation using gvfCellSeg() failed \n"); } else { // now display the results // parameters for the new image data V3DLONG new_sz0 = tmp_outimg->sz0(); V3DLONG new_sz1 = tmp_outimg->sz1(); V3DLONG new_sz2 = tmp_outimg->sz2(); V3DLONG new_sz3 = 1; V3DLONG tunits = new_sz0*new_sz1*new_sz2*new_sz3; // USHORTINT16 * outvol1d = new USHORTINT16 [tunits]; // USHORTINT16 * tmpImg_d1d = (USHORTINT16 *)(tmp_outimg->getData1dHandle()); memcpy((void *)outvol1d, (void *)tmp_outimg->getData1dHandle(), tunits*sizeof(USHORTINT16)); if (tmp_outimg) {delete tmp_outimg; tmp_outimg=0;} //free the space immediately for better use of memory Image4DSimple p4DImage; p4DImage.setData((unsigned char*)outvol1d, sz0, sz1, sz2, 1, V3D_UINT16); v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, &p4DImage); callback.setImageName(newwin, QString("Segmented Image")); callback.updateImageWindow(newwin); } return; } else { v3d_msg(tr("A plugin for cell segmentation using Gradient Vector Flow. " "Developed based on the source code developed by Tianming Liu, Fuhui Long, and Hanchuan Peng (2010-2014)")); } }
void call_open_using_imagej(bool ismenu, QString inputfile, QString savefile,V3DPluginCallback2 &callback) { QSettings setting("Vaa3D_tools", "open_imagej"); QString imagej_binary_file = "non-existing"; //setting.value("imagej_binary_path").toByteArray(); v3d_msg(QString("The default path of imagej is [%1]").arg(imagej_binary_file), 0); // v3d_msg(QString("The default temporary location for saving intermediate conversion files is [%1]").arg(tmp_conversion_folder), 0); QFile f_imagej_binary_file(imagej_binary_file); if (!f_imagej_binary_file.exists()) { // v3d_msg("Cannot find the specified default ImageJ location. Adding path"); #if defined(Q_OS_MAC) imagej_binary_file = getAppPath().append("/Fiji.app/Contents/MacOS/ImageJ-macosx"); #elif defined(Q_OS_LINUX) imagej_binary_file = getAppPath().append("/Fiji.app/ImageJ-linux64"); #elif defined(Q_OS_WIN32) imagej_binary_file = getAppPath().append("/Fiji.app/ImageJ-win32.exe"); #elif defined(Q_OS_WIN64) imagej_binary_file = getAppPath().append("/Fiji.app/ImageJ-win64.exe"); #else v3d_msg(tr("Currently only available for Linux, Mac OSX 10.5+ and Windows")); return; #endif v3d_msg(QString("Now set the new imagej path to [%1]").arg(imagej_binary_file), 0); f_imagej_binary_file.setFileName(imagej_binary_file); } if (!f_imagej_binary_file.exists()) { v3d_msg("Cannot locate the executable of ImageJ/Fiji program. Please specify Fiji location."); // v3d_msg("Cannot locate the executable of ImageJ/Fiji program. Now you can specify where it is.",0); // use this code above for dofunc version- prints to command line instead of window imagej_binary_file = QFileDialog::getOpenFileName(0, QObject::tr("Locate your ImageJ/Fiji application"), QDir::currentPath(), QObject::tr(" *")); if(imagej_binary_file.isEmpty()) { return; } // the user will select the .app location, not the the command line executable location, which is OS-dependent // need these for WIN32, WIN64, LINUX and MAC #if defined(Q_OS_MAC) // mac QString fijiPath = imagej_binary_file.append("/Contents/MacOS/ImageJ-macosx"); #elif defined(Q_OS_LINUX) // linux QString fijiPath = imagej_binary_file.append("/ImageJ-linux64"); #elif defined(Q_OS_WIN32) //32 bit windows QString fijiPath = imagej_binary_file.append("/ImageJ-win32.exe"); #elif defined(Q_OS_WIN64) // 64 bit windows QString fijiPath = imagej_binary_file.append("/ImageJ-win64.exe"); #else v3d_msg(tr("Currently only available for Linux, Mac OSX 10.5+ and Windows")); return; #endif f_imagej_binary_file.setFileName(imagej_binary_file); } // the user will select the .app location on mac and linux //now have found the ImageJ location. thus save it for future use setting.setValue("imagej_binary_path", qPrintable(imagej_binary_file)); //now call ImageJ QString v3dAppPath = getAppPath(); QString cmd_Fiji = QString("%1 --headless -batch %2/brl_FijiConvert.js %3:%4").arg(imagej_binary_file.toStdString().c_str()).arg(v3dAppPath.toStdString().c_str()).arg(inputfile.toStdString().c_str()).arg(savefile.toStdString().c_str()); v3d_msg(cmd_Fiji, 0); //the folowing direct call work nicely: //20141004, PHC // /Users/pengh/work/v3d_external/bin/Fiji.app/Contents/MacOS/ImageJ-macosx --headless -batch /Users/pengh/work/v3d_external/bin/brl_FijiConvert.js /workdata/v3d_demo_data/single_neuron/Twin-Spot_GH146_lAL_39-2.lsm:/Users/pengh/Downloads/44/11.avi // //Thus it seems the problem is "Unrecognized command: "raw writer"" system(qPrintable(cmd_Fiji)); if (!QFile(savefile).exists()) v3d_msg("File conversion failed.\n"); //need change later if (ismenu) { // display image if it's being called from the menu. otherwise don't! // load V3DLONG sz_relative[4]; int datatype_relative = 0; unsigned char* relative1d = 0; if (simple_loadimage_wrapper(callback, const_cast<char *>(savefile.toStdString().c_str()), relative1d, sz_relative, datatype_relative)!=true) { fprintf (stderr, "Error happens while reading the subject file [%s]. Exit. \n",savefile.toStdString().c_str()); //return; } // visualize Image4DSimple p4DImage; if(datatype_relative == V3D_UINT8) { p4DImage.setData((unsigned char*)relative1d, sz_relative[0], sz_relative[1], sz_relative[2], sz_relative[3], V3D_UINT8); } else if(datatype_relative == V3D_UINT16) { p4DImage.setData((unsigned char*)relative1d, sz_relative[0], sz_relative[1], sz_relative[2], sz_relative[3], V3D_UINT16); } else if(datatype_relative == V3D_FLOAT32) { p4DImage.setData((unsigned char*)relative1d, sz_relative[0], sz_relative[1], sz_relative[2], sz_relative[3], V3D_FLOAT32); } else { printf("\nError: The program only supports UINT8, UINT16, and FLOAT32 datatype.\n"); return; } v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, &p4DImage); callback.setImageName(newwin, savefile.toStdString().c_str()); callback.updateImageWindow(newwin); } }
void processImage(V3DPluginCallback2 &callback, QWidget *parent, const QString & menu_name) { v3dhandle curwin = callback.currentImageWindow(); if (!curwin) { v3d_msg("You don't have any image open in the main window."); return; } Image4DSimple* p4DImage = callback.getImage(curwin); if (!p4DImage) { v3d_msg("The image pointer is invalid. Ensure your data is valid and try again!"); return; } unsigned char* data1d = p4DImage->getRawData(); V3DLONG sc = p4DImage->getCDim(); //input bool b_res; bool ok1; V3DLONG c=-1; if (menu_name==QString("Split Channels")) c = -1; else if (menu_name==QString("Extract One Channel")) { if (sc>1) //only need to ask if more than one channel { c = QInputDialog::getInteger(parent, "Channel", "Enter channel # (0, 1... for the 1st, 2nd... channels; -1 for all channels):", -1, -1, sc-1, 1, &ok1); if (!ok1) return; } } V3DLONG in_sz[4]; in_sz[0] = p4DImage->getXDim(); in_sz[1] = p4DImage->getYDim(); in_sz[2] = p4DImage->getZDim(); in_sz[3] = sc; // V3DLONG cb, ce, k; if (c<0) {cb=0; ce=sc-1;} else {cb = ce = c;} void * outimg=0; for (k=cb; k<=ce; k++) { switch(p4DImage->getDatatype()) { case V3D_UINT8: b_res = extract_a_channel(data1d, in_sz, k, outimg); break; case V3D_UINT16: b_res = extract_a_channel((unsigned short int *)data1d, in_sz, k, outimg); break; case V3D_FLOAT32: b_res = extract_a_channel((float *)data1d, in_sz, k, outimg); break; default: b_res = false; v3d_msg("Right now this plugin supports only UINT8/UINT16/FLOAT32 data. Do nothing."); return; } //display Image4DSimple * new4DImage = new Image4DSimple(); new4DImage->createImage(in_sz[0], in_sz[1], in_sz[2], 1, p4DImage->getDatatype()); memcpy(new4DImage->getRawData(), (unsigned char *)outimg, new4DImage->getTotalBytes()); v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, new4DImage); callback.setImageName(newwin, QString("").setNum(k).prepend("_C").prepend(p4DImage->getFileName())); callback.updateImageWindow(newwin); } }
bool do_AtlasGuidedStrAnno(V3DPluginCallback2 &callback, QString &qs_filename_img, QString &qs_filename_marker_input, // input files QString &qs_filename_atals_input, QString &qs_filename_celloi_input, QString &qs_filename_celloi_2_input, CSParas ¶s_str, CParas ¶s_anno, bool &b_use_celloi_2, // paras QString &qs_filename_atals_output, QString &qs_filename_seglabel_output ) // output files { //------------------------------------------------------------------------------------------------------------------------------------ printf("1. Import image. \n"); unsigned char *p_img_input=0; V3DLONG sz_img_input[4]; int datatype_input=0; if(!paras_str.b_imgfromV3D) { if(qs_filename_img.isEmpty()) { v3d_msg(QString("invalid image path!")); return false; } if(!simple_loadimage_wrapper(callback, (char *)qPrintable(qs_filename_img),p_img_input,sz_img_input,datatype_input)) { v3d_msg(QString("open file [%1] failed!").arg(qs_filename_img)); return false; } printf("\t>>read image file [%s] complete.\n",qPrintable(qs_filename_img)); } else { printf("\t>>import image from V3D. \n"); v3dhandleList h_wndlist=callback.getImageWindowList(); if(h_wndlist.size()<1) { v3d_msg(QString("Make sure there are at least 1 image in V3D!")); return false; } Image4DSimple* image=callback.getImage(callback.currentImageWindow()); p_img_input=image->getRawData(); //sz_img_input=new V3DLONG[4](); sz_img_input[0]=image->getXDim(); sz_img_input[1]=image->getYDim(); sz_img_input[2]=image->getZDim(); sz_img_input[3]=image->getCDim(); datatype_input=image->getDatatype(); } printf("\t\timage size: [w=%ld, h=%ld, z=%ld, c=%ld]\n",sz_img_input[0],sz_img_input[1],sz_img_input[2],sz_img_input[3]); printf("\t\tdatatype: %d\n",datatype_input); //------------------------------------------------------------------------------------------------------------------------------------ printf("2. Convert image datatype to uint8. \n"); unsigned char * p_img_8u=0; { V3DLONG l_npixels=sz_img_input[0]*sz_img_input[1]*sz_img_input[2]*sz_img_input[3]; p_img_8u=new unsigned char[l_npixels]; if(!p_img_8u) { printf("ERROR: Fail to allocate memory. Do nothing. \n"); if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } if(datatype_input==1) { printf("\t>>convert image data from uint8 to uint8. \n"); for(V3DLONG i=0;i<l_npixels;i++) p_img_8u[i]=p_img_input[i]; } else if(datatype_input==2) { printf("\t>>convert image data from uint16 to uint8. \n"); double min,max; if(!rescale_to_0_255_and_copy((unsigned short int *)p_img_input,l_npixels,min,max,p_img_8u)) { printf("ERROR: rescale_to_0_255_and_copy() return false.\n"); if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } } else if(datatype_input==4) { printf("\t>>convert image data from float to uint8. \n"); double min,max; if(!rescale_to_0_255_and_copy((float *)p_img_input,l_npixels,min,max,p_img_8u)) { printf("ERROR: rescale_to_0_255_and_copy() return false.\n"); if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } } else { v3d_msg(QString("Unknown datatype!\n")); if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } } //------------------------------------------------------------------------------------------------------------------------------------ printf("3. Import markers. \n"); //get initial head and tail marker position from V3D vector< vector<double> > vec2d_markers; if(!paras_str.b_markerfromV3D) { if(qs_filename_marker_input.isEmpty() || qs_filename_marker_input.toUpper()=="NULL") { v3d_msg(QString("Invalid marker path! Ignore it!"), 0); vec2d_markers.clear(); /* //commented by PHC 2013-08-21 if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; */ } else { QList<ImageMarker> ql_markers=readMarker_file(qs_filename_marker_input); printf("\t>>read %d markers from file: %s.\n",ql_markers.size(),qPrintable(qs_filename_marker_input)); vector<double> vec_marker(3,0); for(V3DLONG i=0;i<ql_markers.size();i++) { vec_marker[0]=ql_markers[i].x; vec_marker[1]=ql_markers[i].y; vec_marker[2]=ql_markers[i].z; vec2d_markers.push_back(vec_marker); } } } else { printf("\t>>import markers from V3D. \n"); v3dhandleList h_wndlist=callback.getImageWindowList(); if(h_wndlist.size()<1) { v3d_msg(QString("Make sure there are at least 1 image in V3D!")); if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } LandmarkList ml_makers=callback.getLandmark(callback.currentImageWindow()); vector<double> vec_marker(3,0); for(V3DLONG i=0;i<ml_makers.size();i++) { vec_marker[0]=ml_makers[i].x; vec_marker[1]=ml_makers[i].y; vec_marker[2]=ml_makers[i].z; vec2d_markers.push_back(vec_marker); } } //------------------------------------------------------------------------------------------------------------------------------------ printf("4. Read atlas apo file. \n"); QList<CellAPO> ql_atlasapo; ql_atlasapo=readAPO_file(qs_filename_atals_input); printf("\t>>read %d points from [%s]\n",ql_atlasapo.size(),qPrintable(qs_filename_atals_input)); if(ql_atlasapo.size()<=0) { v3d_msg(QString("Given atlas file is empty or invalid!")); if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } //------------------------------------------------------------------------------------------------------------------------------------ printf("5. Read interesting cell file. \n"); QList<QString> ql_celloi_name; if(!readCelloi_file(qs_filename_celloi_input,ql_celloi_name)) { printf("ERROR: readCelloi_file() return false! \n"); if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } printf("\t>>interesting cell:\n"); for(V3DLONG i=0;i<ql_celloi_name.size();i++) printf("\t\t%s\n",qPrintable(ql_celloi_name[i])); //------------------------------------------------------------------------------------------------------------------------------------ printf("6. Do Straightening. \n"); unsigned char *p_strimg=0; V3DLONG *sz_strimg=0; vector< vector< vector< vector<V3DLONG> > > > vec4d_mappingfield_str2ori; if(vec2d_markers.size()<2) { printf("\t>>marker num < 2, skip straightening.\n"); } else { printf("\t>>marker num >= 2, do straightening.\n"); V3DLONG l_width=paras_str.l_radius_cuttingplane*2+1; QList<ImageMarker> ql_marker; for(unsigned V3DLONG i=0;i<vec2d_markers.size();i++) { ImageMarker tmp; tmp.x=vec2d_markers[i][0]; tmp.y=vec2d_markers[i][1]; tmp.z=vec2d_markers[i][2]; ql_marker.push_back(tmp); } if(!q_celegans_restacking_xy( p_img_8u,sz_img_input, ql_marker,l_width, p_strimg,sz_strimg, vec4d_mappingfield_str2ori)) { printf("ERROR: q_celegans_restacking_xy() return false! \n"); if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } } //------------------------------------------------------------------------------------------------------------------------------------ printf("7. Do Annotation. \n"); QList<CellAPO> ql_musclecell_output; unsigned char *p_img8u_seglabel=0; COutputInfo outputinfo; { unsigned char *p_img_anno=0; V3DLONG *sz_img_anno=0; if(vec2d_markers.size()<2) //on non-straightened image { p_img_anno=p_img_input; sz_img_anno=sz_img_input; } else //on straightened image { p_img_anno=p_strimg; sz_img_anno=sz_strimg; } if(paras_anno.l_mode==-1) //non-partial annotation { if(!q_atlas2image(paras_anno,callback, p_img_anno,sz_img_anno,ql_atlasapo,ql_celloi_name, ql_musclecell_output,p_img8u_seglabel,outputinfo)) { printf("ERROR: q_atlas2image() return false!\n"); if(p_strimg) {delete []p_strimg; p_strimg=0;} if(sz_strimg) {delete []sz_strimg; sz_strimg=0;} if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } } else if(paras_anno.l_mode==4) //align dapi { if(!q_align_dapicells(paras_anno,callback, p_img_anno,sz_img_anno,ql_atlasapo,ql_celloi_name, ql_musclecell_output,outputinfo)) { printf("ERROR: q_atlas2image_partial() return false!\n"); if(p_strimg) {delete []p_strimg; p_strimg=0;} if(sz_strimg) {delete []sz_strimg; sz_strimg=0;} if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } } else //partial annotation { if(!q_atlas2image_partial(paras_anno,callback, p_img_anno,sz_img_anno,ql_atlasapo,ql_celloi_name, ql_musclecell_output,outputinfo)) { printf("ERROR: q_atlas2image_partial() return false!\n"); if(p_strimg) {delete []p_strimg; p_strimg=0;} if(sz_strimg) {delete []sz_strimg; sz_strimg=0;} if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} return false; } } } if(outputinfo.b_rotate90) v3d_msg("The deformed atlas maybe wrongly 90 degree rotated!\nCheck result!\n"); //------------------------------------------------------------------------------------------------------------------------------------ //warping the secondary interesting cells if provided based on the TPS paras obtained from the first group result QList<CellAPO> ql_cellio2_tps; if( b_use_celloi_2 || !qs_filename_celloi_2_input.isEmpty()) // b_use_celloi_2 = DLG_stranno.checkBox_celloi_2->isChecked() { printf("8-9. Warping the secondary interesting cells. \n"); QList<QString> ql_celloi2_name; if(!readCelloi_file(qs_filename_celloi_2_input,ql_celloi2_name)) { printf("ERROR: readCelloi_file() return false! \n"); return false; } vector<point3D64F> vec_cellio1_ori,vec_cellio1_tps,vec_cellio2_ori; QList<CellAPO> ql_cellio2_ori; point3D64F tmp; //extract first interesting cells for(V3DLONG i=0;i<ql_atlasapo.size();i++) { QString qs_cellname=ql_atlasapo[i].name; qs_cellname=qs_cellname.simplified(); qs_cellname=qs_cellname.toUpper(); ql_atlasapo[i].name=qs_cellname; for(V3DLONG j=0;j<ql_celloi_name.size();j++) { if(ql_celloi_name[j].contains("*")) { QString qs_cellnamevalid=ql_celloi_name[j]; qs_cellnamevalid.remove("*"); if(qs_cellname.contains(qs_cellnamevalid,Qt::CaseInsensitive)) { tmp.x=ql_atlasapo[i].x; tmp.y=ql_atlasapo[i].y; tmp.z=ql_atlasapo[i].z; vec_cellio1_ori.push_back(tmp); } } else if(qs_cellname.compare(ql_celloi_name[j],Qt::CaseInsensitive)==0) { tmp.x=ql_atlasapo[i].x; tmp.y=ql_atlasapo[i].y; tmp.z=ql_atlasapo[i].z; vec_cellio1_ori.push_back(tmp); } } } for(V3DLONG i=0;i<ql_musclecell_output.size();i++) { tmp.x=ql_musclecell_output[i].x; tmp.y=ql_musclecell_output[i].y; tmp.z=ql_musclecell_output[i].z; vec_cellio1_tps.push_back(tmp); } //extract secondary interesting cells for(V3DLONG i=0;i<ql_atlasapo.size();i++) { QString qs_cellname=ql_atlasapo[i].name; qs_cellname=qs_cellname.simplified(); qs_cellname=qs_cellname.toUpper(); ql_atlasapo[i].name=qs_cellname; for(V3DLONG j=0;j<ql_celloi2_name.size();j++) { if(ql_celloi2_name[j].contains("*")) { QString qs_cellnamevalid=ql_celloi2_name[j]; qs_cellnamevalid.remove("*"); if(qs_cellname.contains(qs_cellnamevalid,Qt::CaseInsensitive)) { ql_cellio2_ori.push_back(ql_atlasapo[i]); tmp.x=ql_atlasapo[i].x; tmp.y=ql_atlasapo[i].y; tmp.z=ql_atlasapo[i].z; vec_cellio2_ori.push_back(tmp); } } else if(qs_cellname.compare(ql_celloi2_name[j],Qt::CaseInsensitive)==0) { ql_cellio2_ori.push_back(ql_atlasapo[i]); tmp.x=ql_atlasapo[i].x; tmp.y=ql_atlasapo[i].y; tmp.z=ql_atlasapo[i].z; vec_cellio2_ori.push_back(tmp); } } } ql_cellio2_tps=ql_cellio2_ori; printf("\t>>[%d] cells in the secondary intereting cell group.\n",ql_cellio2_ori.size()); //compute TPS paras Matrix x4x4_affine,xnx4_c,xnxn_K; if(!q_TPS_cd(vec_cellio1_ori,vec_cellio1_tps,0,x4x4_affine,xnx4_c,xnxn_K)) { printf("ERROR: q_TPS_cd() return false!\n"); return false; } //compute TPS kernal matrix Matrix xmxn_K; if(!q_TPS_k(vec_cellio2_ori,vec_cellio1_ori,xmxn_K)) { printf("ERROR: q_TPS_k() return false!\n"); return false; } //warp the secondary cells Matrix x_ori(ql_cellio2_ori.size(),4),x_tps(ql_cellio2_ori.size(),4); for(V3DLONG i=0;i<ql_cellio2_ori.size();i++) { x_ori(i+1,1)=1.0; x_ori(i+1,2)=ql_cellio2_ori[i].x; x_ori(i+1,3)=ql_cellio2_ori[i].y; x_ori(i+1,4)=ql_cellio2_ori[i].z; } x_tps=x_ori*x4x4_affine+xmxn_K*xnx4_c; for(V3DLONG i=0;i<ql_cellio2_tps.size();i++) { ql_cellio2_tps[i].x=x_tps(i+1,2)/x_tps(1,1); ql_cellio2_tps[i].y=x_tps(i+1,3)/x_tps(1,1); ql_cellio2_tps[i].z=x_tps(i+1,4)/x_tps(1,1); } } //------------------------------------------------------------------------------------------------------------------------------------ printf("10. Map the annotated cell back to non-straightened image. \n"); QList<CellAPO> ql_musclecell_output_ori(ql_musclecell_output); //map back to non-straightened image if(vec2d_markers.size()>=2) { for(V3DLONG i=0;i<ql_musclecell_output.size();i++) { V3DLONG x=ql_musclecell_output[i].x; V3DLONG y=ql_musclecell_output[i].y; V3DLONG z=ql_musclecell_output[i].z; ql_musclecell_output_ori[i].x=vec4d_mappingfield_str2ori[y][x][z][0]; ql_musclecell_output_ori[i].y=vec4d_mappingfield_str2ori[y][x][z][1]; ql_musclecell_output_ori[i].z=vec4d_mappingfield_str2ori[y][x][z][2]; } } if(b_use_celloi_2 || !qs_filename_celloi_2_input.isEmpty()) // b_use_celloi_2=DLG_stranno.checkBox_celloi_2->isChecked() ql_musclecell_output_ori.append(ql_cellio2_tps); //save deformed point cloud to apo file if(!qs_filename_atals_output.isEmpty()) writeAPO_file(qPrintable(qs_filename_atals_output),ql_musclecell_output_ori); //show deformed atlas pts in V3D // if(!(paras_anno.b_showatlas || paras_anno.b_showsegmentation)) { v3dhandle curwin=callback.currentImageWindow(); // v3dhandle curwin=callback.getImageWindowList()[0]; callback.open3DWindow(curwin); LandmarkList curlist; for(int i=0;i<ql_musclecell_output_ori.size();i++) { LocationSimple s; s.x=ql_musclecell_output_ori[i].x+1;//note: marker coord start from 1 instead of 0 s.y=ql_musclecell_output_ori[i].y+1;//note: marker coord start from 1 instead of 0 s.z=ql_musclecell_output_ori[i].z+1;//note: marker coord start from 1 instead of 0 s.name=ql_musclecell_output_ori[i].name.toStdString(); s.radius=10; curlist << s; } callback.setLandmark(curwin,curlist); callback.updateImageWindow(curwin); callback.pushObjectIn3DWindow(curwin); } //------------------------------------------------------------------------------------------------------------------------------------ printf("11. Save segmentation label image to file. \n"); if(!qs_filename_seglabel_output.isEmpty() && p_img8u_seglabel) { V3DLONG sz_seglabelimg[4]={sz_img_input[0],sz_img_input[1],sz_img_input[2],1}; simple_saveimage_wrapper(callback, qPrintable(qs_filename_seglabel_output),p_img8u_seglabel,sz_seglabelimg,1); } //------------------------------------------------------------------------------------------------------------------------------------ //free memory printf(">>Free memory\n"); if(p_img8u_seglabel) {delete []p_img8u_seglabel; p_img8u_seglabel=0;} if(p_strimg) {delete []p_strimg; p_strimg=0;} if(sz_strimg) {delete []sz_strimg; sz_strimg=0;} if(p_img_8u) {delete []p_img_8u; p_img_8u=0;} if(p_img_input && !paras_str.b_imgfromV3D) {delete []p_img_input; p_img_input=0;} //if(sz_img_input) {delete []sz_img_input; sz_img_input=0;} //------------------------------------------------------------------------------------------------------------------------------------ v3d_msg("Program exit successfully!\n", 0); return true; }
void bwlabelimg(V3DPluginCallback2 &callback, QWidget *parent, int method_code) { v3dhandle curwin = callback.currentImageWindow(); if (!curwin) { v3d_msg("You don't have any image open in the main window."); return; } if (method_code!=3 && method_code!=2) { v3d_msg("Invalid BWLabel method code. You should never see this message. Report this bug to the developer"); return; } LabelImgObjectParaDialog dialog(callback, parent); if (!dialog.image) return; if (dialog.exec()!=QDialog::Accepted) return; V3DLONG ch = dialog.ch; V3DLONG th_idx = dialog.th_idx; double tt = dialog.thresh; V3DLONG volsz_thres = (dialog.b_filtersmallobjs) ? dialog.volsz : 0; //the threshold to filter out small objects int start_t = clock(); // record time Image4DSimple* subject = callback.getImage(curwin); QString m_InputFileName = callback.getImageName(curwin); if (!subject) { QMessageBox::information(0, title, QObject::tr("No image is open.")); return; } if (subject->getDatatype()!=V3D_UINT8) { QMessageBox::information(0, title, QObject::tr("This demo program only supports 8-bit data. Your current image data type is not supported.")); return; } if (th_idx==0 || th_idx==1) { double mm, vv; mean_and_std(subject->getRawDataAtChannel(ch), subject->getTotalUnitNumberPerChannel(), mm, vv); tt = (th_idx == 0) ? mm : mm+vv; v3d_msg(QString("in fast image object labeling: ch=%1 mean=%2 std=%2").arg(ch).arg(mm).arg(vv), 0); } Image4DProxy<Image4DSimple> pSub(subject); V3DLONG sz0 = subject->getXDim(); V3DLONG sz1 = subject->getYDim(); V3DLONG sz2 = subject->getZDim(); V3DLONG sz3 = subject->getCDim(); //---------------------------------------------------------------------------------------------------------------------------------- V3DLONG channelsz = sz0*sz1*sz2; unsigned short int *pLabel = 0; unsigned char *pData = 0; try { pLabel = new unsigned short int [channelsz]; pData = new unsigned char [channelsz]; } catch (...) { v3d_msg("Fail to allocate memory in Fast Object Labeling Plugin."); if (pLabel) {delete []pLabel; pLabel=0;} if (pData) {delete []pData; pData=0;} return; } unsigned char * pSubtmp = pSub.begin(); for(V3DLONG i = 0; i < channelsz; i++) { pData[i] = (pSubtmp[i]<=tt) ? 0 : 1; } // dist transform V3DLONG sz_data[4]; sz_data[0]=sz0; sz_data[1]=sz1; sz_data[2]=sz2; sz_data[3]=1; V3DLONG nobjs=0; if (method_code==3) { V3DLONG nh_code=26; //6,18,or 26 nobjs = findConnectedComponent(pData, sz_data, 3, nh_code, pLabel); } else if (method_code==2) { V3DLONG nh_code=8; //4 or 8 nobjs = findConnectedComponent(pData, sz_data, 2, nh_code, pLabel); } else { v3d_msg("Invalid BWLabelN method code. You should never see this message. Report this bug to the developer"); return; } if (pData) {delete []pData; pData=0;} if (volsz_thres>0) //filter out small objects { try { float * hh = new float [nobjs]; float * mapval = new float [nobjs]; V3DLONG j; for (j=0;j<nobjs;j++) {hh[j]=0; mapval[j]=j;} //of course, 0 also map to 0! for (j=0;j<channelsz;j++) { //pLabel[j]--; //it seems Fuhui's data is 1-based, so subtract 1. Is this correct? hh[pLabel[j]]++; } V3DLONG k=0; for (j=1;j<nobjs;j++) //start from 1 as it is the background! { if (hh[j]<volsz_thres) { mapval[j]=0; //if less than a thres, then map to 0, which is background } else { printf("Obj [%ld] = [%ld]\n", V3DLONG(j), V3DLONG(hh[j])); k++; mapval[j] = k; //otherwise map to a continous label-value } } for (j=0;j<channelsz;j++) pLabel[j] = mapval[pLabel[j]]; if (hh) {delete []hh; hh=0;} if (mapval) {delete []mapval; mapval=0;} } catch (...) { v3d_msg("Unable to allocate memory to filter small objects. Thus skip it."); } } //---------------------------------------------------------------------------------------------------------------------------------- int end_t = clock(); printf("time eclapse %d s for labeling objects!\n", (end_t-start_t)/1000000); Image4DSimple p4DImage; p4DImage.setData((unsigned char*)pLabel, sz0, sz1, sz2, 1, V3D_UINT16); v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, &p4DImage); callback.setImageName(newwin, QString("Object-Labeled Image")); callback.updateImageWindow(newwin); }
// show different datatype data in V3D int datatype_converting(V3DPluginCallback2 &callback, QWidget *parent) { v3dhandleList win_list = callback.getImageWindowList(); if(win_list.size()<1) { v3d_msg("No image is open."); return -1; } // DTCDialog dialog(callback,parent); if (dialog.exec()!=QDialog::Accepted) return -1; dialog.update(); V3DLONG isub = dialog.isub; V3DLONG dt = dialog.dt; // ImagePixelType cnvrt_dt; if(dt==0) { cnvrt_dt = V3D_UINT8; } else if(dt==1) { cnvrt_dt = V3D_UINT16; } else if(dt==2) { cnvrt_dt = V3D_FLOAT32; } Image4DSimple* subject = callback.getImage(win_list[isub]); if (!subject) { QMessageBox::information(0, title, QObject::tr("Image does not exist.")); return -1; } unsigned char* subject1d = subject->getRawData(); V3DLONG sx = subject->getXDim(); V3DLONG sy = subject->getYDim(); V3DLONG sz = subject->getZDim(); V3DLONG sc = subject->getCDim(); V3DLONG sz_sub = sx*sy*sz*sc; ImagePixelType sub_dt = subject->getDatatype(); //Converting if(cnvrt_dt == V3D_UINT8) { unsigned char * data1d = NULL; try { data1d = new unsigned char [sz_sub]; } catch(...) { printf("Error allocating memory. \n"); return -1; } // if(sub_dt == V3D_UINT8) { converting<unsigned char, unsigned char>((unsigned char *)subject1d, data1d, sz_sub, cnvrt_dt); } else if(sub_dt == V3D_UINT16) { converting<unsigned short, unsigned char>((unsigned short *)subject1d, data1d, sz_sub, cnvrt_dt); } else if(sub_dt == V3D_FLOAT32) { converting<float, unsigned char>((float *)subject1d, data1d, sz_sub, cnvrt_dt); } //display Image4DSimple p4DImage; p4DImage.setData((unsigned char*)data1d, sx, sy, sz, sc, V3D_UINT8); // v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, &p4DImage); callback.setImageName(newwin, "Converted Image"); callback.updateImageWindow(newwin); } else if(cnvrt_dt == V3D_UINT16) { unsigned short * data1d = NULL; try { data1d = new unsigned short [sz_sub]; } catch(...) { printf("Error allocating memory. \n"); return -1; } // if(sub_dt == V3D_UINT8) { converting<unsigned char, unsigned short>((unsigned char *)subject1d, data1d, sz_sub, cnvrt_dt); } else if(sub_dt == V3D_UINT16) { converting<unsigned short, unsigned short>((unsigned short *)subject1d, data1d, sz_sub, cnvrt_dt); } else if(sub_dt == V3D_FLOAT32) { converting<float, unsigned short>((float *)subject1d, data1d, sz_sub, cnvrt_dt); } //display Image4DSimple p4DImage; p4DImage.setData((unsigned char*)data1d, sx, sy, sz, sc, V3D_UINT16); // v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, &p4DImage); callback.setImageName(newwin, "Converted Image"); callback.updateImageWindow(newwin); } else if(cnvrt_dt == V3D_FLOAT32) { float * data1d = NULL; try { data1d = new float [sz_sub]; } catch(...) { printf("Error allocating memory. \n"); return -1; } // if(sub_dt == V3D_UINT8) { converting<unsigned char, float>((unsigned char *)subject1d, data1d, sz_sub, cnvrt_dt); } else if(sub_dt == V3D_UINT16) { converting<unsigned short, float>((unsigned short *)subject1d, data1d, sz_sub, cnvrt_dt); } else if(sub_dt == V3D_FLOAT32) { converting<float, float>((float *)subject1d, data1d, sz_sub, cnvrt_dt); } //display Image4DSimple p4DImage; p4DImage.setData((unsigned char*)data1d, sx, sy, sz, sc, V3D_FLOAT32); // v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, &p4DImage); callback.setImageName(newwin, "Converted Image"); callback.updateImageWindow(newwin); } else { printf("Currently this program only support UINT8, UINT16, and FLOAT32 data type.\n"); return -1; } // return 0; }
void FL_cellseg(V3DPluginCallback2 &callback, QWidget *parent) { v3dhandle curwin = callback.currentImageWindow(); if (!curwin) { v3d_msg("You don't have any image open in the main window."); return; } Image4DSimple* subject = callback.getImage(curwin); QString m_InputFileName = callback.getImageName(curwin); if (!subject) { QMessageBox::information(0, title, QObject::tr("No image is open.")); return; } if (subject->getDatatype()!=V3D_UINT8) { QMessageBox::information(0, title, QObject::tr("This demo program only supports 8-bit data. Your current image data type is not supported.")); return; } V3DLONG sz0 = subject->getXDim(); V3DLONG sz1 = subject->getYDim(); V3DLONG sz2 = subject->getZDim(); V3DLONG sz3 = subject->getCDim(); Image4DProxy<Image4DSimple> pSub(subject); //---------------------------------------------------------------------------------------------------------------------------------- V3DLONG channelsz = sz0*sz1*sz2; float *pLabel = 0; unsigned char *pData = 0; //get the segmentation parameters segParameter segpara; dialog_watershed_para *p_mydlg=0; if (!p_mydlg) p_mydlg = new dialog_watershed_para(&segpara, subject); int res = p_mydlg->exec(); if (res!=QDialog::Accepted) return; else p_mydlg->fetchData(&segpara); if (p_mydlg) {delete p_mydlg; p_mydlg=0;} // now allocate memory and do computation int start_t = clock(); Vol3DSimple <unsigned char> * tmp_inimg = 0; Vol3DSimple <USHORTINT16> * tmp_outimg = 0; try { tmp_inimg = new Vol3DSimple <unsigned char> (sz0, sz1, sz2); tmp_outimg = new Vol3DSimple <USHORTINT16> (sz0, sz1, sz2); } catch (...) { v3d_msg("Unable to allocate memory for processing. Do nothing."); if (tmp_inimg) {delete tmp_inimg; tmp_inimg=0;} if (tmp_outimg) {delete tmp_outimg; tmp_outimg=0;} return; } //do computation memcpy((void *)tmp_inimg->getData1dHandle(), (void *)subject->getRawDataAtChannel(segpara.channelNo), sz0*sz1*sz2); bool b_res = FL_cellseg(tmp_inimg, tmp_outimg, segpara); if (tmp_inimg) {delete tmp_inimg; tmp_inimg=0;} //free the space immediately for better use of memory if (!b_res) { v3d_msg("Fail to do the cell segmentation using FL_cellseg().\n"); } else { V3DLONG new_sz0 = tmp_outimg->sz0(); V3DLONG new_sz1 = tmp_outimg->sz1(); V3DLONG new_sz2 = tmp_outimg->sz2(); V3DLONG new_sz3 = 1; V3DLONG tunits = new_sz0*new_sz1*new_sz2*new_sz3; USHORTINT16 * outvol1d = new USHORTINT16 [tunits]; USHORTINT16 * tmpImg_d1d = (USHORTINT16 *)(tmp_outimg->getData1dHandle()); memcpy((void *)outvol1d, (void *)tmp_outimg->getData1dHandle(), tunits*sizeof(USHORTINT16)); if (tmp_outimg) {delete tmp_outimg; tmp_outimg=0;} //free the space immediately for better use of memory Image4DSimple p4DImage; p4DImage.setData((unsigned char*)outvol1d, sz0, sz1, sz2, 1, V3D_UINT16); v3dhandle newwin = callback.newImageWindow(); callback.setImage(newwin, &p4DImage); callback.setImageName(newwin, QString("Segmented Image")); callback.updateImageWindow(newwin); } //---------------------------------------------------------------------------------------------------------------------------------- int end_t = clock(); printf("time eclapse %d s for labeling objects!\n", (end_t-start_t)/1000000); }
int v3dneuron_tracing(V3DPluginCallback2 &callback, QWidget *parent) { v3dhandleList win_list = callback.getImageWindowList(); if(win_list.size()<1) { QMessageBox::information(0, title, QObject::tr("No image is open.")); return -1; } v3dhandle curwin = callback.currentImageWindow(); LandmarkList landmarks = callback.getLandmark(curwin); if(landmarks.empty()) { v3d_msg("Please set a landmark!"); return 0; } QList <ImageMarker> imagemarks; for(int i = 0; i < landmarks.size(); i++) { ImageMarker m; LocationSimple l = landmarks.at(i); m.x = l.x; m.y = l.y; m.z = l.z; imagemarks.push_back(m); } system("rm -f /tmp/mymarks.marker"); system("rm -f /tmp/tmp_out*"); writeMarker_file("/tmp/mymarks.marker",imagemarks); QString img_file = callback.getImageName(curwin); bool ok; QString nt_path = QInputDialog::getText(0, QObject::tr("Set path"), QObject::tr("v3dneuron_tracing path : "), QLineEdit::Normal, "~/Local/bin/v3dneuron_tracing", &ok); //QString paras = QObject::tr("v3dneuron_tracing -s %1 -S /tmp/mymarks.marker -o /tmp/tmp_out").arg(img_file); QString paras = QObject::tr("%1 -s \"%2\" -S /tmp/mymarks.marker -o /tmp/tmp_out").arg(nt_path).arg(img_file); qDebug(paras.toStdString().c_str()); //QMessageBox::information(0,"",paras); system(paras.toStdString().c_str()); NeuronTree nt = readSWC_file("/tmp/tmp_out_0.swc"); //nt.editable = false; callback.setSWC(curwin, nt); callback.updateImageWindow(curwin); callback.open3DWindow(curwin); //callback.getView3DControl(curwin)->setShowSurfObjects(2); //TestDialog dialog(callback, parent); //if (dialog.exec()!=QDialog::Accepted) return -1; //dialog.update(); //int i = dialog.i; //int c = dialog.channel; //Image4DSimple *p4DImage = callback.getImage(win_list[i]); //if(p4DImage->getCDim() <= c) {v3d_msg(QObject::tr("The channel isn't existed.")); return -1;} //V3DLONG sz[3]; //sz[0] = p4DImage->getXDim(); //sz[1] = p4DImage->getYDim(); //sz[2] = p4DImage->getZDim(); //unsigned char * inimg1d = p4DImage->getRawDataAtChannel(c); //v3dhandle newwin; //if(QMessageBox::Yes == QMessageBox::question(0, "", QString("Do you want to use the existing windows?"), QMessageBox::Yes, QMessageBox::No)) //newwin = callback.currentImageWindow(); //else //newwin = callback.newImageWindow(); //p4DImage->setData(inimg1d, sz[0], sz[1], sz[2], sz[3]); //callback.setImage(newwin, p4DImage); //callback.setImageName(newwin, QObject::tr("v3dneuron_tracing")); //callback.updateImageWindow(newwin); return 1; }