Java OpenCV 图像处理41 图形图像 图片缩放
1 图片缩放
Java OpenCV 代码 OpenCV 提供的主要图像缩放函数,可以指定缩放比例或者目标尺寸。
Imgproc . resize ( src, dst, new Size ( width, height) , fx, fy, interpolation) ;
Imgproc.resize 参数 解释 src 原图像。 dst 输出图像。 Size(width, height) 目标尺寸。如果指定了这个参数,fx 和 fy 将被忽略。 fx 水平方向的缩放比例。 fy 垂直方向的缩放比例。 interpolation 插值方法。
interpolation 参数 解释 Imgproc.INTER_NEAREST 最近邻插值。 Imgproc.INTER_LINEAR 双线性插值(默认)。 Imgproc.INTER_AREA 使用像素区域关系进行重采样,通常用于缩小图像。 Imgproc.INTER_CUBIC 4x4像素邻域的双三次插值。 Imgproc.INTER_LANCZOS4 8x8像素邻域的 Lanczos 插值。
package com. xu. opencv. image ; import java. io. File ; import org. opencv. core. Mat ;
import org. opencv. core. Size ;
import org. opencv. highgui. HighGui ;
import org. opencv. imgcodecs. Imgcodecs ;
import org. opencv. imgproc. Imgproc ;
public class ImageResize { static { String os = System . getProperty ( "os.name" ) ; String type = System . getProperty ( "sun.arch.data.model" ) ; if ( os. toUpperCase ( ) . contains ( "WINDOWS" ) ) { File lib; if ( type. endsWith ( "64" ) ) { lib = new File ( "lib\\opencv\\x64\\" + System . mapLibraryName ( "opencv_java490" ) ) ; } else { lib = new File ( "lib\\opencv\\x86\\" + System . mapLibraryName ( "opencv_java490" ) ) ; } System . load ( lib. getAbsolutePath ( ) ) ; } } public static void main ( String [ ] args) { String path = "C:\\Users\\xuyq\\Desktop\\2.jpg" ; resize1 ( path) ; } public static void resize1 ( String path) { Mat src = Imgcodecs . imread ( path) ; Mat dst = new Mat ( ) ; Size size = new Size ( 300 , 300 ) ; Imgproc . resize ( src, dst, size, 0 , 0 , Imgproc . INTER_LINEAR ) ; HighGui . imshow ( "src" , src) ; HighGui . imshow ( "dst" , dst) ; HighGui . waitKey ( ) ; } }
2 仿射变换
Java OpenCV 代码 通过仿射变换矩阵进行缩放,可以同时进行旋转、平移等操作。
Mat affineMatrix = Imgproc . getAffineTransform ( srcPoints, dstPoints) ;
Imgproc . warpAffine ( src, dst, affineMatrix, new Size ( width, height) ) ;
Imgproc.getAffineTransform 参数 解释 srcPoints 原图像中的三个点。 dstPoints 目标图像中的三个点。
package com. xu. opencv. image ; import java. io. File ; import org. opencv. core. Mat ;
import org. opencv. core. MatOfPoint2f ;
import org. opencv. core. Point ;
import org. opencv. core. Size ;
import org. opencv. highgui. HighGui ;
import org. opencv. imgcodecs. Imgcodecs ;
import org. opencv. imgproc. Imgproc ;
public class ImageResize { static { String os = System . getProperty ( "os.name" ) ; String type = System . getProperty ( "sun.arch.data.model" ) ; if ( os. toUpperCase ( ) . contains ( "WINDOWS" ) ) { File lib; if ( type. endsWith ( "64" ) ) { lib = new File ( "lib\\opencv\\x64\\" + System . mapLibraryName ( "opencv_java490" ) ) ; } else { lib = new File ( "lib\\opencv\\x86\\" + System . mapLibraryName ( "opencv_java490" ) ) ; } System . load ( lib. getAbsolutePath ( ) ) ; } } public static void main ( String [ ] args) { String path = "C:\\Users\\xuyq\\Desktop\\2.jpg" ; resize2 ( path) ; } public static void resize2 ( String path) { Mat src = Imgcodecs . imread ( path) ; Mat dst = new Mat ( ) ; Point [ ] srcPoints = { new Point ( 0 , 0 ) , new Point ( src. cols ( ) - 1 , 0 ) , new Point ( 0 , src. rows ( ) - 1 ) } ; Point [ ] dstPoints = { new Point ( 0 , src. rows ( ) * 0.33 ) , new Point ( src. cols ( ) * 0.85 , src. rows ( ) * 0.25 ) , new Point ( src. cols ( ) * 0.15 , src. rows ( ) * 0.7 ) } ; MatOfPoint2f srcMat = new MatOfPoint2f ( srcPoints) ; MatOfPoint2f dstMat = new MatOfPoint2f ( dstPoints) ; Mat affineMatrix = Imgproc . getAffineTransform ( srcMat, dstMat) ; Imgproc . warpAffine ( src, dst, affineMatrix, new Size ( src. cols ( ) , src. rows ( ) ) ) ; HighGui . imshow ( "src" , src) ; HighGui . imshow ( "dst" , dst) ; HighGui . waitKey ( ) ; } }
3 透视变换
Java OpenCV 代码 用于更复杂的变换,包括缩放和透视校正。
Mat perspectiveMatrix = Imgproc . getPerspectiveTransform ( srcPoints, dstPoints) ;
Imgproc . warpPerspective ( src, dst, perspectiveMatrix, new Size ( width, height) ) ;
Imgproc.getPerspectiveTransform 参数 解释 srcPoints 原图像中的四个点。 dstPoints 目标图像中的四个点。
package com. xu. opencv. image ; import java. io. File ; import org. opencv. core. Mat ;
import org. opencv. core. MatOfPoint2f ;
import org. opencv. core. Point ;
import org. opencv. core. Size ;
import org. opencv. highgui. HighGui ;
import org. opencv. imgcodecs. Imgcodecs ;
import org. opencv. imgproc. Imgproc ;
public class ImageResize { static { String os = System . getProperty ( "os.name" ) ; String type = System . getProperty ( "sun.arch.data.model" ) ; if ( os. toUpperCase ( ) . contains ( "WINDOWS" ) ) { File lib; if ( type. endsWith ( "64" ) ) { lib = new File ( "lib\\opencv\\x64\\" + System . mapLibraryName ( "opencv_java490" ) ) ; } else { lib = new File ( "lib\\opencv\\x86\\" + System . mapLibraryName ( "opencv_java490" ) ) ; } System . load ( lib. getAbsolutePath ( ) ) ; } } public static void main ( String [ ] args) { String path = "C:\\Users\\xuyq\\Desktop\\2.jpg" ; resize3 ( path) ; } public static void resize3 ( String path) { Mat src = Imgcodecs . imread ( path) ; Mat dst = new Mat ( ) ; Point [ ] srcPoints = { new Point ( 0 , 0 ) , new Point ( src. cols ( ) - 1 , 0 ) , new Point ( src. cols ( ) - 1 , src. rows ( ) - 1 ) , new Point ( 0 , src. rows ( ) - 1 ) } ; Point [ ] dstPoints = { new Point ( src. cols ( ) * 0.05 , src. rows ( ) * 0.33 ) , new Point ( src. cols ( ) * 0.9 , src. rows ( ) * 0.25 ) , new Point ( src. cols ( ) * 0.8 , src. rows ( ) * 0.9 ) , new Point ( src. cols ( ) * 0.2 , src. rows ( ) * 0.7 ) } ; MatOfPoint2f srcMat = new MatOfPoint2f ( srcPoints) ; MatOfPoint2f dstMat = new MatOfPoint2f ( dstPoints) ; Mat perspectiveMatrix = Imgproc . getPerspectiveTransform ( srcMat, dstMat) ; Imgproc . warpPerspective ( src, dst, perspectiveMatrix, new Size ( src. cols ( ) , src. rows ( ) ) ) ; HighGui . imshow ( "src" , src) ; HighGui . imshow ( "dst" , dst) ; HighGui . waitKey ( ) ; } }