Giter VIP home page Giter VIP logo

imagingkit's Introduction

ImagingKit

Build Status Coverage Status Maven Central

A Java library for imaging tasks that integrates well with the commonly used java.awt.image environment (especially well with TYPE_INT BufferedImages). Its goal is to make image processing more convenient and to ease performance optimization. The library is intended for images using integer typed values like 24bit RGB or 32bit ARGB.

So far the ImagingKit-Core and ImagingKit-Fourier artifacts of the library are available through the maven central repository:

<dependency>
  <groupId>com.github.hageldave.imagingkit</groupId>
  <artifactId>imagingkit-core</artifactId>
  <version>2.1</version>
</dependency>

<dependency>
  <groupId>com.github.hageldave.imagingkit</groupId>
  <artifactId>imagingkit-fourier</artifactId>
  <version>2.1</version>
</dependency>

As this library aims at convenience and ease of use, look at this code snippet for grayscale conversion as a teaser:

Img img = ImageLoader.loadImgFromURL("file:///home/pictures/rainbow.jpg");
for(Pixel px: img){
    int grey = (px.r() + px.g() + px.b()) / 3;
    px.setRGB(grey, grey, grey);
}

And now for the parallel processing part:

img.stream().parallel().forEach( px -> {
    int grey = px.getLuminance();
    px.setRGB(grey, grey, grey);
});

Code Examples


Convert an image to grayscale:

BufferedImage buffimg = ImageLoader.loadImage("myimage_colored.png", BufferedImage.TYPE_INT_ARGB);
Img img = Img.createRemoteImg(buffimg);
img.forEach(true, (pixel) -> {
    int gray = (pixel.r() + pixel.g() + pixel.b())/3;
    pixel.setARGB(pixel.a(), gray, gray, gray);
});
ImageSaver.saveImage(buffimg,"myimage_grayscale.png");

original lena image greyscale lena image


Draw into image (using java.awt.Graphics2D):

Img img = new Img(128, 128);
img.fill(0xff000000);
img.paint(g2d -> {
	g2d.setColor(Color.white);
	String helloWorld = "Hello World";
	int textWidth = g2d.getFontMetrics().stringWidth(helloWorld);
	g2d.drawString(helloWorld, img.getWidth()/2-textWidth/2, img.getHeight()/2);
	g2d.drawLine(0, img.getHeight()/2+4, img.getWidth(), img.getHeight()/2+4);
});
ImageFrame.display(img);
ImageSaver.saveImage(img.getRemoteBufferedImage(), "hello_world.png");

hello world image


Fancy polar color thing:

Img img = new Img(128, 128);
img.forEach(px -> {
	double x = px.getXnormalized()*2-1;
	double y = px.getYnormalized()*2-1;
	double len = Math.max(Math.abs(x),Math.abs(y));
	double angle = (Math.atan2(x,y)+Math.PI)*(180/Math.PI);
	
	double r = Math.max(0,1-Math.abs((angle-120)/120.0));
	double g = Math.max(0, 1-Math.abs((angle-240)/120.0));
	double b = Math.max(0, angle <= 120 ? 
			1-Math.abs((angle)/120.0):1-Math.abs((angle-360)/120.0));
	
	px.setRGB_fromDouble(r*(1-len), g*(1-len), b*(1-len));
});
ImageFrame.display(img);

fancy polor color image


Shifting hue (using color space transformation):

Img img = ImageLoader.loadImgFromURL("http://sipi.usc.edu/database/preview/misc/4.2.03.png");

img.forEach(ColorSpaceTransformation.RGB_2_HSV);
double hueShift = (360-90)/360.0;
img.forEach(pixel -> {
	// R channel corresponds to hue (modulo 1.0 for cyclic behaviour)
	pixel.setR_fromDouble((pixel.r_asDouble()+hueShift) % 1.0);
});
img.forEach(ColorSpaceTransformation.HSV_2_RGB);

ImageFrame.display(img);

baboon image hue shifted baboom image


Fourier Filtering

ColorImg img = new ColorImg(128,128,false);
img.paint(g2d->g2d.fillRect(64-16, 64-8, 32, 16));
ImageFrame.display(img.getRemoteBufferedImage()).setTitle("original");
ComplexImg fourier = Fourier.transform(img, ColorImg.channel_r);
fourier.shiftCornerToCenter();
ImageFrame.display(fourier.getPowerSpectrumImg().toImg()).setTitle("fft");
fourier.forEach(px->{
	int xfreq = px.getXFrequency();
	int yfreq = px.getYFrequency();
	double freqRadius = Math.sqrt(xfreq*xfreq+yfreq*yfreq);
	double gaussian = Math.exp(-freqRadius/(0.05*128));
	px.mult(gaussian, 0);
});
ImageFrame.display(fourier.getPowerSpectrumImg().toImg()).setTitle("filtered fft");
ColorImg restored = Fourier.inverseTransform(null, fourier, ColorImg.channel_r);
ColorImg redChannel = restored.getChannelImage(ColorImg.channel_r);
ImageFrame.display(redChannel.toImg()).setTitle("filterd original");

