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 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); } }
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 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); }