Back to home page

EIC code displayed by LXR

 
 

    


Warning, /geant4/examples/advanced/dna/cellularPhantom/ImageJ/phantom.ijm is written in an unsupported language. File is not indexed.

0001 // Created by
0002 // - Ph. Barberet, J. Bordes
0003 //   Bordeaux U., France
0004 //   E-mail: barberet@lp2ib.in2p3.fr
0005 // - L. Morelli
0006 //   Politecnico di Milano, Italy
0007 
0008 // Show progress
0009 showProgress(0);
0010 
0011 // The phantom file will be saved in the directory chosen by the user
0012 dir = getDirectory("Choose the output directory");
0013 
0014 // Get voxel size and image dimensions
0015 getVoxelSize(voxelWidth, voxelHeight, depth, unit);
0016 getDimensions(imgWidth, imgHeight, channels, slices, frames);
0017 
0018 // User settings dialog
0019 title = "Phantom settings";
0020 threshold1 = 0;
0021 threshold2 = 0;
0022 threshold3 = 0;
0023 Dialog.createNonBlocking(title);
0024 Dialog.addString("Output file name (.dat):", "phantom");
0025 Dialog.addNumber("Threshold red [0:255]:", 30);
0026 Dialog.addNumber("Threshold green [0:255]:", 30);
0027 Dialog.addNumber("Threshold blue [0:255]:", 30);
0028 
0029 numberSlices = 0;
0030 items = newArray("RGB", "RBG", "BRG", "BGR", "GRB", "GBR");  //Definition of color priority order (1st color priority, 2nd color priority, 3rd color priority)
0031 Dialog.addChoice("Priority", items);
0032 Dialog.show();
0033 
0034 // Read dialog parameters
0035 filename = Dialog.getString();
0036 threshold1 = Dialog.getNumber();
0037 threshold2 = Dialog.getNumber();
0038 threshold3 = Dialog.getNumber();
0039 priority = Dialog.getChoice();
0040 
0041 // Print output directory
0042 print(dir);
0043 
0044 // Generate file path
0045 path2file = dir + filename + ".dat";
0046 
0047 for (num = 0; File.exists(path2file); num++) {
0048     newfilename = filename + "_" + num;
0049     path2file = dir + newfilename + ".dat";
0050 }
0051 
0052 // Open temporary file for writing
0053 tempF = File.open(dir + "_temp.dat");
0054 
0055 // Display file parameters
0056 W = getWidth(); // Image width in voxels
0057 H = getHeight(); // Image height in voxels
0058 
0059 print("Voxel size : ", voxelWidth, " ", voxelHeight, " ", depth, " ", unit);
0060 print("Number of slices : ", slices);
0061 print("Definition : ", W, "*", H);
0062 print("Thresholds : ", threshold1, threshold2, threshold3);
0063 
0064 // Display number of voxels
0065 showStatus("Voxels count");
0066 
0067 
0068 // Initialize voxel counters
0069 numberVoxels1 = 0;
0070 numberVoxels2 = 0;
0071 numberVoxels3 = 0;
0072 
0073 
0074 // Initialize a string to store lines of data
0075 linesToWrite = "";
0076 linesArray = newArray("");
0077 
0078 // Loop through the image to write voxel coordinates and material in the phantom file
0079 for(k=0; k< nSlices; k++)
0080 {
0081   showProgress(k/(nSlices));
0082   setSlice(k+1);
0083 
0084    for(j=0; j<H; j++)
0085     {
0086        for(i=0; i< W; i++)
0087       {
0088 
0089         v=getPixel(i,j);
0090         red = (v>>16)&0xff;     //Extracting red color data - bits 23-16
0091         green = (v>>8)&0xff;    //Extracting green color data - bits 15-8
0092         blue = v&0xff;          //Extracting blue color data - bits 7-0
0093 
0094         //voxel coordinates (real units)
0095             x=i*voxelWidth;
0096             y=j*voxelWidth;
0097             z=k*depth;
0098 
0099 
               material = 0;
0100         if (priority=="RGB")  //Red has priority over blue, which has priority over green, if 2 or 3 of these colors are greater than their threshold.
0101         {
0102           if (red>=threshold1)  {
0103             numberVoxels1 +=1;
0104             material = 1;}
0105           else if (green>=threshold2) {
0106             numberVoxels2 +=1;
0107             material = 2;}
0108           else if (blue>=threshold3) {
0109             numberVoxels3 +=1;
0110             material = 3;}
0111 
0112 
0113         }
0114 
0115         else if (priority=="RBG")
0116         {
0117           if (red>=threshold1)  {
0118             numberVoxels1 +=1;
0119             material = 1;}
0120           else if (blue>=threshold3) {
0121             numberVoxels3 +=1;
0122             material = 3}
0123           else if (green>=threshold2) {
0124             numberVoxels2 +=1;
0125             material = 2;}
0126         }
0127 
0128         else if (priority=="BRG")
0129         {
0130           if (blue>=threshold3) {
0131             numberVoxels3 +=1;
0132             material = 3;}
0133           else if (red>=threshold1)  {
0134             numberVoxels1 +=1;
0135             material = 1;}
0136           else if (green>=threshold2) {
0137             numberVoxels2 +=1;
0138             material = 2;}
0139         }
0140 
0141         else if (priority=="BGR")
0142         {
0143           if (blue>=threshold3) {
0144             numberVoxels3 +=1;
0145             material = 3;}
0146           else if (green>=threshold2) {
0147             numberVoxels2 +=1;
0148             material = 2;}
0149           else if (red>=threshold1)  {
0150             numberVoxels1 +=1;
0151             material = 1;}
0152         }
0153 
0154         else if (priority=="GBR")
0155         {
0156           if (green>=threshold2) {
0157             numberVoxels2 +=1;
0158             material = 2;}
0159           else if (blue>=threshold3) {
0160             numberVoxels3 +=1;
0161             material = 3;}
0162           else if (red>=threshold1)  {
0163             numberVoxels1 +=1;
0164             material = 1;}
0165         }
0166 
0167         else if (priority=="GRB")
0168         {
0169           if (green>=threshold2) {
0170           numberVoxels2 +=1;
0171           material = 2;}
0172           else if (red>=threshold1)  {
0173           numberVoxels1 +=1;
0174           material = 1;}
0175           else if (blue>=threshold3) {
0176           numberVoxels3 +=1;
0177           material = 3;}
0178         }
0179 
0180         // Append the line to the list of lines to write
0181         if (material != 0){
0182                 print(tempF, d2s(x,4) + "  \t" + d2s(y,4) + "  \t" + d2s(z,4) + "  \t" + material + "\n");
0183         }
0184 
0185         }
0186     }
0187 }
0188 
0189 numberVoxels=numberVoxels1+numberVoxels2+numberVoxels3;
0190 
0191 // Close temporary file
0192 File.close(tempF);
0193 
0194 // Open main file for writing
0195 F = File.open(path2file);
0196 
0197 // Write header in main file
0198 print(F, numberVoxels + "\t" + numberVoxels1 + "\t" + numberVoxels2 + "\t" + numberVoxels3 + "\n");
0199 print(F, imgWidth * voxelWidth + "\t" + imgHeight * voxelWidth + "\t" + slices * depth + "\t" + unit + "\n");
0200 print(F, voxelWidth + "\t" + voxelWidth + "\t" + depth + "\t" + unit + "\n");
0201 
0202 // Read data from temporary file and write to main file
0203 data = File.openAsString(dir + "_temp.dat");
0204 print(F, data);
0205 
0206 // Close main file
0207 File.close(F);
0208 
0209 // Delete temporary file
0210 File.delete(dir + "_temp.dat");
0211 
0212 // Show completion messages
0213 showProgress(1)
0214 
0215 if (num > 0) {
0216     showMessage("WARNING: '" + filename + ".dat' file already exists.\nNew file: '" + newfilename + ".dat'");
0217 }
0218 showStatus("Completed");
0219 showMessage("Completed");