Esempio n. 1
0
    def cubify_ROI(self, e):
        self.manualROI_name = self.name + "_manualROI-" + self.textfield3.text

        # warn the user if that ROI exists already in the processed data
        self.processed_files = listdir(self.output_path)
        self.out_core_names = get_core_names(self.processed_files, self.file_core_name)
        if self.manualROI_name in self.out_core_names:
            print("##################                       ##################")
            print("CAREFUL!!!! This ROI already exists in your processed data:")
            print("##################                       ##################")

        print(self.manualROI_name)

        # set square roi size in the low resolution level
        gui_adjust = 128  # for historic reasons
        hr_L = int(self.textfield1.text) * gui_adjust
        # convert back to low resolution
        self.L = hr_L / self.binFactor
        # get info
        tit = self.lr_dapi.getTitle()
        self.roi = self.lr_dapi.getRoi()

        # get corners
        corners = get_corners(self.roi, self.L)
        self.corners_cleaned = clean_corners(corners, self.roi, self.L)
        # get the overlay
        self.ov = overlay_corners(self.corners_cleaned, self.L)
        self.ov = overlay_roi(self.roi, self.ov)
        # write roi name
        self.ov = write_roi_numbers(self.ov, self.corners_cleaned, self.L)
        # overlay
        self.lr_dapi.setOverlay(self.ov)
        self.lr_dapi.updateAndDraw()

        # open the Xth highest resolution one to see if it is in focus
        if int(self.textfield5.text) != 0:
            pir_for_focus = int(self.textfield5.text)
            bf_corr = self.binFactor / (self.binStep ** (pir_for_focus - 1))
            min_x = int(min([x[0] for x in self.corners_cleaned]) * bf_corr)
            min_y = int(min([x[1] for x in self.corners_cleaned]) * bf_corr)
            max_x = int((max([x[0] for x in self.corners_cleaned]) + self.L) * bf_corr)
            max_y = int((max([x[1] for x in self.corners_cleaned]) + self.L) * bf_corr)
            series_num = self.high_res_index + pir_for_focus - 1
            self.med_res_image = open_czi_series(
                self.input_path, series_num,
                rect=[min_x, min_y, max_x - min_x, max_y - min_y])  # read the image
            # play with that one, and do the real processing in the background
            self.med_res_image.show()
            self.med_res_image.setDisplayMode(IJ.COMPOSITE)
            color_order = ['Grays', 'Green', 'Red', 'Cyan']
            for c in range(self.med_res_image.getNChannels()):
                self.med_res_image.setC(c + 1)
                IJ.run(color_order[c])
                ContrastEnhancer().stretchHistogram(self.med_res_image, 0.35)
                self.med_res_image.updateAndDraw()
            # rename
            self.med_res_image.setTitle(self.manualROI_name)
