bool autotrace(const V3DPluginArgList & input, V3DPluginArgList & output,V3DPluginCallback2 &callback) { cout<<"Welcome to MOST tracing"<<endl; unsigned int c=1; int InitThreshold = 20; seed_size_all = 20; int slipsize=20; if (input.size()>=2) { vector<char*> paras = (*(vector<char*> *)(input.at(1).p)); cout<<paras.size()<<endl; if(paras.size() >= 1) c = atoi(paras.at(0)); if(paras.size() >= 2) InitThreshold = atoi(paras.at(1)); if(paras.size() >= 3) seed_size_all = atoi(paras.at(2)); if(paras.size() >= 4) slipsize = atoi(paras.at(3)); } char * inimg_file = ((vector<char*> *)(input.at(0).p))->at(0); cout<<"ch = "<<c<<endl; cout<<"threshold = "<<InitThreshold<<endl; cout<<"inimg_file = "<<inimg_file<<endl; cout<<"seedsize = "<<seed_size_all<<endl; cout<<"slipsize = "<<slipsize<<endl; Image4DSimple *subject = callback.loadImage(inimg_file); if(!subject || !subject->valid()) { v3d_msg("Fail to load the input image.",0); if (subject) {delete subject; subject=0;} return false; } if( c < 1 || c > subject->getCDim()) { v3d_msg("Invalid channel input.",0); if (subject) {delete subject; subject=0;} return false; } V3DLONG N = subject->getXDim(); V3DLONG M = subject->getYDim(); V3DLONG P = subject->getZDim(); V3DLONG pagesz = N*M*P; int datatype = subject->getDatatype(); unsigned char *data1d = subject->getRawDataAtChannel(c-1); unsigned char *output_image=0; switch (datatype) { case V3D_UINT8: try {output_image = new unsigned char [pagesz];} catch(...) {v3d_msg("cannot allocate memory for output_image.",0); return false;} for(V3DLONG i = 0; i<pagesz; i++) output_image[i] = data1d[i]; break; default: v3d_msg("Invalid data type. Do nothing.",0); return false; } MOSTImage img; img.setData( (unsigned char*)output_image, subject->getXDim(),subject->getYDim(),subject->getZDim(),subject->getCDim(),subject->getDatatype()); QString swcfile = QString(inimg_file) + "_MOST.swc"; LandmarkList seedList; QTime qtime_seed; qtime_seed.start(); if(1) { for(int i =1;i<=N;i+=20) img.auto_detect_seedx(seedList,i,InitThreshold,seed_size_all); } if(1) { for(int i =1;i<=M;i+=20) img.auto_detect_seedy(seedList,i,InitThreshold,seed_size_all); } if(1) { for(int i =1;i<=P;i+=20) img.auto_detect_seedz(seedList,i,InitThreshold,seed_size_all); } qDebug(" cost time seed = %g sec", qtime_seed.elapsed()*0.001); static long init_flag = 0; for(init_flag = 0;init_flag<subject->getTotalUnitNumberPerChannel();init_flag++) { visited.push_back(false); } // converte the formate NeuronTree vt; QTime qtime; qtime.start(); vt = img.trace_seed_list(seedList, visited,InitThreshold,1.0,1.0,1.0,swcfile,slipsize,0); qDebug(" cost time totol= %g sec", qtime.elapsed()*0.001); v3d_msg(QString("\nNow you can drag and drop the generated swc fle [%1] into Vaa3D.").arg(swcfile),0); if (subject) {delete subject; subject=0;} return true; }
bool findedgeimg(V3DPluginCallback2 &callback, const V3DPluginArgList & input, V3DPluginArgList & output) { cout<<"Welcome to Label edge of a mask image"<<endl; if (output.size() != 1) return false; int method_code = 0; if (input.size()>=2) { vector<char*> paras = (*(vector<char*> *)(input.at(1).p)); if(paras.size() >= 1) method_code = atoi(paras.at(0)); } char * inimg_file = ((vector<char*> *)(input.at(0).p))->at(0); char * outimg_file = ((vector<char*> *)(output.at(0).p))->at(0); cout<<"method_code = "<<method_code<<endl; cout<<"inimg_file = "<<inimg_file<<endl; cout<<"outimg_file = "<<outimg_file<<endl; Image4DSimple *image = callback.loadImage(inimg_file); if (!image || !image->valid()) { cerr<<"load image "<<inimg_file<<" error!"<<endl; return false; } V3DLONG szx=image->getXDim(), szy=image->getYDim(), szz=image->getZDim(), szc=image->getCDim(); V3DLONG N = image->getTotalUnitNumber(); //create the output buffer unsigned char *outputData = 0; try { outputData = new unsigned char [N]; for (V3DLONG tmpi=0; tmpi<N; ++tmpi) outputData[tmpi] = 0; //preset to be all 0 } catch (...) { v3d_msg("Fail to allocate memory."); if (outputData) { delete []outputData; outputData=0; } return false; } Image4DSimple outputImage; outputImage.setData((unsigned char*)outputData, szx, szy, szz, szc, V3D_UINT8); Image4DProxy<Image4DSimple> outputIProxy(&outputImage); //now do computation { bool bset255 = (method_code==0) ? false : true; Image4DProxy<Image4DSimple> p(image); Image4DProxy_foreach(p, ix, iy, iz, ic) { double v = p.value_at(ix, iy, iz, ic); V3DLONG cx, cy, cz; bool bb=false; for (cz = iz-1; cz<iz+2; ++cz) { for (cy = iy-1; cy<iy+2; ++cy) { for (cx = ix-1; cx<ix+2; ++cx) { if (!p.is_inner(cx, cy, cz, ic)) continue; if (v!=p.value_at(cx, cy, cz, ic)) { *outputIProxy.at(ix, iy, iz, ic) = (bset255) ? 255 : v; bb = true; break; } } if (bb) break; } if (bb) break; } //note that all value had been preset as 0, thus no need to set as the background color in case not an edge point } }
void set_dialog(V3DPluginCallback2 &v3d, QWidget *parent) { int x_begin; int x_end; int x_distance; int y_begin; int y_end; int y_distance; int z_begin; int z_end; int z_distance; int pruning_flag; int c; QString swcfile; int sslip; x_flag = 0; y_flag = 0; z_flag = 0; pruning_flag = 0; v3dhandle curwin = v3d.currentImageWindow(); if (!curwin) { v3d_msg("You don't have any image open in the main window."); return; } Image4DSimple* img = v3d.getImage(curwin); if (!img || !img->valid()) return; AdaTDialog dialog; dialog.endx->setMaximum(img->getXDim()); dialog.endx->setMinimum(1); dialog.endx->setValue(img->getXDim()); dialog.endy->setMaximum(img->getYDim()); dialog.endy->setMinimum(1); dialog.endy->setValue(img->getYDim()); dialog.endz->setMaximum(img->getZDim()); dialog.endz->setMinimum(1); dialog.endz->setValue(img->getZDim()); dialog.ds->setText(QString(img->getFileName()) + "_MOST.swc"); dialog.channel->setMaximum(img->getCDim()); dialog.channel->setMinimum(1);dialog.channel->setValue(1); if (dialog.exec()!=QDialog::Accepted) return; else { InitThreshold = dialog.threshould_value->value(); res_x_all = dialog.resx->value(); res_y_all = dialog.resy->value(); res_z_all = dialog.resz->value(); seed_size_all = dialog.size->value(); sslip = dialog.slipsize->value(); swcfile = dialog.ds->text(); c = dialog.channel->value(); QFile file(swcfile); if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) { v3d_msg("Cannot create the swc file to write, please try it again! "); return; } if(dialog.x_select->isChecked()) { x_flag =1; x_begin = dialog.beginx->value(); x_end = dialog.endx->value(); x_distance = dialog.distancex->value(); } if(dialog.y_select->isChecked()) { y_flag =1; y_begin = dialog.beginy->value(); y_end = dialog.endy->value(); y_distance = dialog.distancey->value(); } if(dialog.z_select->isChecked()) { z_flag =1; z_begin = dialog.beginz->value(); z_end = dialog.endz->value(); z_distance = dialog.distancez->value(); } // v3d.open3DWindow(curwin); if(dialog.pruning->isChecked()) { pruning_flag =1; } startVesselTracing(v3d,x_flag,y_flag,z_flag,x_begin,x_end,x_distance,y_begin,y_end,y_distance,z_begin,z_end,z_distance,swcfile,sslip,pruning_flag,c); } return; }
void ThreadedTracer::run(){ qDebug()<<"loadScan input: "<<latestString; //QString latestString = getFileString(); // latestString =QString("/data/mat/BRL/DATA_FOR_GROUP/testDataForZhi/ZSeries-06092016-1407-8470/ZSeries-06092016-1407-8470_Cycle00001_Ch1_000001.ome.tif"); // bool isAdaptive = 1; // int methodChoice = 1; //LandmarkList inputRootList; QString fString; fString = QString("Cycle%1").arg(tileNumber,5,10,QLatin1Char('0')); qDebug()<<tileNumber; QList<LandmarkList> newTipsList; LandmarkList newTargetList; QFileInfo imageFileInfo = QFileInfo(latestString); if (imageFileInfo.isReadable()){ Image4DSimple * pNewImage = cb->loadImage(latestString.toLatin1().data()); QDir imageDir = imageFileInfo.dir(); QStringList filterList; filterList.append(QString("*").append(fString).append("_Ch").append(channel).append("*.tif")); qDebug()<<"filterlist.first()"<<filterList.first(); imageDir.setNameFilters(filterList); QStringList fileList = imageDir.entryList(); //use this to id the number of images in the stack (in one channel?!) V3DLONG x = pNewImage->getXDim(); V3DLONG y = pNewImage->getYDim(); V3DLONG nFrames = fileList.length(); V3DLONG tunits = x*y*nFrames; unsigned short int * total1dData = new unsigned short int [tunits]; unsigned short int * total1dData_mip= new unsigned short int [x*y]; for(V3DLONG i =0 ; i < x*y; i++) total1dData_mip[i] = 0; V3DLONG totalImageIndex = 0; double p_vmax=0; qDebug()<<"nFrames = "<<nFrames; for (int f=0; f<nFrames; f++){ qDebug()<<fileList[f]; Image4DSimple * pNewImage = cb->loadImage(imageDir.absoluteFilePath(fileList[f]).toLatin1().data()); if (pNewImage->valid()){ unsigned short int * data1d = 0; data1d = new unsigned short int [x*y]; data1d = (unsigned short int*)pNewImage->getRawData(); for (V3DLONG i = 0; i< (x*y); i++) { total1dData[totalImageIndex]= data1d[i]; if(data1d[i] > p_vmax) p_vmax = data1d[i]; if(total1dData_mip[i] < data1d[i]) total1dData_mip[i] = data1d[i]; totalImageIndex++; } if(data1d) {delete []data1d; data1d = 0;} }else{ qDebug()<<imageDir.absoluteFilePath(fileList[f])<<" failed!"; } } Image4DSimple* total4DImage = new Image4DSimple; total4DImage->setData((unsigned char*)total1dData, x, y, nFrames, 1, V3D_UINT16); Image4DSimple* total4DImage_mip = new Image4DSimple; total4DImage_mip->setData((unsigned char*)total1dData_mip, x, y, 1, 1, V3D_UINT16); QString swcString = saveDirString; swcString.append("/x_").append(QString::number((int)tileLocation.x)).append("_y_").append(QString::number((int)tileLocation.y)).append("_").append(imageFileInfo.fileName()).append(fString).append(".swc"); QString scanDataFileString = saveDirString; scanDataFileString.append("/").append("scanData.txt"); qDebug()<<scanDataFileString; QFile saveTextFile; saveTextFile.setFileName(scanDataFileString);// add currentScanFile if (!saveTextFile.isOpen()){ if (!saveTextFile.open(QIODevice::Text|QIODevice::Append )){ qDebug()<<"unable to save file!"; return;} } QTextStream outputStream; outputStream.setDevice(&saveTextFile); total4DImage->setOriginX(tileLocation.x); total4DImage->setOriginY(tileLocation.y); qDebug()<<total4DImage->getOriginX(); outputStream<< (int) total4DImage->getOriginX()<<" "<< (int) total4DImage->getOriginY()<<" "<<swcString<<" "<< (int) x<<" "<< (int) y<<" "<<"\n"; saveTextFile.close(); V3DLONG mysz[4]; mysz[0] = total4DImage->getXDim(); mysz[1] = total4DImage->getYDim(); mysz[2] = total4DImage->getZDim(); mysz[3] = total4DImage->getCDim(); tileLocation.ev_pc1 = (double) total4DImage->getXDim(); tileLocation.ev_pc2 = (double) total4DImage->getYDim(); QString imageSaveString = saveDirString; // add bit of code to image green - red channels. This will require an additional argument or another signal/slot combination to monitor the value of a combobox in the GUI... // add button to do || nT on mXtls //convert to 8bit image using 8 shiftnbits unsigned char * total1dData_8bit = 0; try { total1dData_8bit = new unsigned char [tunits]; } catch (...) { v3d_msg("Fail to allocate memory in total1dData_8bit.\n"); return; } double dn = pow(2.0, double(5)); for (V3DLONG i=0;i<tunits;i++) { double tmp = (double)(total1dData[i]) / dn; if (tmp>255) total1dData_8bit[i] = 255; else total1dData_8bit[i] = (unsigned char)(tmp); } total4DImage->setData((unsigned char*)total1dData_8bit, x, y, nFrames, 1, V3D_UINT8); imageSaveString.append("/x_").append(QString::number((int)tileLocation.x)).append("_y_").append(QString::number((int)tileLocation.y)).append("_").append(imageFileInfo.fileName()).append(imageFileInfo.fileName()).append(fString).append(".v3draw"); simple_saveimage_wrapper(*cb, imageSaveString.toLatin1().data(),(unsigned char *)total1dData_8bit, mysz, V3D_UINT8); qDebug()<<"=== immediately before tracing ====="; //------------- V3DPluginArgItem arg; V3DPluginArgList input; V3DPluginArgList output; QString full_plugin_name; QString func_name; arg.type = "random";std::vector<char*> arg_input; std:: string fileName_Qstring(imageSaveString.toStdString());char* fileName_string = new char[fileName_Qstring.length() + 1]; strcpy(fileName_string, fileName_Qstring.c_str()); arg_input.push_back(fileName_string); arg.p = (void *) & arg_input; input<< arg; char* char_swcout = new char[swcString.length() + 1];strcpy(char_swcout, swcString.toStdString().c_str()); arg.type = "random";std::vector<char*> arg_output;arg_output.push_back(char_swcout); arg.p = (void *) & arg_output; output<< arg; arg.type = "random"; std::vector<char*> arg_para; arg_para.push_back("1"); arg_para.push_back("1"); full_plugin_name = "neuTube"; func_name = "neutube_trace"; arg.p = (void *) & arg_para; input << arg; if(!cb->callPluginFunc(full_plugin_name,func_name,input,output)) { qDebug()<<("Can not find the tracing plugin!\n"); return; } emit done(); }else{ qDebug()<<"invalid image"; } }