DrBurlao

DeconvolutionScript: Wiener

May 21st, 2023
66
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.15 KB | Science | 0 0
  1. import ij.ImagePlus;
  2. import ij.plugin.filter.Convolver;
  3. import ij.process.FloatProcessor;
  4. import ij.process.ImageProcessor;
  5. import ij.IJ;
  6.  
  7. // Get the list of open images in ImageJ
  8. String[] imageList = IJ.getOverlayList();
  9. int numImages = imageList.length;
  10.  
  11. // Check if there are open images
  12. if (numImages == 0) {
  13.     IJ.log("No images are open in ImageJ.");
  14. } else {
  15.     IJ.log("Starting deconvolution using Wiener space regularization algorithm for " + numImages + " image(s).");
  16.  
  17.     // Deconvolution parameters
  18.     double regularization = 0.1; // Regularization parameter
  19.  
  20.     // Process each image in the list
  21.     for (int i = 0; i < numImages; i++) {
  22.         String imageTitle = imageList[i];
  23.         IJ.log("Processing image: " + imageTitle);
  24.  
  25.         try {
  26.             // Load the original image
  27.             ImagePlus originalImage = IJ.getImage();
  28.  
  29.             // Create a duplicate of the original image to store the deconvolution result
  30.             ImagePlus deconvolvedImage = originalImage.duplicate();
  31.             String deconvolvedTitle = "Deconvolved_" + imageTitle;
  32.             deconvolvedImage.setTitle(deconvolvedTitle);
  33.  
  34.             // Get the image processors
  35.             ImageProcessor originalIP = originalImage.getProcessor();
  36.             ImageProcessor deconvolvedIP = deconvolvedImage.getProcessor();
  37.  
  38.             // Calculate the power spectrum of the original image
  39.             FloatProcessor originalPowerSpectrum = calculatePowerSpectrum(originalIP);
  40.  
  41.             // Calculate the Wiener filter
  42.             FloatProcessor wienerFilter = calculateWienerFilter(originalPowerSpectrum, regularization);
  43.  
  44.             // Perform the convolution with the Wiener filter
  45.             Convolver convolver = new Convolver();
  46.             convolver.setNormalize(true);
  47.             convolver.convolve(originalIP, wienerFilter);
  48.  
  49.             // Update the pixels of the deconvolved image
  50.             deconvolvedIP.setPixels(originalIP.getPixels());
  51.  
  52.             // Show the deconvolved image
  53.             deconvolvedImage.show();
  54.         } catch (Exception e) {
  55.             IJ.log("Error processing the image: " + imageTitle);
  56.             e.printStackTrace();
  57.         }
  58.     }
  59.  
  60.     IJ.log("Image processing finished.");
  61. }
  62.  
  63. // Function to calculate the power spectrum of an image
  64. private FloatProcessor calculatePowerSpectrum(ImageProcessor ip) {
  65.     FloatProcessor fp = ip.convertToFloatProcessor();
  66.     fp.multiply(255.0);
  67.     fp.power(2.0);
  68.     fp.transformFFT();
  69.     fp.swapQuadrants();
  70.     fp.log();
  71.     return fp;
  72. }
  73.  
  74. // Function to calculate the Wiener filter
  75. private FloatProcessor calculateWienerFilter(FloatProcessor powerSpectrum, double regularization) {
  76.     int width = powerSpectrum.getWidth();
  77.     int height = powerSpectrum.getHeight();
  78.     float[] powerPixels = (float[]) powerSpectrum.getPixels();
  79.     float[] wienerPixels = new float[width * height];
  80.  
  81.     for (int i = 0; i < powerPixels.length; i++) {
  82.         float power = powerPixels[i];
  83.         float wiener = power / (power + (float) regularization);
  84.         wienerPixels[i] = wiener;
  85.     }
  86.  
  87.     return new FloatProcessor(width, height, wienerPixels);
  88. }
  89.  
Advertisement
Comments
  • DrBurlao
    2 years
    Comment was deleted
  • DrBurlao
    2 years
    # text 1.93 KB | 0 0
    1. The provided code is an implementation of a deconvolution algorithm using the Wiener space regularization technique in ImageJ. Here's a summary of what it does:
    2.  
    3. 1. It checks if there are open images in ImageJ. If there are no open images, it logs a message indicating that.
    4. 2. If there are open images, it logs a message indicating the start of the deconvolution process.
    5. 3. It defines a regularization parameter for the deconvolution process.
    6. 4. For each open image, it performs the following steps:
    7. a. Retrieves the original image.
    8. b. Creates a duplicate of the original image to store the deconvolved result.
    9. c. Retrieves the image processors for the original and deconvolved images.
    10. d. Calculates the power spectrum of the original image.
    11. e. Calculates the Wiener filter based on the power spectrum and the regularization parameter.
    12. f. Performs convolution on the original image using the Wiener filter.
    13. g. Updates the pixels of the deconvolved image with the convolved result.
    14. h. Displays the deconvolved image.
    15. 5. If an error occurs during the processing of an image, it logs an error message and prints the stack trace.
    16. 6. Once all images have been processed, it logs a message indicating the end of the image processing.
    17.  
    18. The code also includes two helper functions:
    19. 1. `calculatePowerSpectrum`: Calculates the power spectrum of an image by converting it to a float processor, scaling the pixel values, performing FFT transformation, swapping quadrants, taking the logarithm, and returning the resulting float processor.
    20. 2. `calculateWienerFilter`: Calculates the Wiener filter based on a power spectrum and a regularization parameter. It computes the filter for each pixel by dividing the power by the sum of the power and the regularization value, and returns a float processor containing the filter values.
    21.  
    22. Overall, this code performs deconvolution using the Wiener space regularization algorithm on open images in ImageJ.
Add Comment
Please, Sign In to add comment