Esempio n. 2
0
    def save_registration_image(self):
        reg_text_info = self.textfield4.text.split(',')
        reg_pir_num = int(reg_text_info[0])
        reg_channel = int(reg_text_info[1])
        self.reg_final_res = float(reg_text_info[2])

        self.forreg_output_path = path.join(self.output_path, "000_Slices_for_ARA_registration")

        if path.isdir(self.forreg_output_path):
            print("Output path for low resolution slices was already created")
        else:
            mkdir(self.forreg_output_path)
            print("Output path for low resolution slices created")

        # check that this slice has not been saved before
        reg_slice_name = path.join(self.forreg_output_path, self.name)
        if path.isfile(reg_slice_name + '.tif'):
            print("Registration slice already exists")
        else:
            # save otherwise
            print("Saving for registration channel {} at {} um/px".format(reg_channel, self.reg_final_res))
            # get the Xth resolution image and Xth channel for saving it for registration
            series_num = self.high_res_index + reg_pir_num
            self.raw_reg_image = open_czi_series(self.input_path, series_num)  # read the image
            self.regist_image = extractChannel(self.raw_reg_image, reg_channel, 1)
            ContrastEnhancer().stretchHistogram(self.regist_image, 0.35)
            # self.regist_image.show()

            self.regist_image.getProcessor().resetRoi()
            # reset min and max automatically
            # convert to 8-bit (which also applies the contrast)
            # ImageConverter(self.regist_image).convertToGray8()
            # convert to Xum/px so that it can be aligned to ARA
            reg_im_bin_factor = self.binStep ** reg_pir_num
            regres_resolution = reg_im_bin_factor * self.res_xy_size
            rescale_factor = regres_resolution / self.reg_final_res
            new_width = int(rescale_factor * self.regist_image.getWidth())
            # self.lr_dapi_reg.getProcessor().scale(rescale_factor, rescale_factor)
            ip = self.regist_image.getProcessor().resize(new_width)
            self.regist_image.setProcessor(ip)
            # Add the information to the metadata
            self.regist_image.getCalibration().pixelWidth = self.reg_final_res
            self.regist_image.getCalibration().pixelHeight = self.reg_final_res
            self.regist_image.getCalibration().pixelDepth = 1
            self.regist_image.getCalibration().setXUnit("micrometer")
            self.regist_image.getCalibration().setYUnit("micrometer")
            self.regist_image.getCalibration().setZUnit("micrometer")
            # self.lr_dapi_reg.getProcessor().resetRoi()
            IJ.saveAsTiff(self.regist_image, reg_slice_name)
            self.regist_image.close()
            self.regist_image.flush()
            print("Slice for registration saved")
Esempio n. 3
0
    def open_lowres_image(self, e):
        sender = e.getSource()
        IJ.run("Close All")
        if not e.getValueIsAdjusting():
            self.name = sender.getSelectedValue()
            print(self.name)
            # parse the slice number
            self.sl_num = int(self.name.split('-')[-1])
            print("Opening slice " + str(self.sl_num))

            if not path.exists(self.input_path):
                print("I don't find the file, which is weird as I just found it before")
            else:
                # get the number of piramids for that image, the index of highres and the binning
                self.num_of_piramids = self.num_of_piramids_list[self.sl_num]
                self.high_res_index = self.max_res_indexes[self.sl_num]
                self.binFactor = self.binFactor_list[self.sl_num]
                self.binStep = self.binStep_list[self.sl_num]
                # get the lowest resolution binned, depending on the number
                # of resolutions. The order is higher to lower.
                series_num = self.high_res_index + self.num_of_piramids - 1
                self.low_res_image = open_czi_series(self.input_path, series_num)  # read the image
                # save the resolution (every image has the high-resolution information)
                self.res_xy_size = self.low_res_image.getCalibration().pixelWidth
                self.res_units = self.low_res_image.getCalibration().getXUnit()
                # play with that one, and do the real processing in the background
                # select the DAPI channel and adjust the intensity
                self.lr_dapi = extractChannel(self.low_res_image, 1, 1)
                ContrastEnhancer().stretchHistogram(self.lr_dapi, 0.35)
                self.lr_dapi.setTitle(self.name)
                self.lr_dapi.show()
                # reposition image
                self.lr_dapi.getWindow().setLocation(420, 10)
                self.lr_dapi.updateAndDraw()

                # clean
                self.low_res_image.close()
                self.low_res_image.flush()
