Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Operatore Morfologico Closing

    Salve, per via di un esame che devo dare all'università, Elaborazione delle Immagini per la precisione, mi trovo a dover combattere con questo operatore morfologico. Non è importante sapere di cosa si tratti, quello che serve a me è scrivere un algoritmo, compilabile ed eseguibile, in java, che lo esegua. Cercando in rete ho trovatoun ottimo manuale sulle applicazioni Java delle Elaborazioni delle Immagini, che includeva anche dei sorgenti che mi servono per realizzarlo.

    A me serve il Closing, che non è altro che una Dilate seguita da una Erosion, quindi fare un Closing, o una Dilate seguita da una Erosion è la stessa identica cosa. I sorgenti che ho trovato sono appunto di Dilate ed Erosion, e sono strasicuro che siano scritti bene perchè fanno parte di CD allegato ad un manuale ufficialmente in vendita, ma quì iniziano i problemi:

    1) non riesco inspiegabilmente a compilarli.
    2) sono sorgenti privi di main, che quindi andrebbe scritto a parte, e che non sono assolutamente in grado di scrivere.

    Sono in tutto quattro file: StandardGreyOp.java, BinaryMorphologicalOp.java, BinaryDilateOp.java, BinaryErodeOp.java.

    BinaryDilateOp e BinaryErodeOp estendono BinaryMorphologicalOp, che a sua volta estende StandardGreyOp. Ve li scrivo:

    StandardGreyOp
    codice:
    package com.pearsoneduc.ip.op;
    
    
    
    import java.awt.RenderingHints;
    import java.awt.geom.*;
    import java.awt.image.*;
    
    
    
    /**
     * Implements a BufferedImageOp for standard operations on 8-bit
     * greyscale images.  Subclasses should override the
     * {@link #filter(BufferedImage, RenderingHints) filter()} method
     * to carry out processing.
     *
     * @author Nick Efford
     * @version 1.1 [1999/07/23]
     */
    
    public class StandardGreyOp implements BufferedImageOp {
    
    
      /**
       * Performs an operation on an image.  Here, the operation is a
       * simple copy of source to destination.  Subclasses must override
       * this method to produce more interesting behaviour.
       * @param src source image
       * @param dest destination image, or null
       * @return destination image.
       */
    
      public BufferedImage filter(BufferedImage src, BufferedImage dest) {
        checkImage(src);
        if (dest == null)
          dest = createCompatibleDestImage(src, null);
        WritableRaster raster = dest.getRaster();
        src.copyData(raster);
        return dest;
      }
    
    
      /**
       * Creates a zeroed destination image with the same dimensions
       * and number of bands as the source image.
       * @param src source image
       * @param destModel ColorModel of the destination image
       *  (if null, ColorModel of the source image will be used)
       */
    
      public BufferedImage createCompatibleDestImage(BufferedImage src,
       ColorModel destModel) {
        if (destModel == null)
          destModel = src.getColorModel();
        int width = src.getWidth();
        int height = src.getHeight();
        BufferedImage image = new BufferedImage(destModel,
         destModel.createCompatibleWritableRaster(width, height),
         destModel.isAlphaPremultiplied(), null);
        return image;
      }
    
    
      /**
       * Determines bounding box of the destination image.  Here, it is
       * assumed that this is identical to that of the source image.
       * @param src source image
       * @return bounding box of the destination image.
       */
    
      public Rectangle2D getBounds2D(BufferedImage src) {
        return src.getRaster().getBounds();
      }
    
    
      /**
       * Given a point in the source image, determines the corresponding
       * point in the destination image.  Here, it is assumed that this
       * will be the same as the point in the source image.
       * @param srcPoint a point from the source image
       * @param destPoint a point in the destination image, or null
       * @return point in the destination image.
       */
    
      public Point2D getPoint2D(Point2D srcPoint, Point2D destPoint) {
        if (destPoint == null)
          destPoint = new Point2D.Float();
        destPoint.setLocation(srcPoint.getX(), srcPoint.getY());
        return destPoint;
      }
    
    
      /**
       * @return rendering hints for this operation.
       */
    
      public RenderingHints getRenderingHints() {
        return null;
      }
    
    
      /**
       * Tests that source image is suitable for processing.
       * @param src source image
       * @exception ImagingOpException if the source image is not an
       *  8-bit greyscale image.
       */
    
      public void checkImage(BufferedImage src) {
        if (src.getType() != BufferedImage.TYPE_BYTE_GRAY)
          throw new ImagingOpException("operation requires an 8-bit grey image");
      }
    
    
    }
    BinaryMorphologicalOp
    codice:
    package com.pearsoneduc.ip.op;
    
    
    
    import java.awt.image.*;
    
    
    
    /**
     * Adapts StandardGreyOp for binary morphological operations.
     * The method checkImage() is overridden to check that the source
     * image is either a true binary image or a greyscale image with
     * no more than two grey levels.
     *
     * @author Nick Efford
     * @version 1.0 [1999/08/30]
     */
    
    public class BinaryMorphologicalOp extends StandardGreyOp {
    
    
      /////////////////////////// INSTANCE VARIABLES ///////////////////////////
    
    
      /** Non-zero value assigned to pixels in the output image. */
      protected int nonZeroValue;
    
    
      //////////////////////////////// METHODS /////////////////////////////////
    
    
      /**
       * Checks that the image to be processed is of the correct type -
       * either a true binary image or a two-level 8-bit greyscale image
       * with 0 as one of the values.
       * @param image image to be processed
       * @exception ImagingOpException if the image is not of the correct type.
       */
    
      public void checkImage(BufferedImage image) {
        if (image.getType() == BufferedImage.TYPE_BYTE_BINARY)
          nonZeroValue = 1;
        else if (image.getType() == BufferedImage.TYPE_BYTE_GRAY) {
          nonZeroValue = 255;
          Raster raster = image.getRaster();
          int[] hist = new int[256];
          for (int y = 0; y < image.getHeight(); ++y)
            for (int x = 0; x < image.getWidth(); ++x)
              ++hist[raster.getSample(x, y, 0)];
          int n = 0;
          for (int i = 1; i < 256; ++i)
            if (hist[i] > 0) {
              ++n;
              if (n > 1)
                throw new ImagingOpException(
                 "image must be binary, or grey with <= one non-zero value");
            }
        }
        else
          throw new ImagingOpException("invalid image type");
      }
    
    
    }
    BinaryDilateOp
    codice:
    package com.pearsoneduc.ip.op;
    
    
    
    import java.awt.Point;
    import java.awt.image.*;
    
    
    
    /**
     * Performs the morphological operation of dilation on a binary image.
     * 'Binary image' is taken here to mean a greyscale image with just
     * two values, one of which is zero.
     *
     * @author Nick Efford
     * @version 1.0 [1999/08/30]
     */
    
    public class BinaryDilateOp extends BinaryMorphologicalOp {
    
    
      /////////////////////////// INSTANCE VARIABLES ///////////////////////////
    
    
      /** Structuring element used to perform the dilation. */
      private BinaryStructElement structElement;
    
    
      //////////////////////////////// METHODS /////////////////////////////////
    
    
      /**
       * Creates a BinaryDilateOp that uses the specified structuring element.
       * @param element structuring element
       */
    
      public BinaryDilateOp(BinaryStructElement element) {
        structElement = element;
      }
    
    
      /**
       * Performs morphological dilation of a binary image.
       * @param src source image
       * @param dest destination image, or null
       * @return dilated image.
       */
    
      public BufferedImage filter(BufferedImage src, BufferedImage dest) {
    
        checkImage(src);
        if (dest == null)
          dest = createCompatibleDestImage(src, null);
    
        int w = src.getWidth();
        int h = src.getHeight();
        Raster srcRaster = src.getRaster();
        WritableRaster destRaster = dest.getRaster();
    
        // Determine range of pixels for which operation can be performed
    
        Point origin = structElement.getOrigin(null);
        int xmin = Math.max(origin.x, 0);
        int ymin = Math.max(origin.y, 0);
        int xmax = origin.x + w - structElement.getWidth();
        int ymax = origin.y + h - structElement.getHeight();
        xmax = Math.min(w-1, xmax);
        ymax = Math.min(h-1, ymax);
    
        // Intersect structuring element with source image
    
        for (int y = ymin; y <= ymax; ++y)
          for (int x = xmin; x <= xmax; ++x)
            if (structElement.hits(srcRaster, x, y))
              destRaster.setSample(x, y, 0, nonZeroValue);
    
        return dest;
    
      }
    
    
    }
    BinaryErodeOp
    codice:
    package com.pearsoneduc.ip.op;
    
    
    
    import java.awt.Point;
    import java.awt.image.*;
    
    
    
    /**
     * Performs the morphological operation of erosion on a binary image.
     * 'Binary image' is taken here to mean a greyscale image with just
     * two values, one of which is zero.
     *
     * @author Nick Efford
     * @version 1.0 [1999/08/30]
     */
    
    public class BinaryErodeOp extends BinaryMorphologicalOp {
    
    
      /////////////////////////// INSTANCE VARIABLES ///////////////////////////
    
    
      /** Structuring element used to perform the erosion. */
      private BinaryStructElement structElement;
    
    
      //////////////////////////////// METHODS /////////////////////////////////
    
    
      /**
       * Creates a BinaryErodeOp that uses the specified structuring element.
       * @param element structuring element
       */
    
      public BinaryErodeOp(BinaryStructElement element) {
        structElement = element;
      }
    
    
      /**
       * Performs morphological erosion of a binary image.
       * @param src source image
       * @param dest destination image, or null
       * @return eroded image.
       */
    
      public BufferedImage filter(BufferedImage src, BufferedImage dest) {
    
        checkImage(src);
        if (dest == null)
          dest = createCompatibleDestImage(src, null);
    
        int w = src.getWidth();
        int h = src.getHeight();
        Raster srcRaster = src.getRaster();
        WritableRaster destRaster = dest.getRaster();
    
        // Determine range of pixels for which operation can be performed
    
        Point origin = structElement.getOrigin(null);
        int xmin = Math.max(origin.x, 0);
        int ymin = Math.max(origin.y, 0);
        int xmax = origin.x + w - structElement.getWidth();
        int ymax = origin.y + h - structElement.getHeight();
        xmax = Math.min(w-1, xmax);
        ymax = Math.min(h-1, ymax);
    
        // Fit structuring element into source image
    
        for (int y = ymin; y <= ymax; ++y)
          for (int x = xmin; x <= xmax; ++x)
            if (structElement.fits(srcRaster, x, y))
              destRaster.setSample(x, y, 0, nonZeroValue);
    
        return dest;
    
      }
    
    
    }
    Come si può notare è specificato il loro package, che io rimuoverei per prima cosa, cambiando ogni protected in public all'interno delle classi (tanto non dovrebbe creare problemi); toglierei inoltre tutti quegli inutili commenti. Qualcuno potrebbe aiutarmi a fare una classe di main che esegua prima il Dilate e poi l'Erode, e a capire perchè così non riesco a compilarli? In alternativa, qualche anima pia potrebbe scrivermela direttamente, dato che sono sommerso di studio per il prossimo esame di Lunedì mattina? Grazie di cuore, aspetto risposte!

  2. #2

    Re: Operatore Morfologico Closing

    Originariamente inviato da Gil1688
    In alternativa, qualche anima pia potrebbe scrivermela direttamente, dato che sono sommerso di studio per il prossimo esame di Lunedì mattina? Grazie di cuore, aspetto risposte!
    max

    Silence is better than bullshit.
    @mmarcon
    jHERE, Maps made easy

  3. #3
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328

    Moderazione

    Come espressamente indicato nel regolamento interno, non sono accettate richieste del tipo "qualcuno mi scrive il codice per...".

    Essendo un forum tecnico, vengono poste domande tecniche, su specifici problemi tecnici.

    Devo chiudere la discussione. Se dovessi riscontrare problemi specifici durante lo sviluppo della classe principale, in futuro, e dovessi fare riferimento a queste classi, puoi postare un link a questa discussione.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.