Advertisement
KirilGeorgiev

Class ImageProcessing

May 7th, 2021 (edited)
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.16 KB | None | 0 0
  1. import ij.fach.FilterValue;
  2.  
  3. import java.awt.Color;
  4. import java.awt.image.BufferedImage;
  5. import java.awt.image.IndexColorModel;
  6. import java.awt.image.WritableRaster;
  7. import java.util.ArrayList;
  8.  
  9. /**
  10.  * @author Florian Thiel
  11.  *
  12.  *         Superklasse die die Operation z.b. Zugriff auf die Pixel �bernimmt.
  13.  *         Die Subklassen sind jeweils eine Spezialisierung dieser Klasse.
  14.  * */
  15. public abstract class ImageProcessing {
  16.     protected int[] pixelOrg;
  17.     protected int[] pixelNew;
  18.  
  19.     // Breite und H�he vom Bild
  20.     protected int height;
  21.     protected int width;
  22.     protected int type;
  23.     protected int maxValue;
  24.     // nimmt die Parameter f�r den ausgew�hlten Algorithmus entgegen
  25.     protected ArrayList<FilterValue> para;
  26.  
  27.     /**
  28.      * Dies sind drei Korrekturfaktoren die bei der Konvertierung ben�tigt
  29.      * werden, da das menschliche Auge gr�ne Farbanteile intensiver wahrnimmt
  30.      * als rote und rote Farbanteile st�rker als blaue.
  31.      */
  32.     protected float r = 0.229f, g = 0.587f, b = 0.114f;
  33.  
  34.     public ImageProcessing(BufferedImage input, ArrayList<FilterValue> para) {
  35.         width = input.getWidth();
  36.         height = input.getHeight();
  37.         type = input.getType();
  38.  
  39.         /**
  40.          * Pr�fe ob das IndexColorModel verwendet wird( z.B. gif-Bilder), dann
  41.          * muss der Farb/Grauwert aus einer Tabelle ermittelt werden.
  42.          * */
  43.         if (input.getColorModel() instanceof IndexColorModel) {
  44.             IndexColorModel model = (IndexColorModel) input.getColorModel();
  45.             pixelOrg = new int[input.getWidth() * input.getHeight()];
  46.  
  47.             for (int y = 0; y < height; y++) {
  48.                 for (int x = 0; x < width; x++) {
  49.                     int key = input.getRaster().getSample(x, y, 0);
  50.  
  51.                     pixelOrg[y * width + x] = new Color(model.getRed(key),
  52.                             model.getGreen(key), model.getBlue(key),
  53.                             model.getAlpha(key)).getRGB();
  54.                 }
  55.             }
  56.  
  57.             type = BufferedImage.TYPE_INT_RGB;
  58.         } else {
  59.             /**
  60.              * Bei der Verwendung des DirektColorModels, kann der Farbwert
  61.              * direkt ausgelesen werden.
  62.              * */
  63.  
  64.             if (type == BufferedImage.TYPE_INT_RGB) {
  65.                 pixelOrg = input.getRGB(0, 0, input.getWidth(),
  66.                         input.getHeight(), null, 0, input.getWidth());
  67.             } else {
  68.                 pixelOrg = new int[input.getWidth() * input.getHeight()];
  69.  
  70.                 for (int y = 0; y < height; y++) {
  71.                     for (int x = 0; x < width; x++) {
  72.                         pixelOrg[y * width + x] = input.getRaster().getSample(
  73.                                 x, y, 0);
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.         maxValue = 255;
  79.  
  80.         pixelNew = new int[pixelOrg.length];
  81.         System.arraycopy(pixelOrg, 0, pixelNew, 0, pixelOrg.length);
  82.  
  83.         this.para = para;
  84.     }
  85.  
  86.     public ImageProcessing(int width, int height, int type, int maxValue,
  87.             int[] input) {
  88.         this.width = width;
  89.         this.height = height;
  90.         this.type = type;
  91.         this.maxValue = maxValue;
  92.         pixelOrg = input;
  93.         pixelNew = new int[input.length];
  94.         System.arraycopy(pixelOrg, 0, pixelNew, 0, pixelOrg.length);
  95.     }
  96.  
  97.     // Diese Methode f�hrt die eigentliche Bildberechnung durch.
  98.     public abstract void processing();
  99.  
  100.     protected void convertToGray() {
  101.         if (type != BufferedImage.TYPE_BYTE_GRAY) {
  102.             type = BufferedImage.TYPE_BYTE_GRAY;
  103.  
  104.             for (int i = 0; i < pixelOrg.length; i++) {
  105.                 Color c = new Color(pixelOrg[i]);
  106.                 pixelOrg[i] = (int) (r * c.getRed() + g * c.getGreen() + b
  107.                         * c.getBlue());
  108.                 c = new Color(pixelNew[i]);
  109.                 pixelNew[i] = (int) (r * c.getRed() + g * c.getGreen() + b
  110.                         * c.getBlue());
  111.             }
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * Konvertiert das
  117.      * */
  118.     protected void convertToBin(int threshold) {
  119.         if (type == BufferedImage.TYPE_BYTE_GRAY) {
  120.             for (int i = 0; i < pixelOrg.length; i++) {
  121.                 if (threshold > pixelOrg[i])
  122.                     pixelOrg[i] = 0;
  123.                 else
  124.                     pixelOrg[i] = maxValue;
  125.             }
  126.         } else {
  127.             for (int i = 0; i < pixelOrg.length; i++) {
  128.                 Color c = new Color(pixelOrg[i]);
  129.  
  130.                 if (threshold > (r * c.getRed() + g * c.getGreen() + b
  131.                         * c.getBlue()))
  132.                     pixelOrg[i] = 0;
  133.                 else
  134.                     pixelOrg[i] = maxValue;
  135.             }
  136.         }
  137.         type = BufferedImage.TYPE_BYTE_BINARY;
  138.     }
  139.  
  140.     protected void convertToColor() {
  141.         if (type != BufferedImage.TYPE_INT_RGB) {
  142.             for (int i = 0; i < pixelNew.length; i++) {
  143.                 int tmp = pixelOrg[i];
  144.                 pixelOrg[i] = new Color(tmp, tmp, tmp).getRGB();
  145.  
  146.                 tmp = pixelNew[i];
  147.                 pixelNew[i] = new Color(tmp, tmp, tmp).getRGB();
  148.             }
  149.  
  150.             type = BufferedImage.TYPE_INT_RGB;
  151.         }
  152.     }
  153.  
  154.     protected Object getParameter(String s) {
  155.         if (para != null)
  156.             for (FilterValue f : para) {
  157.                 if (f.getName().equals(s))
  158.                     return f.getValue();
  159.             }
  160.  
  161.         return null;
  162.     }
  163.  
  164.     /**
  165.      * Erstellt ein neues BufferedImage und f�llt es mit den Array pixelNew
  166.      * */
  167.     public BufferedImage getProcessedImage() {
  168.         BufferedImage output = new BufferedImage(width, height, type);
  169.  
  170.         if (type == BufferedImage.TYPE_BYTE_BINARY
  171.                 || type == BufferedImage.TYPE_BYTE_GRAY) {
  172.             WritableRaster raster = output.getRaster();
  173.  
  174.             for (int y = 0; y < height; y++) {
  175.                 for (int x = 0; x < width; x++) {
  176.                     raster.setPixel(x, y, new int[] { pixelNew[y * width + x] });
  177.                 }
  178.             }
  179.  
  180.             output.setData(raster);
  181.         } else {
  182.             for (int y = 0; y < height; y++) {
  183.                 for (int x = 0; x < width; x++) {
  184.                     output.setRGB(x, y, pixelNew[y * width + x]);
  185.                 }
  186.             }
  187.         }
  188.  
  189.         return output;
  190.     }
  191.  
  192. }
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement