Java OpenCV圖像處理之仿射變換,透視變換,旋轉(zhuǎn)詳解
1 仿射變換
仿射變換:一種二維坐標(biāo)到二維坐標(biāo)的線性變換,它保持二維圖像的平直性與平行性,即變換后直線依然是直線,平行的線依然平行。
package com.xu.opencv.image; import java.io.File; import java.util.ArrayList; import java.util.List; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint2f; import org.opencv.core.Point; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; /** * @author Administrator */ public class ImageChange { 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-455\\x64\\" + System.mapLibraryName("opencv_java455")); } else { lib = new File("lib\\OpenCV-455\\x86\\" + System.mapLibraryName("opencv_java455")); } System.load(lib.getAbsolutePath()); } } public static void main(String[] args) { warpAffine(); } /** * OpenCV 仿射變換 * * @return void * @Author: hyacinth * @Title: warpAffine * @Description: TODO * @date: 2022年2月22日12點(diǎn)32分 */ public static void warpAffine() { Mat src = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\1.png"); MatOfPoint2f point1 = new MatOfPoint2f(new Point(0, 0), new Point(0, src.rows()), new Point(src.cols(), 0)); MatOfPoint2f point2 = new MatOfPoint2f(new Point(src.cols() * 0.1, src.cols() * 0.1), new Point(src.cols() * 0.2, src.cols() * 0.7), new Point(src.cols() * 0.7, src.cols() * 0.2)); // 獲取 放射變換 矩陣 Mat dst = Imgproc.getAffineTransform(point1, point2); // 進(jìn)行 仿射變換 Mat image = new Mat(); Imgproc.warpAffine(src, image, dst, src.size()); HighGui.imshow("原圖", src); HighGui.imshow("仿射變換", image); HighGui.waitKey(0); } }
2 透視變換
透視變換:透視變換是將一個平面投影到另一個平面的過程,也稱投影映射。是一種非線性變換,表現(xiàn)為可將梯形變換為平行四邊形,因此需要四個點(diǎn)來確定透視變換矩陣
package com.xu.opencv.image; import java.io.File; import java.util.ArrayList; import java.util.List; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint2f; import org.opencv.core.Point; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; /** * @author Administrator */ public class ImageChange { 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-455\\x64\\" + System.mapLibraryName("opencv_java455")); } else { lib = new File("lib\\OpenCV-455\\x86\\" + System.mapLibraryName("opencv_java455")); } System.load(lib.getAbsolutePath()); } } public static void main(String[] args) { warpPerspective(); } /** * OpenCV 透視變換 * * @return void * @Author: hyacinth * @Title: warpPerspective * @Description: TODO * @date: 2022年2月22日12點(diǎn)32分 */ public static void warpPerspective() { Mat src = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\1.png"); MatOfPoint2f point1 = new MatOfPoint2f(); List<Point> before = new ArrayList<>(); before.add(new Point(0, 0)); before.add(new Point(src.cols(), 0)); before.add(new Point(0, src.rows())); before.add(new Point(src.cols(), src.rows())); point1.fromList(before); MatOfPoint2f point2 = new MatOfPoint2f(); List<Point> after = new ArrayList<>(); after.add(new Point(src.cols(), src.rows())); after.add(new Point(src.cols() * 0.1, src.rows() * 0.8)); after.add(new Point(src.cols() * 0.7, src.rows() * 0.3)); after.add(new Point(0, 0)); point2.fromList(after); // 獲取 透視變換 矩陣 Mat dst = Imgproc.getPerspectiveTransform(point1, point2); // 進(jìn)行 透視變換 Mat image = new Mat(); Imgproc.warpPerspective(src, image, dst, src.size()); HighGui.imshow("原圖", src); HighGui.imshow("透視變換", image); HighGui.waitKey(0); } }
3 圖像旋轉(zhuǎn)
package com.xu.opencv.image; import java.io.File; import java.util.ArrayList; import java.util.List; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint2f; import org.opencv.core.Point; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; /** * @author Administrator */ public class ImageChange { 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-455\\x64\\" + System.mapLibraryName("opencv_java455")); } else { lib = new File("lib\\OpenCV-455\\x86\\" + System.mapLibraryName("opencv_java455")); } System.load(lib.getAbsolutePath()); } } public static void main(String[] args) { rotate(); } /** * OpenCV 透視變換 * * @return void * @Author: hyacinth * @Title: rotate * @Description: TODO * @date: 2022年2月22日12點(diǎn)32分 */ public static void rotate() { Mat src = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\1.png"); // 圖像中心 Point center = new Point(src.cols() / 2, src.rows() / 2); // 獲取 旋轉(zhuǎn) 矩陣 Mat dst = Imgproc.getRotationMatrix2D(center, 45, 0.5); // 進(jìn)行 圖像旋轉(zhuǎn) Mat image = new Mat(); Imgproc.warpAffine(src, image, dst, src.size()); HighGui.imshow("原圖", src); HighGui.imshow("圖像旋轉(zhuǎn)", image); HighGui.waitKey(0); } }
到此這篇關(guān)于Java OpenCV圖像處理之仿射變換,透視變換,旋轉(zhuǎn),平移,縮放詳解的文章就介紹到這了,更多相關(guān)Java OpenCV圖像處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?AOP手寫動態(tài)代理代碼實(shí)例
這篇文章主要介紹了Spring?AOP手寫動態(tài)代理代碼實(shí)例,AOP我們知道,是在不修改源代碼的情況下,為代碼添加一些新功能的技術(shù),通過動態(tài)代理,可以在不修改原始類代碼的前提下,對方法進(jìn)行攔截和增強(qiáng),需要的朋友可以參考下2024-01-01Java使用集合實(shí)現(xiàn)斗地主分牌完整代碼
在斗地主游戲中,通常是將一副牌平均分成3份,每份17張牌,并留3張底牌,我們可以使用集合來實(shí)現(xiàn)這一功能,這篇文章主要給大家介紹了關(guān)于Java使用集合實(shí)現(xiàn)斗地主分牌的相關(guān)資料,需要的朋友可以參考下2024-05-05Java棧之鏈?zhǔn)綏4鎯Y(jié)構(gòu)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java棧之鏈?zhǔn)綏4鎯Y(jié)構(gòu)的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04Mybatis 動態(tài)SQL搭建環(huán)境的全過程
這篇文章主要給大家介紹了關(guān)于Mybatis動態(tài)SQL搭建環(huán)境的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05mybatis-plus之如何根據(jù)數(shù)據(jù)庫主鍵定義字段類型
這篇文章主要介紹了mybatis-plus之如何根據(jù)數(shù)據(jù)庫主鍵定義字段類型問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07使用java實(shí)現(xiàn)百萬級別數(shù)據(jù)導(dǎo)出excel的三種方式
這篇文章主要介紹了使用java實(shí)現(xiàn)百萬級別數(shù)據(jù)導(dǎo)出excel的三種方式,有些業(yè)務(wù)系統(tǒng)可能動輒涉及到百萬上千萬的數(shù)據(jù),用正常的方法效率就變得很低,今天我們來看看這幾種實(shí)現(xiàn)思路2023-03-03SpringBoot ThreadLocal實(shí)現(xiàn)公共字段自動填充案例講解
每一次在Controller層中封裝改動數(shù)據(jù)的方法時(shí)都要重新設(shè)置一些共性字段,顯得十分冗余。為了解決此問題也是在項(xiàng)目中第一次利用到線程,總的來說還是讓我眼前一亮,也開闊了視野,對以后的開發(fā)具有深遠(yuǎn)的意義2022-10-10springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConver
這篇文章主要介紹了springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConversions問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08SpringMVC實(shí)現(xiàn)賬號只能在一處登陸
這篇文章主要為大家詳細(xì)介紹了SpringMVC如何實(shí)現(xiàn)賬號只能在一處登陸,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03