original fft filtered fft filtered original

imagingkit's People

Contributors

dependabot[bot] avatar hageldave avatar sadatanwar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sadatanwar lvcarx

imagingkit's Issues

Progress Measurement of Filters

think about a lightweight solution to get information about the progression of a filter (or filter chain produced by followedBy) while in the applyTo Method. Would be nice to have a listener object to be passed on applyTo(Img,x,y,w,h, progressListener) which is notified a couple of times about the progress of applying the filters to img

Pixel neighbour referencing

get value at position relative to current pixel like px.neighborValue(-1,0) for value of left neighbour pixel. Maybe also like px.neighbourValue(-1,0, Img.boundary_mode_mirror)

Remote Area Img

Ability to create an Img that uses a rectangular area from another Img as its buffer. The same memory is referenced though. Would be useful for using boundary stuff for parts of a bigger image.

Img remoteArea = img.getRemoteAreaImg(x,y, w,h);

copy and getRemoteBufferedImage will be more problematic then... lets see if this can be fixed somehow

setRGB method that preserves alpha

when using pixel.setRGB(r,g,b) this will overwrite existing alpha with 255 so it will be an opaque color.
It may be desired to preserve the previous alpha value and still change RGB. So far this is only possible by using pixel.setARGB(pixel.a(), r,g,b)

bilinear interpolation in Img class is broken

bilinear interpolation does only use 3 points of the 4 points.
This seems to be the case since v1.0

private static int interpolateColors(final int c00, final int c01, final int c10, final int c11, final float mx, final float my){
return Pixel.argb_fast/*_bounded*/(
	blend( blend(Pixel.a(c00), Pixel.a(c10), mx), blend(Pixel.a(c10), Pixel.a(c01), mx), my),
	blend( blend(Pixel.r(c00), Pixel.r(c10), mx), blend(Pixel.r(c10), Pixel.r(c01), mx), my),
	blend( blend(Pixel.g(c00), Pixel.g(c10), mx), blend(Pixel.g(c10), Pixel.g(c01), mx), my),
	blend( blend(Pixel.b(c00), Pixel.b(c10), mx), blend(Pixel.b(c10), Pixel.b(c01), mx), my) );
}

c11 is not used.

Area forEach

same as forEach but only on the scope of a specified rectangular area of the image.
like this:

img.forEach(x,y, w,h, (px)->{...});

Enhance ImageFrame

ImageFrame and ImagePanel are currently quite limited in functionality. The image is scaled to fit the frame and on mouse button press it is shown at original resolution.
Features for enhancement:

  • Save image to file (with file chooser dialog), accessible through right click pop up menu
  • Alternative zooming and panning behavior just like usual image viewers do it (scroll to zoom, mouse drag to pan), some UI elements to toggle 'fit to frame' and 'zoom to original resolution'

Cache x,y for Pixel

when getX or getY is called, cache the value for later calls so subsequent calls to these will be faster and not require modulo or division ops.

(needs benchmark as proof of concept)

Resize for arbitrary implementations of ImgBase

Resize for Img can be done using the g2d mapping, but this may not work for other implementations of ImgBase (maybe not even for ColorImg) especially when creategraphics is not supported.
The approach for downsampling should use mipmaps or image pyramids of some kind, and interpolate between pyramid layers for sizes in between halves to counteract aliasing (trilinear interpolation).
For upsampling, linear interpolation may suffice, but if not too complicated, 2nd or 3rd order spline surfaces can be used.

Filters to implement

Point Operations

  • Generic Affine Transformation g(x,y) = a * f(x,y) + b
  • Greyscale
  • Contrast
  • Generic Function Transformation g(x,y) = h( f(x,y) )
  • Gamma Correction
  • Histogram Equalisation
  • Thresholding (Black&White)

Convolution Filters

  • Generic Convolution by Kernel
  • Gaussian
  • Difference of Gaussians (DOG)
  • Sobel
  • Directional (Motion) Blur

Morphological Filters

  • Dilation
  • Erosion
  • Opening
  • Closing
  • Median

Mixed Methods

  • Canny Edge Detector

Variational Methods

  • Mumford-Shah Segmentation
    ... well this stuff is not light, lets see ...

ImageSaver has bug on saving TYPE_INT_ARGB to jpg

the handling for this type of image formats is already implemented but has a bug where the 'flattened' rgb image is created but not used for savig
see ImageSaver#saveImage(Image image, OutputStream os, String imgFileFormat)
lines 100 and 102

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.