Java基於opencv—矯正圖像


更多的時候,我們得到的圖像不可能是正的,多少都會有一定的傾斜,就比如下面的
Java基於opencv—矯正圖像

我們要做的就是把它們變成下面這樣的
Java基於opencv—矯正圖像

我們采用的是尋找輪廓的思路,來矯正圖片;只要有明顯的輪廓都可以采用這種思路

具體思路:
1、先用opencv提供的canny函數,進行一次邊緣檢測
2、再用opencv提供的findContours函數,尋找圖像的輪廓,從中間結果種,找到最大的輪廓,就是我們圖像的最外面的輪廓
3、得到最終輪廓后,計算矩形輪廓與水平的夾角,然后旋轉圖像
4、最后我們在從旋轉后的圖像中,把我們感興趣的切割出來,就可以了

我們實際的實現一下

先用opencv提供的canny函數,進行一次邊緣檢測;具體的函數就不再講解,百度上非常多

/**
	 * canny算法,邊緣檢測
	 * 
	 * @param src
	 * @return
	 */
	public static Mat canny(Mat src) {
		Mat mat = src.clone();
		Imgproc.Canny(src, mat, 60, 200);
		HandleImgUtils.saveImg(mat , "C:/Users/admin/Desktop/opencv/open/x/canny.jpg");
		return mat;
	}

再用opencv提供的findContours函數,尋找圖像的輪廓,從中間結果種,找到最大的輪廓,就是我們圖像的最外面的輪廓

/**
	 * 返回邊緣檢測之后的最大矩形,並返回
	 * 
	 * @param cannyMat
	 *            Canny之后的mat矩陣
	 * @return
	 */
	public static RotatedRect findMaxRect(Mat cannyMat) {

		List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
		Mat hierarchy = new Mat();

		// 尋找輪廓
		Imgproc.findContours(cannyMat, 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);

		return rect;
	}

得到最終輪廓后,計算矩形輪廓與水平的夾角,然后旋轉圖像

/**
	 * 旋轉矩形
	 * 
	 * @param src
	 *            mat矩陣
	 * @param rect
	 *            矩形
	 * @return
	 */
	public static Mat rotation(Mat cannyMat, RotatedRect rect) {
		// 獲取矩形的四個頂點
		Point[] rectPoint = new Point[4];
		rect.points(rectPoint);

		double angle = rect.angle + 90;

		Point center = rect.center;

		Mat CorrectImg = new Mat(cannyMat.size(), cannyMat.type());

		cannyMat.copyTo(CorrectImg);

		// 得到旋轉矩陣算子
		Mat matrix = Imgproc.getRotationMatrix2D(center, angle, 0.8);

		Imgproc.warpAffine(CorrectImg, CorrectImg, matrix, CorrectImg.size(), 1, 0, new Scalar(0, 0, 0));

		return CorrectImg;
	}

最后我們在從旋轉后的圖像中,把我們感興趣的切割出來,就可以了

/**
	 * 把矯正后的圖像切割出來
	 * 
	 * @param correctMat
	 *            圖像矯正后的Mat矩陣
	 */
	public static void cutRect(Mat correctMat , Mat nativeCorrectMat) {
		// 獲取最大矩形
		RotatedRect rect = findMaxRect(correctMat);
		
		Point[] rectPoint = new Point[4];
		rect.points(rectPoint);
		
		int startLeft = (int)Math.abs(rectPoint[0].x);
		int startUp = (int)Math.abs(rectPoint[0].y < rectPoint[1].y ? rectPoint[0].y : rectPoint[1].y);
		int width = (int)Math.abs(rectPoint[2].x - rectPoint[0].x);
		int height = (int)Math.abs(rectPoint[1].y - rectPoint[0].y);
		
		System.out.println("startLeft = " + startLeft);
		System.out.println("startUp = " + startUp);
		System.out.println("width = " + width);
		System.out.println("height = " + height);
		
		for(Point p : rectPoint) {
			System.out.println(p.x + " , " + p.y);
		}
		
		Mat temp = new Mat(nativeCorrectMat , new Rect(startLeft , startUp , width , height ));
		Mat t = new Mat();
		temp.copyTo(t);
		
		HandleImgUtils.saveImg(t , "C:/Users/admin/Desktop/opencv/open/x/cutRect.jpg");
	}

整合整個過程

/**
	 * 矯正圖像
	 * 
	 * @param src
	 * @return
	 */
	public static void correct(Mat src) {
		// Canny
		Mat cannyMat = canny(src);

		// 獲取最大矩形
		RotatedRect rect = findMaxRect(cannyMat);

		// 旋轉矩形
		Mat CorrectImg = rotation(cannyMat , rect);
		Mat NativeCorrectImg = rotation(src , rect);
		
		
		//裁剪矩形
		cutRect(CorrectImg , NativeCorrectImg);
		
		HandleImgUtils.saveImg(src, "C:/Users/admin/Desktop/opencv/open/x/srcImg.jpg");

		HandleImgUtils.saveImg(CorrectImg, "C:/Users/admin/Desktop/opencv/open/x/correct.jpg");
	}

測試代碼

/**
	 * 測試矯正圖像
	 */
	public void testCorrect() {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
		Mat src = HandleImgUtils.matFactory("C:/Users/admin/Desktop/opencv/open/x/x7.jpg");
		HandleImgUtils.correct(src);
	}

Java方面opencv的例子還是蠻少的,代碼都是自己參考博客寫的,照顧不周的地方,請見諒

本項目的所有代碼地址:https://github.com/YLDarren/opencvHandleImg


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM