Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-19 of 19 total

Java - EdgeW / EdgeH

   1  
   2  public BufferedImage EdgeW(BufferedImage bi)
   3  	{
   4  		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
   5  		
   6  		Kernel kernel = new Kernel(3, 3, new float[] {
   7  														-1f, 0f, 1f, 
   8  														-2f, 0f, 2f, 
   9  														-1f, 0f, 1f
  10  													 });
  11  		
  12  		ConvolveOp op = new ConvolveOp(kernel);
  13  		op.filter(bi, buff);
  14  		
  15  		return buff;
  16  	}


   1  
   2  	public BufferedImage EdgeH(BufferedImage bi)
   3  	{
   4  		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
   5  		
   6  		Kernel kernel = new Kernel(3, 3, new float[] {
   7  														-1f, -2f, -1f, 
   8  														 0f,  0f,  0f, 
   9  														 1f,  2f,   1f
  10  													 });
  11  		
  12  		ConvolveOp op = new ConvolveOp(kernel);
  13  		op.filter(bi, buff);
  14  		
  15  		return buff;
  16  	}

Java - Brightness

   1  
   2  public BufferedImage brightness(BufferedImage bi, float value)
   3  	{
   4  		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
   5  		
   6  		Kernel kernel = new Kernel(1, 1, new float[] {value});
   7  		
   8  		ConvolveOp op = new ConvolveOp(kernel);
   9  		op.filter(bi, buff);
  10  		
  11  		return buff;
  12  	}

Matlab - DoubleFlip Image

// Flip Width & Height

   1  
   2  function flipIMG=DoubleFlip(img)
   3  
   4      tic
   5  
   6      flipIMG = img([1:end/2, end/2:-1:1], [1:end/2, end/2:-1:1], :);
   7  
   8      toc

Java - JMF Simple Filter

   1  
   2  import java.awt.Dimension;
   3  
   4  import javax.media.Buffer;
   5  import javax.media.Effect;
   6  import javax.media.Format;
   7  import javax.media.ResourceUnavailableException;
   8  import javax.media.format.RGBFormat;
   9  
  10  public class SimpleFilter implements Effect
  11  {
  12  	protected Format inputFormat = null;
  13  	protected Format outputFormat = null;
  14  	
  15  	protected Format[] inputFormats = null;
  16  	protected Format[] outputFormats = null;
  17  	
  18  	public AngelMotionCodec()
  19  	{
  20  		inputFormats = new Format[]{ new RGBFormat(null, Format.NOT_SPECIFIED, Format.byteArray, Format.NOT_SPECIFIED, 24, 3, 2, 1, 3, Format.NOT_SPECIFIED, Format.TRUE, Format.NOT_SPECIFIED) };
  21  		outputFormats = new Format[]{ new RGBFormat(null, Format.NOT_SPECIFIED, Format.byteArray, Format.NOT_SPECIFIED, 24, 3, 2, 1, 3, Format.NOT_SPECIFIED, Format.TRUE, Format.NOT_SPECIFIED) };
  22  	}
  23  	
  24  	/****** Codec ******/
  25  	public Format[] getSupportedInputFormats()
  26  	{
  27  		return inputFormats;
  28  	}
  29  
  30  	public Format[] getSupportedOutputFormats(Format input)
  31  	{
  32  		if(input != null)
  33  		{
  34  			if(matches(input, inputFormats) != null)
  35  				return new Format[]{ outputFormats[0].intersects(input) };
  36  			else
  37  				return new Format[0];
  38  		}
  39  		
  40  		return outputFormats;
  41  	}
  42  
  43  	public int process(Buffer input, Buffer output)
  44  	{
  45  		// Swap tra input & output
  46  		Object tmp = input.getData();
  47  		
  48  		input.setData(output.getData());
  49  		output.setData(tmp);
  50  		
  51  		return BUFFER_PROCESSED_OK;
  52  	}
  53  
  54  	public Format setInputFormat(Format input)
  55  	{
  56  		inputFormat = input;
  57  		
  58  		return input;
  59  	}
  60  
  61  	public Format setOutputFormat(Format output)
  62  	{
  63  		if(output != null || matches(output, outputFormats) != null)
  64  		{
  65  			RGBFormat inRGB = (RGBFormat) output;
  66  			
  67  			Dimension size = inRGB.getSize();
  68  			int maxDataLength = inRGB.getMaxDataLength();
  69  			int lineStride = inRGB.getLineStride();
  70  			int flipped = inRGB.getFlipped();
  71  			
  72  			if(size == null)
  73  				return null;
  74  			
  75  			if(maxDataLength < size.width*size.height*3)
  76  				maxDataLength = size.width*size.height*3;
  77  			
  78  			if(lineStride < size.width*3)
  79  				lineStride = size.width*3;
  80  			
  81  			if(flipped != Format.FALSE)
  82  				flipped = Format.FALSE;
  83  			
  84  			outputFormat = outputFormats[0].intersects(new RGBFormat(size, maxDataLength, inRGB.getDataType(), inRGB.getFrameRate(), inRGB.getBitsPerPixel(), inRGB.getRedMask(), inRGB.getGreenMask(), inRGB.getBlueMask(), inRGB.getPixelStride(), lineStride, flipped, inRGB.getEndian()));
  85  			
  86  			return outputFormat;
  87  		}
  88  		
  89  		return null;
  90  	}
  91  	/****** Codec ******/
  92  
  93  	/****** PlugIn ******/
  94  	public void close()
  95  	{
  96  
  97  	}
  98  
  99  	public String getName()
 100  	{
 101  		return "Simple-Filter";
 102  	}
 103  
 104  	public void open() throws ResourceUnavailableException
 105  	{
 106  
 107  	}
 108  
 109  	public void reset()
 110  	{
 111  
 112  	}
 113  	/****** PlugIn ******/
 114  
 115  	/****** Controls ******/
 116  	public Object getControl(String controlType)
 117  	{
 118  		return null;
 119  	}
 120  
 121  	public Object[] getControls()
 122  	{
 123  		return null;
 124  	}
 125  	/****** Controls ******/
 126  	
 127  	/****** Utility ******/
 128  	private Format matches(Format in, Format[] out)
 129  	{
 130  		if(in != null && out != null)
 131  		{
 132  			for(int i=0, cnt=out.length; i<cnt; i++)
 133  			{
 134  				if(in.matches(out[i]))
 135  					return out[i];
 136  			}
 137  		}
 138  		
 139  		return null;
 140  	}
 141  	/****** Utility ******/
 142  }

Java - ImageFilter Simple

// Filtro Embrossing
   1  
   2  	public BufferedImage Embrossing(BufferedImage bi)
   3  	{
   4  		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
   5  		
   6  		Kernel kernel = new Kernel(3, 3, new float[] {
   7  														-2f, 0f, 0f, 
   8  														 0f, 1f, 0f, 
   9  														 0f, 0f, 2f
  10  													 });
  11  		
  12  		ConvolveOp op = new ConvolveOp(kernel);
  13  		op.filter(bi, buff);
  14  		
  15  		return buff;
  16  	}


// Filtro Blurring
   1  
   2  	public BufferedImage Blurring(BufferedImage bi)
   3  	{
   4  		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
   5  		
   6  		Kernel kernel = new Kernel(3, 3, new float[] {
   7  														1f/9f, 1f/9f, 1f/9f, 
   8  														1f/9f, 1f/9f, 1f/9f, 
   9  														1f/9f, 1f/9f, 1f/9f
  10  													 });
  11  		
  12  		ConvolveOp op = new ConvolveOp(kernel);
  13  		op.filter(bi, buff);
  14  		
  15  		return buff;
  16  	}


// Filtro Sharpening
   1  
   2  	public BufferedImage Sharpening(BufferedImage bi)
   3  	{
   4  		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
   5  		
   6  		Kernel kernel = new Kernel(3, 3, new float[] {
   7  														-1f, -1f, -1f, 
   8  														-1f,  9f, -1f, 
   9  														-1f, -1f, -1f
  10  													 });
  11  		
  12  		ConvolveOp op = new ConvolveOp(kernel);
  13  		op.filter(bi, buff);
  14  		
  15  		return buff;
  16  	}

image filtering

// smooth image

   1  
   2  #include "highgui.h"
   3  #include "cv.h"
   4  
   5  void main() {
   6  
   7  	IplImage* src = cvLoadImage("lena.jpg", 1);
   8  	IplImage* dst = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
   9  
  10  	cvSmooth(src, dst, CV_BLUR, 3, 3);
  11  
  12  	cvNamedWindow("src", 1);
  13  	cvShowImage("src", src);
  14  
  15  	cvNamedWindow("dst", 1);
  16  	cvShowImage("dst", dst);
  17  
  18  	cvReleaseImage(&src);
  19  	cvReleaseImage(&dst);
  20  
  21  	cvWaitKey(0);
  22  
  23  }

filter table

A very very simple manner to improve all your tables with a search/filter function

Test it here: http://leparlement.org/filterTable

   1  
   2  /*
   3   * A very simple script to filter a table according to search criteria
   4   *
   5   * http://leparlement.org/filterTable
   6   * See also http://www.vonloesch.de/node/23
   7   */
   8  function filterTable(term, table) {
   9    dehighlight(table);
  10    var terms = term.value.toLowerCase().split(" ");
  11  
  12    for (var r = 1; r < table.rows.length; r++) {
  13      var display = '';
  14      for (var i = 0; i < terms.length; i++) {
  15        if (table.rows[r].innerHTML.replace(/<[^>]+>/g, "").toLowerCase()
  16          .indexOf(terms[i]) < 0) {
  17          display = 'none';
  18        } else {
  19          if (terms[i].length) highlight(terms[i], table.rows[r]);
  20        }
  21        table.rows[r].style.display = display;
  22      }
  23    }
  24  }
  25  
  26  
  27  /*
  28   * Transform back each
  29   * <span>preText <span class="highlighted">term</span> postText</span>
  30   * into its original
  31   * preText term postText
  32   */
  33  function dehighlight(container) {
  34    for (var i = 0; i < container.childNodes.length; i++) {
  35      var node = container.childNodes[i];
  36  
  37      if (node.attributes && node.attributes['class']
  38        && node.attributes['class'].value == 'highlighted') {
  39        node.parentNode.parentNode.replaceChild(
  40            document.createTextNode(
  41              node.parentNode.innerHTML.replace(/<[^>]+>/g, "")),
  42            node.parentNode);
  43        // Stop here and process next parent
  44        return;
  45      } else if (node.nodeType != 3) {
  46        // Keep going onto other elements
  47        dehighlight(node);
  48      }
  49    }
  50  }
  51  
  52  /*
  53   * Create a
  54   * <span>preText <span class="highlighted">term</span> postText</span>
  55   * around each search term
  56   */
  57  function highlight(term, container) {
  58    for (var i = 0; i < container.childNodes.length; i++) {
  59      var node = container.childNodes[i];
  60  
  61      if (node.nodeType == 3) {
  62        // Text node
  63        var data = node.data;
  64        var data_low = data.toLowerCase();
  65        if (data_low.indexOf(term) >= 0) {
  66          //term found!
  67          var new_node = document.createElement('span');
  68  
  69          node.parentNode.replaceChild(new_node, node);
  70  
  71          var result;
  72          while ((result = data_low.indexOf(term)) != -1) {
  73            new_node.appendChild(document.createTextNode(
  74                  data.substr(0, result)));
  75            new_node.appendChild(create_node(
  76                  document.createTextNode(data.substr(
  77                      result, term.length))));
  78            data = data.substr(result + term.length);
  79            data_low = data_low.substr(result + term.length);
  80          }
  81          new_node.appendChild(document.createTextNode(data));
  82        }
  83      } else {
  84        // Keep going onto other elements
  85        highlight(term, node);
  86      }
  87    }
  88  }
  89  
  90  function create_node(child) {
  91    var node = document.createElement('span');
  92    node.setAttribute('class', 'highlighted');
  93    node.attributes['class'].value = 'highlighted';
  94    node.appendChild(child);
  95    return node;
  96  }
  97  
  98  /*
  99   * Here is the code used to set a filter on all filterable elements, usually I
 100   * use the behaviour.js library which does that just fine
 101   */
 102  tables = document.getElementsByTagName('table');
 103  for (var t = 0; t < tables.length; t++) {
 104    element = tables[t];
 105  
 106    if (element.attributes['class']
 107      && element.attributes['class'].value == 'filterable') {
 108  
 109      /* Here is dynamically created a form */
 110      var form = document.createElement('form');
 111      form.setAttribute('class', 'filter');
 112      // For ie...
 113      form.attributes['class'].value = 'filter';
 114      var input = document.createElement('input');
 115      input.onkeyup = function() {
 116        filterTable(input, element);
 117      }
 118      form.appendChild(input);
 119      element.parentNode.insertBefore(form, element);
 120    }
 121  }

PHP filename bad character filter

// Function to filter out bad characters in a given filename

   1  
   2  function replace_bad_filename_chars($filename) {
   3    $filtered_filename = "";
   4  
   5    $patterns = array(
   6      "/\s/", # Whitespace
   7      "/\&/", # Ampersand
   8      "/\+/"  # Plus
   9    );
  10    $replacements = array(
  11      "_",   # Whitespace
  12      "and", # Ampersand
  13      "plus" # Plus
  14    );
  15    
  16    $filename = preg_replace($patterns,$replacements,$filename);
  17    for ($i=0;$i<strlen($filename);$i++) {
  18      $current_char = substr($filename,$i,1);
  19      if (ctype_alnum($current_char) == TRUE || $current_char == "_" || $current_char == ".") {
  20        $filtered_filename .= $current_char;
  21      }
  22    }     
  23          
  24    return $filtered_filename;
  25  }

Bayesian classifier and filtering

(Also copied and modified from seb's code here)
Reverend is a free Bayesian module for Python. Get it here
Here's how we train the classifier
   1  
   2  from reverend.thomas import Bayes
   3  g = Bayes()    # guesser
   4  g.train('french','La souris est rentrée dans son trou.')
   5  g.train('english','my tailor is rich.')
   6  g.train('french','Je ne sais pas si je viendrai demain.')
   7  g.train('english','I do not plan to update my website soon.')

Then use it to guess the language
   1  
   2  >>> print g.guess('Jumping out of cliffs it not a good idea.')
   3  [('english', 0.99990000000000001), ('french', 9.9999999999988987e-005)]
   4  # 99.99% English
   5  
   6  >>> print g.guess('Demain il fera très probablement chaud.')
   7  [('french', 0.99990000000000001), ('english', 9.9999999999988987e-005)]
   8  # 99.99% French

You can train it with more languages. You can also train it
to classify the kind of text.
« Newer Snippets
Older Snippets »
Showing 11-19 of 19 total