个性化阅读
专注于IT技术分析

Java中的图像处理S11(图像的更改方向)

在本文中, 我们将通过使用OpenCV库的CORE.flip()方法, 使用opencv更改任何输入图像的方向。

主要思想是将输入的缓冲图像对象转换为mat对象, 然后创建一个新的mat对象, 在其中将原始mat对象的值放置在方向修改后。

为了获得上述结果, 我们将需要一些OpenCV方法:

  • getRaster() –该方法返回可写栅格, 该栅格又用于从输入图像获取原始数据。
  • put(int row, int column, byte [] data)/get(int行, int列, byte []数据) –用于将原始数据读/写到mat对象。
  • flip(mat mat1, mat mat2, int flip_value)– mat1和mat2对应于输入和输出mat对象, flip_value决定方向类型。flip_value可以为0(沿x轴翻转), 1(沿y轴翻转), -1(沿两个轴翻转)。 。
//Java program to illustrate orientation modification of image
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
  
public class OrientingImage
{
   public static void main( String[] args ) throws IOException
   {
     //loads methods of the opencv library
     System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
  
     //input buffered image object
     File input = new File( "E:\\test.jpg" );
     BufferedImage image = ImageIO.read(input);
  
     //converting buffered image object to mat object
     byte [] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
     Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
     mat.put( 0 , 0 , data);
  
     //creating a new mat object and putting the modified input mat object by using flip()
     Mat newMat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
     Core.flip(mat, newMat, - 1 );  //flipping the image about both axis
  
     //converting the newly created mat object to buffered image object
     byte [] newData = new byte [newMat.rows()*newMat.cols()*( int )(newMat.elemSize())];
     newMat.get( 0 , 0 , newData);
     BufferedImage image1 = new BufferedImage(newMat.cols(), newMat.rows(), 5 );
     image1.getRaster().setDataElements( 0 , 0 , newMat.cols(), newMat.rows(), newData);
  
     File ouptut = new File( "E:\\result.jpg" );
     ImageIO.write(image1, "jpg" , ouptut);
   }
}

输出如下:

test.jpg


result.jpg

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

赞(0)
未经允许不得转载:srcmini » Java中的图像处理S11(图像的更改方向)

评论 抢沙发

评论前必须登录!