[英]Extracting information from the largest rectangle in an image using openCV in Android
我使用以下代碼來查找從android相機捕獲的圖像的最大矩形:
private static Mat findLargestRectangle(Mat original_image) {
Mat imgSource = original_image;
Bitmap bmpOriOut = Bitmap.createBitmap(imgSource.cols(), imgSource.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(imgSource, bmpOriOut);
try {
bmpOriOut.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/original.jpg"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//convert the image to black and white, commenting this wont crash
Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2RGB);
//convert the image to black and white does (8 bit), commenting this crashes
Imgproc.Canny(imgSource, imgSource, 50, 50);
//apply gaussian blur to smoothen lines of dots, commenting this crashes
Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5);
//find the contours
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = -1;
int maxAreaIdx = -1; MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
MatOfPoint2f approxCurve = new MatOfPoint2f();
Mat largest_contour = contours.get(0);
List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
for (int idx = 0; idx < contours.size(); idx++) {
temp_contour = contours.get(idx);
double contourarea = Imgproc.contourArea(temp_contour);
//compare this contour to the previous largest contour found
if (contourarea > maxArea) {
//check if this contour is a square
MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
int contourSize = (int)temp_contour.total();
Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true);
if (approxCurve.total() == 4) {
maxArea = contourarea;
maxAreaIdx = idx;
largest_contours.add(temp_contour);
largest_contour = temp_contour;
}
}
}
MatOfPoint temp_largest = largest_contours.get(largest_contours.size()-1);
largest_contours = new ArrayList<MatOfPoint>();
largest_contours.add(temp_largest);
Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
Imgproc.drawContours(imgSource, largest_contours, -1, new Scalar(0, 255, 0), 1);
//create the new image here using the largest detected square
//Toast.makeText(getApplicationContext(), "Largest Contour: ", Toast.LENGTH_LONG).show();
Bitmap bmpOut = Bitmap.createBitmap(imgSource.cols(), imgSource.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(imgSource, bmpOut);
try {
bmpOut.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/bigrect.jpg"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imgSource;
}
像這樣成功檢測到圖像中最大的矩形: http : //img153.imageshack.us/img153/9308/nn4w.png
但是我的問題是,如何將“檢測到的最大矩形”另存為位圖(無黑白),請多多幫助。
而不是在imgSource
矩陣上繪制它:
Imgproc.drawContours(imgSource, largest_contours, -1, new Scalar(0, 255, 0), 1);
創建一個空的黑白Mat
並在其上繪制。 像這樣( 黑色初始矩陣):
emptyMat = Mat.zeros(imgSource.size(), imgSource.channels());
...
Imgproc.drawContours(emptyMat, largest_contours, -1, new Scalar(0, 255, 0), 1);
當然,您可以使用Mat
構造函數來創建所需的初始背景矩陣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.