欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java調(diào)用opencv實現(xiàn)圖片矯正功能

 更新時間:2023年09月04日 11:11:46   作者:冒泡的肥皂  
這篇文章主要為大家詳細介紹了Java如何調(diào)用opencv實現(xiàn)圖片矯正功能,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學習一下

1.通過霍夫線矯正土圖片

public static void getCorrect1(Mat image) {
Mat clone=image.clone();
Mat src=image.clone();
int width = image.width();
int height = image.height();
int pointCount = width * height;
Mat points=image.reshape(3, pointCount);
points.convertTo(points,  CvType.CV_32F);
Imgproc.GaussianBlur(clone, clone, new Size(3, 3), 0, 0);
HighGui.imshow("GaussianBlur", clone);
Imgproc.cvtColor(clone, clone,Imgproc.COLOR_BGR2GRAY);
HighGui.imshow("GRY", clone);
//一般來說,高閾值maxVal推薦是低閾值minVal的2~3倍
int lowThresh=20;
//邊緣檢測
Imgproc.Canny(clone, clone,lowThresh, lowThresh*3,3);
HighGui.imshow("Canny", clone);
Mat storage = new Mat();
/**
	 * HoughLines(Mat image, Mat lines, double rho, double theta, int threshold, double srn, double stn, double min_theta, double max_theta)
	 * image 原圖
	 * lines 霍夫線變換檢測到線條的輸出矢量,由(ρ,θ)表示
	 * rho   以像素為單位的距離精度(直線搜索時的進步尺寸的單位半徑)
	 * theta 以弧度為單位的角度精度(直線搜索時的進步尺寸的角度單位)
	 * threshold 累加平面的閾值參數(shù)(直線被識別時它在累加平面中必須達到的值)
	 * srn    對于多尺度霍夫變換,這是第三個參數(shù)進步尺寸的除數(shù)距離。
?       *        粗略累加器進步尺寸直接是rho,精確的累加器進步尺寸為rho/srn
	 * min_theta 檢測到的直線的最小角度
	 * max_theta 測到的直線的最大角度
	 */
double sum = 0;
double angle=0;
Imgproc.HoughLines(clone, storage, 1, Math.PI/ 180.0, 200, 0, 0);
for (int x = 0; x < storage.rows(); x++) {
	double[] vec = storage.get(x, 0);
	double rho = vec[0];
	double theta = vec[1];
	Point pt1 = new Point();
	Point pt2 = new Point();
	double a = Math.cos(theta);
	double b = Math.sin(theta);
	double x0 = a * rho;
	double y0 = b * rho;
	pt1.x = Math.round(x0 + 1000 * (-b));
	pt1.y = Math.round(y0 + 1000 * (a));
	pt2.x = Math.round(x0 - 1000 * (-b));
	pt2.y = Math.round(y0 - 1000 * (a));
	sum += theta;
	Imgproc.line(clone, pt1, pt2, new Scalar(255, 255, 255, 255), 1, Imgproc.LINE_4, 0);
}
HighGui.imshow("houghLines", clone);
double average = sum / storage.rows(); //對所有角度求平均,這樣做旋轉效果會更好
angle = average/ Math.PI * 180 - 90;
System.out.println("average:"+angle);
Point center=new Point();
center.x=image.cols()/2;
center.y=image.rows()/2;
// 得到旋轉矩陣算子
Mat matrix = Imgproc.getRotationMatrix2D(center, angle, 1);
Imgproc.warpAffine(src, src, matrix,src.size(), 1, 0, new Scalar(0, 0, 0));
HighGui.imshow("rotation", src);
}

2.通過輪廓檢測矯正土圖片

