Android OpenCV Perspective Transform Result
I'm making an Android Application using OpenCV Library for Android. I'm
taking a picture in my application which is a receipt and I'm trying to
apply some filters and make a transformation with warpPerspective
function. I found some code here:Java OpenCV deskewing a contour and I'm
trying to use it in my application. What I have done until now is the code
below + the Quadraple class from previous page. I don't what is going
wrong. Below it's the code I use:
public class ImgProClean {
private static String imagePath =
Environment.getExternalStorageDirectory() + "/DCIM/Camera";
public static Bitmap start(Bitmap bitmap){
//Create mat images
Mat img0 = new Mat(bitmap.getWidth(), bitmap.getHeight(),
CvType.CV_8UC1);
Utils.bitmapToMat(bitmap, img0);
Mat img1 = new Mat(bitmap.getWidth(), bitmap.getHeight(),
CvType.CV_8UC1);
img0.copyTo(img1);
Mat img2 = new Mat(img0.width(), img0.height(), CvType.CV_8UC1);
if(!img1.empty()){
Imgproc.cvtColor(img1, img1, Imgproc.COLOR_RGB2GRAY);
}
Imgproc.adaptiveThreshold(img1, img1,
255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY,501,0);
Imgproc.erode(img1, img1,
Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(3,3)),
new Point(1,1), 10);
Imgproc.dilate(img1, img1,
Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(3,
3)),new Point(1,1), 10);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(img1, contours, new Mat(), Imgproc.RETR_EXTERNAL,
Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = -1;
int maxAreaIdx = -1;
for (int idx = 0; idx < contours.size(); idx++) {
Mat contour = contours.get(idx);
double contourarea = Imgproc.contourArea(contour);
if (contourarea > maxArea) {
maxArea = contourarea;
maxAreaIdx = idx;
}
}
//Imgproc.drawContours(img1, contours, maxAreaIdx, new Scalar(255), 100);
QuadRangle.fromContour(contours.get(maxAreaIdx));
img2 = QuadRangle.warp(img0);
Utils.matToBitmap(img2, bitmap);
try {
FileOutputStream out = new FileOutputStream(imagePath +
"/warpImage.jpg");
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
What i want to do is something like that:(From figure 7 to 8)
The result I'm getting is always NullPointer Exception in Quadraple class
in the lines:
Core.line(result, lines[TOP].get(-5000), lines[TOP].get(5000), new
Scalar(200, 100, 100), 8);
Core.line(result, lines[RIGHT].get(-5000), lines[RIGHT].get(5000), new
Scalar(0, 255, 0), 8);
Core.line(result, lines[BOTTOM].get(-5000), lines[BOTTOM].get(5000), new
Scalar(255, 0, 0), 8);
Core.line(result, lines[LEFT].get(-5000), lines[LEFT].get(5000), new
Scalar(0, 0, 255), 8);
Point p = Line.intersect(lines[TOP], lines[LEFT]);
I think that the problem might be in the get function where the number is
5000. I tried to understand what is this number for by changing to 1000 or
10000 but nothing changed. How can I fix the code so that it works fine?
No comments:
Post a Comment