def ij_macro_processor(fus_tiffs_dict, py_file_loc): """ Uses a specified ImageJ macro to process the fused tiffs located in the FUSED_TIFFS directory and then saves the processed output in PROCESSED_TIFFS directory. """ stitch_path = os.path.dirname(os.path.abspath(py_file_loc)) for key in fus_tiffs_dict: if fus_tiffs_dict[key][0] == '': logging.info('Companion file not available for' + key) logging.info('Running imageJ macro on ' + key) IJ.runMacroFile(stitch_path + r"\stitch_macro.ijm") IJ.saveAs("tiff", fus_tiffs_dict[key][1] + '/' + os.path.basename(key)) IJ.run("Close All") else: logging.info('Running imageJ macro on ' + key) IJ.runMacroFile(stitch_path + r"\stitch_macro.ijm", key + "<>" + fus_tiffs_dict[key][0]) IJ.saveAs("tiff", fus_tiffs_dict[key][1] + '/' + os.path.basename(key)) IJ.run("Close All")
def straighten(stackName): ''' IJ.run("Set Measurements...", "area mean min center redirect=None decimal=3") IJ.open(stackName) imp = IJ.getImage() stack = imp.getImageStack() for i in xrange(1, imp.getNSlices()+1): image = ImagePlus(str(i), stack.getProcessor(i)) xvals = [] yvals = [] maxvals = [] j = 0 for k in xrange(0, 512, 2): IJ.makeRectangle(k, 0, 4, 512) IJ.run("Measure") table = RT.getResultsTable() x = table.getValue("XM", 0) y = table.getValue("YM", 0) #maxi = IJ.getResult("Max") table = [] xvals.append(k) yvals.append(y) #maxvals.append(maxi) #if maxvals[j] == 0 and j > 0: #yvals[j] = yvals[j-1] j += 1 print "xvals:", xvals print "yvals:", yvals mr = MR() IJ.run("Make Selection...", "freeline, "+str(xvals)+" ,"+ str(yvals)) #IJ.runMacro("makeSelection...", "freeline, "+str(xvals)+" ,"+ str(yvals)); #IJ.run("makeSelection(\"freeline\", xvals, yvals);") #IJ.run("Straighten...", "line = 80") ''' IJ.open(stackName) imp = IJ.getImage() stack = imp.getImageStack() print imp.getNSlices() for i in range(1, imp.getNSlices() + 1): image = ImagePlus(str(i), stack.getProcessor(i)) IJ.runMacroFile( "/Users/juliansegert/repos/Bio119_root_tracking/straightenOneImage.ijm" ) newImp = WM.getCurrentImage() fs = FileSaver(newImp) fs.saveAsTiff( "/Users/juliansegert/Documents/Roots/Four_root_image_stacks/Pos01/" + str(i) + ".tif") newImp.close()
def straighten(stackName): ''' IJ.run("Set Measurements...", "area mean min center redirect=None decimal=3") IJ.open(stackName) imp = IJ.getImage() stack = imp.getImageStack() for i in xrange(1, imp.getNSlices()+1): image = ImagePlus(str(i), stack.getProcessor(i)) xvals = [] yvals = [] maxvals = [] j = 0 for k in xrange(0, 512, 2): IJ.makeRectangle(k, 0, 4, 512) IJ.run("Measure") table = RT.getResultsTable() x = table.getValue("XM", 0) y = table.getValue("YM", 0) #maxi = IJ.getResult("Max") table = [] xvals.append(k) yvals.append(y) #maxvals.append(maxi) #if maxvals[j] == 0 and j > 0: #yvals[j] = yvals[j-1] j += 1 print "xvals:", xvals print "yvals:", yvals mr = MR() IJ.run("Make Selection...", "freeline, "+str(xvals)+" ,"+ str(yvals)) #IJ.runMacro("makeSelection...", "freeline, "+str(xvals)+" ,"+ str(yvals)); #IJ.run("makeSelection(\"freeline\", xvals, yvals);") #IJ.run("Straighten...", "line = 80") ''' IJ.open(stackName) imp = IJ.getImage() stack = imp.getImageStack() print imp.getNSlices() for i in range(1, imp.getNSlices()+1): image = ImagePlus(str(i), stack.getProcessor(i)) IJ.runMacroFile("/Users/juliansegert/repos/Bio119_root_tracking/straightenOneImage.ijm") newImp = WM.getCurrentImage() fs = FileSaver(newImp) fs.saveAsTiff("/Users/juliansegert/Documents/Roots/Four_root_image_stacks/Pos01/"+str(i)+".tif") newImp.close()
def run_macro(script_dir, script_fname): """ Run any IJ macro Parameters ---------- script_dir : str Directory of script to run script_fname : str Name of script to run """ IJ.runMacroFile(os.path.join(script_dir, script_fname))
def __calRois(self, imp, indice) : """ Returns the ROIs of a slice given (identified with its n°) in a stack """ ##imp=self.__dictImages[nameimages] # IL FAUT RÉCUPÉRER L'IMAGE DU STACK !!!!! #if self.__batch : imp.hide() #else : imp.show() #imp.hide() imp.show() if self.__batch : imp.hide() imp.setSlice(indice) imp.killRoi() ip = imp.getProcessor() bs=BackgroundSubtracter() #if str(self.__subback) == "0" or str(self.__subback) == "1" : self.__subback = bool(int(self.__subback)) #if self.__subback == True : IJ.run(imp, "Subtract Background...", "rolling="+str(self.__radius)+" light") if self.__subback == True : bs.rollingBallBackground(ip, self.__radius, False, True, False, True, False) if self.__runmacro : imp.show() imp.setSlice(indice) imp.updateAndDraw() IJ.runMacroFile(self.__macropath, imp.getTitle()) imp.updateAndDraw() #if str(self.__manthresh) == "0" or str(self.__manthresh) == "1" : self.__manthresh = bool(int(self.__manthresh)) #if self.__manthresh : IJ.setThreshold(imp, self.__minthr, self.__maxthr) if self.__manthresh : ip.setThreshold(self.__minthr, self.__maxthr, ImageProcessor.RED_LUT) else : self.__setThreshold(imp, indice) rt=ResultsTable() pa1=ParticleAnalyzer(ParticleAnalyzer.SHOW_MASKS+ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES , Measurements.AREA, rt, self.__minArea, self.__maxArea, self.__minCirc, self.__maxCirc) pa1.setHideOutputImage(True) pa1.analyze(imp) masks=pa1.getOutputImage() masks.getProcessor().erode() masks.getProcessor().dilate() masks.getProcessor().invertLut() masks.getProcessor().threshold(1) rm = RoiManager.getInstance() if (rm==None): rm = RoiManager() rm.runCommand("reset") #rm.hide() pa2=ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER+ParticleAnalyzer.CLEAR_WORKSHEET+ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES , Measurements.AREA, rt, self.__minArea, self.__maxArea, self.__minCirc, self.__maxCirc) pa2.analyze(masks) masks.close() temparray=rm.getRoisAsArray() for r in temparray : tempnameroi=r.getName() r.setPosition(indice) r.setName(str(indice)+"-"+tempnameroi) r.setStrokeWidth(1) if len(self.__params) > 0 : for k in self.__params: #if k[0]=="Area": self.__minArea, self.__maxArea = str(k[1]), str(k[2]) if k[0]=="Area": self.__minArea, self.__maxArea = k[1], k[2] for k in self.__params: #if k[0]=="Circ": self.__minCirc, self.__maxCirc = str(k[1]), str(k[2]) if (k[0]=="Circ") and k[3] : self.__minCirc, self.__maxCirc = k[1], k[2] else : self.__minCirc, self.__maxCirc = 0, 1 self.__rr.setRoisarray(temparray, imp) self.__rr.setRange(indice, self.__params) return self.__rr.includeRois else : return temparray
USERNAME = myvars['username'] PASSWORD = myvars['password'] # Prototype analysis example gateway = omeroConnect() imageIds = getImageIds(gateway,datasetId); imageIds.sort() for imageId in imageIds: # imageId = imageIds[2] print imageId openImagePlus(HOST,USERNAME,PASSWORD,groupId,imageId) IJ.run("Enhance Contrast", "saturated=0.35"); #Plug Your analysis here# IJ.runMacroFile(macroFilePath) # Save resultant image using Bio-Formats imp = IJ.getImage(); path = paths + imp.getTitle() + operation + ".ome.tiff"; print(path) options = "save=[" + path + "] export compression=Uncompressed" IJ.run(imp, "Bio-Formats Exporter", options); imp.changes = False imp.close() # Upload image to OMERO str2d = java.lang.reflect.Array.newInstance(java.lang.String,[1]) str2d [0] = path success = uploadImage(str2d, gateway)
from ij import IJ values = [ str(0), r"C:\Users\John Cooper\Box Sync\Finkelstein-Matouschek\byc_data\example_byc_expts\20200221_byc_analysis\pJC069_rpn4d\image.tif", "another path", "death", "measurement" ] arg = ' '.join(values) IJ.runMacroFile('runpyscript.ijm', arg)
from ij import IJ #this code should run following the command code to search for #new directory and create registered/stitched folder """ running the motion correction automatically with input directory """ #dir_path = getArgument() #prepare the reference image dir_path = "/Users/phoenix/Documents/Kwanlab/dataanalysis/discrimination/161013_try/" imp = IJ.openImage(dir_path + "raw/161013_task3079.tif") IJ.run(imp, "32-bit", "") #convert the image to 32 bit ave_imp = IJ.run(imp, "Z Project...", "projection=[Average Intensity]") #acquire average intensity IJ.run(imp, "Close", "") IJ.run("Save", "save=[" + dir_path + "refFrame.tif]") #save the reference file to be used later IJ.run( ave_imp, "Close", "" ) #close all the files and open the reference file in the batchTurboReg_forPy.ijm file #run the ijm (modified to a little to suit the python code macroFileDir = "/Users/phoenix/Documents/Kwanlab/dataanalysis/batchTurboReg_forPy.ijm" IJ.runMacroFile(macroFileDir, dir_path)
#for imageId, imageName in imageIds[1:], imageNames[1:]: # imageId = imageIds[2] imageId = imageIds[counter] imageName = imageNames[counter] print imageId, imageName if imageName != imageNames[counter+1]: #print("this is a new image") #print(done) openImagePlus(HOST,USERNAME,PASSWORD,groupId,imageId) #print("opened image") #IJ.run("Enhance Contrast", "saturated=0.35"); #Plug Your analysis here# imp = w.getImage(imageName); IJ.runMacroFile(str(macroFilePath)) #print("ran macro") imp.close() #print("closed image") done.append(imageName) counter += 1 # Save resultant image using Bio-Formats # #path = paths + imp.getTitle() + operation + ".ome.tiff"; #print(path) #options = "save=" + path + " export compression=Uncompressed" #IJ.run(imp, "Bio-Formats Exporter", options); #imp.changes = False # # Upload image to OMERO
USERNAME = myvars['username'] PASSWORD = myvars['password'] # Prototype analysis example gateway = omeroConnect() imageIds = getImageIds(gateway,datasetId); imageIds.sort() #openImagePlus(HOST,USERNAME,PASSWORD,groupId,imageIds[0]) for imageId in imageIds[1:]: # imageId = imageIds[2] print imageId openImagePlus(HOST,USERNAME,PASSWORD,groupId,imageId) #IJ.run("Enhance Contrast", "saturated=0.35"); #Plug Your analysis here# imp = IJ.getImage(); IJ.runMacroFile(str(macroFilePath)) imp.close() # Save resultant image using Bio-Formats # #path = paths + imp.getTitle() + operation + ".ome.tiff"; #print(path) #options = "save=" + path + " export compression=Uncompressed" #IJ.run(imp, "Bio-Formats Exporter", options); #imp.changes = False # # Upload image to OMERO #str2d = java.lang.reflect.Array.newInstance(java.lang.String,[1]) #str2d [0] = path #success = uploadImage(str2d, gateway)
USERNAME = myvars['username'] PASSWORD = myvars['password'] # Prototype analysis example gateway = omeroConnect() imageIds = getImageIds(gateway,datasetId); imageIds.sort() for imageId in imageIds: # imageId = imageIds[2] print imageId openImagePlus(HOST,USERNAME,PASSWORD,groupId,imageId) IJ.run("Enhance Contrast", "saturated=0.35"); #Plug Your analysis here# IJ.runMacroFile(macroFilePath) # Save resultant image using Bio-Formats imp = IJ.getImage(); path = paths + imp.getTitle() + operation + ".ome.tiff"; print(path) options = "save=" + path + " export compression=Uncompressed" IJ.run(imp, "Bio-Formats Exporter", options); imp.changes = False imp.close() # Upload image to OMERO str2d = java.lang.reflect.Array.newInstance(java.lang.String,[1]) str2d [0] = path success = uploadImage(str2d, gateway)