I developed an Document scanner. I am using opencv for image processing. It works perfectly with C++ code, but i want that in java so i converted it. But not detecting document properly when i am using java code.
Actually cant implement this line in java
> sort(contours.begin(), contours.end(),
compareContourAreas);
**Result with c++ code**
[![Result With C++][1]][1]
**Result wit Java code**
[![Result with Java][2]][2]
**C++ code**
using namespace cv;
using namespace std;
extern "C"
bool compareContourAreas(std::vector contour1,
std::vector contour2) {
double i = fabs(contourArea(cv::Mat(contour1)));
double j = fabs(contourArea(cv::Mat(contour2)));
return (i > j);
}
extern "C"
JNIEXPORT jobject JNICALL
Java_com_example_setlmint_setlmint_CameraScreen_doWithMat(JNIEnv *env, jobject instance,
jlong matAddrGr, jlong matAddrRgba) {
Mat &image = *(Mat *) matAddrRgba;
Rect bounding_rect;
Mat thr(image.rows, image.cols, CV_8UC1);
cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray
threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray
vector> contours; // Vector for storing contour
vector hierarchy;
findContours(thr, contours, hierarchy, CV_RETR_CCOMP,
CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
sort(contours.begin(), contours.end(),
compareContourAreas); //Store the index of largest contour
bounding_rect = boundingRect((const _InputArray &) contours[0]);
rectangle(image, bounding_rect, Scalar(250, 250, 250) , 3);
jclass rectClass = env->FindClass("org/opencv/core/Rect");
jmethodID rectCtorID = env->GetMethodID(rectClass, "", "(IIII)V");
return env->NewObject(rectClass, rectCtorID, bounding_rect.x, bounding_rect.y, bounding_rect.width, bounding_rect.height);
}
**Converted into Java**
Rect bounding_rect;
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
mRgbaT = mRgba.t();
Core.flip(mRgba.t(), mRgbaT, 1);
Imgproc.resize(mRgbaT, mRgbaT, mRgba.size());
Imgproc.cvtColor( mRgba, mGray, Imgproc.COLOR_BGR2GRAY );
Imgproc.threshold( mGray, mGray, 155, 255, Imgproc.THRESH_BINARY + Imgproc.THRESH_OTSU );
List contours = new ArrayList();
Mat hierarchy = new Mat();
Imgproc.findContours( mGray, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE );
int index= 0 ;
double maxim = 0;
for (int contourIdx = 0; contourIdx
↧