1. 产品
  2.   图片
  3.   Java
  4.   ImgLib2
 
  

用于图像处理的开源 Java 库

一种通用的多维图像处理 API,可创建新图像、修改现有图像、处理稀疏数据、使用免费 Java API 复制现有图像。

开源 ImgLib2 库使软件开发人员能够在其 Java 应用程序中创建和操作图像。该库提供了一种界面驱动的设计,使用户能够在自己的应用程序中轻松使用数字和非数字数据类型。

ImgLib2 是一个通用的多维图像处理库,支持与图像处理相关的几个重要功能,例如创建新图像、修改现有图像、打开和读取现有图像、处理稀疏数据、复制现有图像、通用复制图像数据、绘制球体、插值支持等等。

该库非常用户友好,避免了不必要的复杂性,因此开发人员可以在开发项目时专注于算法的本质。该库的伟大之处在于它是独立于维度的,并且允许用户以可以应用于多维数据的方式来表达他们的代码。库的工作不仅限于图像,还有处理 RNA 序列的示例。

Previous Next

ImgLib2 入门

安装 ImgLib2 的最简单和推荐的方法是通过 GitHub。

通过 GitHub 安装 ImgLib2

go get -u github.com/imglib/imglib2.git 

通过 Java 创建新图像

开源 Java 库 ImgLib2 支持仅用几行 Java 代码从头开始创建新图像。使用 ImgLib2 库,您可以创建不同类型的图像,例如简单图像、3D 图像、ImgFactory 等。您还可以只用几行代码修改现有图像的图像。

通过JavaAPI创建新图像


        public Example1c()
        {
            // create the ImgFactory based on cells (cellsize = 5x5x5...x5) that will
            // instantiate the Img
            final ImgFactory< FloatType > imgFactory = new CellImgFactory<>( new FloatType(), 5 );
            // create an 3d-Img with dimensions 20x30x40 (here cellsize is 5x5x5)Ø
            final Img< FloatType > img1 = imgFactory.create( 20, 30, 40 );
            // create another image with the same size. Note that the input provides the size for the new image as it implements the Interval interface
            final Img< FloatType > img2 = imgFactory.create( img1 );
            // display both (but they are empty)
            ImageJFunctions.show( img1 );
            ImageJFunctions.show( img2 );
        }

通过 Java API 进行图像复制

ImgLib2 库包含使用 Java 命令进行图像复制的功能。您可以轻松地制作现有图像的副本。您可以使用光标来完成此任务。您还可以使用作为通用方法的复制方法,其好处是它适用于任何类型的类型。

通过Java次复制图像


	public DuplicateImage() throws ImgIOException
	{
		// open with SCIFIO as a FloatType
		Img< FloatType > img = IO.openImgs( "DrosophilaWing.tif", new FloatType() ).get( 0 );
		// copy the image, as it is a generic method it also works with FloatType
		Img< FloatType > duplicate = copyImage( img );
		// display the copy
		ImageJFunctions.show( duplicate );
	}
        // Generic, type-agnostic method to create an identical copy of an Img
	public < T extends Type< T > > Img< T > copyImage( final Img< T > input )
	{
		// create a new Image with the same properties
		Img< T > output = input.factory().create( input );
		// create a cursor for both images
		Cursor< T > cursorInput = input.cursor();
		Cursor< T > cursorOutput = output.cursor();
		// iterate over the input
		while ( cursorInput.hasNext())
		{
			// move both cursors forward by one pixel
			cursorInput.fwd();
			cursorOutput.fwd();
			// set the value of this pixel of the output image to the same as the input,
			// every Type supports T.set( T type )
			cursorOutput.get().set( cursorInput.get() );
		}
		// return the copy
		return output;
	}

部分通过 Java 查看图像

免费的 ImgLib2 库使软件开发人员能够通过几行 Java 代码在其应用程序中仅显示图像的某些部分。视图非常强大,您可以使用它们来显示图像的选定部分、显示旋转视图以及其他一些东西。视图可以是 RandomAccessible、Interval,因此可以设为 Iterable。

稀疏数据管理

免费的 ImgLib2 库使软件开发人员能够使用 Java 代码处理稀疏数据。该库提供了两种用于显示稀疏数据的插值方案。用户可以通过返回最近样本的值或距离采样位置的 k 个最近邻的插值的距离加权值来计算空间中每个位置的值。

在Java个应用程序中使用稀疏数据


        // Working with sparse data
	public SparseExample()
	{
		// the interval in which to create random points
		FinalInterval interval = new FinalInterval( new long[] { 375, 200 } );
		// create an IterableRealInterval
		IterableRealInterval< FloatType > realInterval = createRandomPoints( interval, 250 );
		// using nearest neighbor search we will be able to return a value an any position in space
		NearestNeighborSearch< FloatType > search =
			new NearestNeighborSearchOnKDTree<>(
				new KDTree<>( realInterval ) );
		// make it into RealRandomAccessible using nearest neighbor search
		RealRandomAccessible< FloatType > realRandomAccessible =
			Views.interpolate( search, new NearestNeighborSearchInterpolatorFactory< FloatType >() );
		// convert it into a RandomAccessible which can be displayed
		RandomAccessible< FloatType > randomAccessible = Views.raster( realRandomAccessible );
		// set the initial interval as area to view
		RandomAccessibleInterval< FloatType > view = Views.interval( randomAccessible, interval );
		// display the view
		ImageJFunctions.show( view );
		// compute a gauss on it
		Img< FloatType > convolved = new ArrayImgFactory<>( new FloatType() ).create( interval );
		Gauss.inFloat( new double[] { 3, 3 }, view, interval, convolved,
			new Point( view.numDimensions() ), convolved.factory() );
		// display the view
		ImageJFunctions.show( convolved );
	}
 中国人