public static Mat getCorrect2(Mat image) {
Mat clone=image.clone();
Mat src=image.clone();
int width = image.width();
int height = image.height();
int pointCount = width * height;
Mat points=image.reshape(3, pointCount);
points.convertTo(points,  CvType.CV_32F);
Imgproc.GaussianBlur(clone, clone, new Size(3, 3), 0, 0);
HighGui.imshow("GaussianBlur", clone);
Imgproc.cvtColor(clone, clone,Imgproc.COLOR_BGR2GRAY);
HighGui.imshow("GRY", clone);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
// 尋找輪廓
Imgproc.findContours(clone, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE,
        new Point(0, 0));
// 找出匹配到的最大輪廓
double area = Imgproc.boundingRect(contours.get(0)).area();
int index = 0;
// 找出匹配到的最大輪廓
for (int i = 0; i < contours.size(); i++) {
    double tempArea = Imgproc.boundingRect(contours.get(i)).area();
    if (tempArea > area) {
        area = tempArea;
        index = i;
    }
}
MatOfPoint2f matOfPoint2f = new MatOfPoint2f(contours.get(index).toArray());
RotatedRect rect = Imgproc.minAreaRect(matOfPoint2f);
 // 獲取矩形的四個頂點
Point[] rectpoint = new Point[4];
rect.points(rectpoint);
double line1 = Math.sqrt((rectpoint[1].y - rectpoint[0].y)*(rectpoint[1].y - rectpoint[0].y) + (rectpoint[1].x - rectpoint[0].x)*(rectpoint[1].x - rectpoint[0].x));
double line2 = Math.sqrt((rectpoint[3].y - rectpoint[0].y)*(rectpoint[3].y - rectpoint[0].y) + (rectpoint[3].x - rectpoint[0].x)*(rectpoint[3].x - rectpoint[0].x));
double angle = rect.angle;
if (line1 > line2) 
{
	angle = 90 + angle;
}
Point center = rect.center;
Mat CorrectImg = new Mat(clone.size(), clone.type());
clone.copyTo(CorrectImg);
// 得到旋轉矩陣算子
Mat matrix = Imgproc.getRotationMatrix2D(center, angle, 0.8);
Imgproc.warpAffine(src, src, matrix, CorrectImg.size(), 1, 0, new Scalar(0, 0, 0));
HighGui.imshow("rotation", src);
return src;
}

3.兩個算法的應用場景

基于輪廓提取的矯正算法更適用于車牌、身份證、人民幣、書本、發(fā)票一類矩形形狀而且邊界明顯的物體矯正。

基于直線探測的矯正算法更適用于文本類的矯正。

到此這篇關于Java調(diào)用opencv實現(xiàn)圖片矯正功能的文章就介紹到這了,更多相關Java opencv圖片矯正內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Springboot整合Activiti操作詳解

    Springboot整合Activiti操作詳解

    這篇文章主要給大家詳細介紹了Springboot整合Activiti的操作流程,文中流程步驟和代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2023-07-07
  • SpringBoot靜態(tài)資源映射,圖片無法實時訪問問題及解決

    SpringBoot靜態(tài)資源映射,圖片無法實時訪問問題及解決

    文章介紹了Spring Boot中靜態(tài)資源映射配置,解決了圖片上傳后無法實時訪問的問題,通過配置虛擬路徑,將訪問路徑映射到指定的物理路徑,解決了圖片無法實時顯示的問題
    2025-02-02
  • SpringBoot實現(xiàn)獲取客戶端IP地理位置

    SpringBoot實現(xiàn)獲取客戶端IP地理位置

    在當今互聯(lián)的世界中,了解客戶端的地理位置對于提供個性化服務和增強用戶體驗至關重要,使用本文為大家介紹了SpringBoot獲取客戶端IP地理位置的相關方法,需要的小伙伴可以參考下
    2023-11-11
  • 教你使用Java獲取當前時間戳的詳細代碼

    教你使用Java獲取當前時間戳的詳細代碼

    這篇文章主要介紹了如何使用Java獲取當前時間戳,通過兩個java示例,向大家展示如何獲取java中的當前時間戳,文本通過示例代碼給大家展示了java獲取當前時間戳的方法,需要的朋友可以參考下
    2022-01-01
  • Java可視化之實現(xiàn)文本的加密和解密

    Java可視化之實現(xiàn)文本的加密和解密

    這篇文章主要介紹了Java可視化之實現(xiàn)文本的加密和解密,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java實現(xiàn)批量下載選中文件功能

    Java實現(xiàn)批量下載選中文件功能

    這篇文章主要介紹了Java實現(xiàn)批量下載選中文件功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-11-11
  • 快速上手Mybatis-plus結構構建過程

    快速上手Mybatis-plus結構構建過程

    這篇文章主要介紹了快速上手Mybatis-plus結構構建過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Mybatis實現(xiàn)自定義的typehandler三步曲

    Mybatis實現(xiàn)自定義的typehandler三步曲

    這篇文章主要介紹了Mybatis實現(xiàn)自定義的typehandler三步曲的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • SpringBoot中的Spring Cloud Hystrix原理和用法詳解

    SpringBoot中的Spring Cloud Hystrix原理和用法詳解

    在Spring Cloud中,Hystrix是一個非常重要的組件,Hystrix可以幫助我們構建具有韌性的分布式系統(tǒng),保證系統(tǒng)的可用性和穩(wěn)定性,在本文中,我們將介紹SpringBoot中的Hystrix,包括其原理和如何使用,需要的朋友可以參考下
    2023-07-07
  • Spring Boot Thymeleaf實現(xiàn)國際化的方法詳解

    Spring Boot Thymeleaf實現(xiàn)國際化的方法詳解

    這篇文章主要給大家介紹了關于Spring Boot Thymeleaf實現(xiàn)國際化的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-10-10

最新評論