Esempio n. 4
0
    def save_ROIs(self, e):
        # save the low resolution image for registration
        self.save_registration_image()

        print('Saving ROIs')
        # add a counter for the ROI name
        roiID = 1

        # create a file to save the ROI coordinates
        # create output directory if it doesn't exist
        self.roi_output_path = path.join(self.output_path, "000_manualROIs_info")
        if path.isdir(self.roi_output_path):
            print("Output path for ROIs information was already created")
        else:
            mkdir(self.roi_output_path)
            print("Output path for ROIs created")
        roi_points_file_path = path.join(self.roi_output_path, self.manualROI_name +
                                         "_roi_positions.txt")
        with open(roi_points_file_path, "w") as roi_points_file:
            roi_points_file.write("{}, {}, {}, {}, {}, {}".
                                  format("roiID", "high_res_x_pos",
                                         "high_res_y_pos", "registration_image_pixel_size",
                                         "high_res_pixel_size", "units"))

        # for each roi
        for [x, y] in self.corners_cleaned:
            print('   -> processing square ROI number ' + str(roiID))
            # tranlate coordinates to high resolution
            xt = int(x * self.binFactor)
            yt = int(y * self.binFactor)
            Lt = int(self.L * self.binFactor)
            # save the corner coordinates of the ROI in a file
            with open(roi_points_file_path, "a") as roi_points_file:
                roi_points_file.write("\n{}, {}, {}, {}, {}, {}".
                                      format(roiID, xt, yt, self.reg_final_res,
                                             self.res_xy_size, self.res_units))

            # open the high resolution image on that roi
            series_num = self.high_res_index
            hr_imp = open_czi_series(self.input_path, series_num, rect=[xt, yt, Lt, Lt])
            # hr_imp.show()
            # name of this ROI
            Roi_name = self.manualROI_name + "_squareROI-" + str(roiID)
            # for each of the channels
            for c in range(1, (hr_imp.getNChannels() + 1)):
                # get the channel
                channel = extractChannel(hr_imp, c, 1)
                # save with coherent name
                IJ.saveAsTiff(channel, path.join(self.output_path, Roi_name + "_channel-" + str(c)))
                # close and flush memory
                channel.close()
                channel.flush()
            # increase counter
            roiID += 1
            # close and flush memory
            hr_imp.close()
            hr_imp.flush()
        print('ROIs saved, saving summary figure')

        # save summary
        # create output directory if it doesn't exist
        self.summary_output_path = path.join(self.output_path, "000_Summary_of_ROIs")
        if path.isdir(self.summary_output_path):
            print("Output path for summary was already created")
        else:
            mkdir(self.summary_output_path)
            print("Output path for summary created")
        IJ.selectWindow(self.name)
        IJ.run("Flatten")
        imp = IJ.getImage()
        IJ.saveAsTiff(imp, path.join(
            self.summary_output_path,
            self.manualROI_name + "_summaryImage"))
        print("summary image saved")
        IJ.run("Close All")

        # save manual ROI
        RoiEncoder.save(self.roi, path.join(self.roi_output_path, self.manualROI_name))
        print("roi information saved, closing images and finishing")
        IJ.run("Close All")
    # for each slice name:
    for sl_name in possible_slices:
        # parse the slice number
        sl_num = int(sl_name.split('-')[-1])
        print("Processing image " + sl_name)
        # get info
        num_of_piramids = num_of_piramids_list[sl_num]
        binFactor = binFactor_list[sl_num]
        high_res_index = max_res_indexes[sl_num]
        binStep = binStep_list[sl_num]
        # open the image
        # get the Xth resolution binned, depending on the number
        # of resolutions. The order is higher to lower.
        series_num = high_res_index + piramid_to_open
        raw_image = open_czi_series(input_path, series_num)
        # save the resolution (every image has the high-resolution information)
        res_xy_size = raw_image.getCalibration().pixelWidth
        res_units = raw_image.getCalibration().getXUnit()
        # select the requested channel and adjust the intensity
        regist_image = extractChannel(raw_image, channel_to_save, 1)
        # TODO: test if contrast enhancement and background sustraction are needed for registration
        ContrastEnhancer().stretchHistogram(regist_image, 0.35)
        # IMPLEMENT BACKGROUND SUSTRACTION HERE
        regist_image.setTitle(sl_name)
        #ch_image.show()
        # clean
        raw_image.close()
        raw_image.flush()
        # convert to Xum/px so that it can be aligned to ARA
        reg_im_bin_factor = binStep**piramid_to_open