【发布时间】:2017-11-27 18:21:25
【问题描述】:
我正在制作文档扫描仪。我正在使用 opencv 进行图像处理。在相机视图中,我在最大轮廓上包围矩形。它正在正确检测最大轮廓。现在我只想捕获用 native-lib.cpp 编写的 boudingRect。所以我想要java类中的native-lib对象。帮助得到那个。
Native-lib.cpp
extern "C"
JNIEXPORT void JNICALL
Java_prisca_ctest_OpenCvCamera_doWithMat(JNIEnv *env, jobject instance, jlong matAddrGr,
jlong matAddrRgba) {
try {
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<vector<Point> > contours; // Vector for storing contour
vector<Vec4i> 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) , 5);
} catch (int a) {
}
}
活动
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cam);
mOpenCvCameraView = (JavaCameraView) findViewById(R.id.tutorial1_activity_java_surface_view);
mOpenCvCameraView.setVisibility(View.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
btnCapture = (Button) findViewById(R.id.btnCapture);
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String timestamp = new SimpleDateFormat("ddMMyyyy_HHmmss", Locale.US).format(new Date());
File imgFolder = new File(FILE_LOCATION);
imgFolder.mkdir();
File image = new File(imgFolder, "Scan" + timestamp + ".jpg");
String fileName = FILE_LOCATION +
"/Scan" + timestamp + ".jpg";
Toast.makeText(OpenCvCamera.this, image + " saved", Toast.LENGTH_SHORT).show();
Imgcodecs.imwrite(fileName, mRgba);
}
}) ;
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
// input frame has RGBA format
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
doWithMat(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr());
return mRgba;
}
我应该在 Imgcodecs.imwrite(fileName, mRgba) 上面添加什么来裁剪矩阵并只保存 boundingRect 部分?
【问题讨论】:
-
请注意,您应该将
c++标记替换为jni,因为您的一个sn-p 包含jni代码(无论如何都是c++) -
第二次阅读后,它似乎更像是一个纯粹的
opencv问题。您基本上只是在询问使用opencv裁剪矩阵/图像的方法,对吗? -
不完全是,因为我在cameraview上得到了boudingRect。所以当我点击捕获按钮时,它应该只捕获边界矩形。 @Nepho
-
目前我可以捕捉图像,但它捕捉整个表面。 @Nepho
-
正如我所说,您似乎正在寻找“如何在 OpenCV 中裁剪 boudingRect”的简单答案,而无需先阅读文档。如果您不想阅读它的文档,我建议您尝试在线查找,或发布更相关的问题
标签: java android opencv image-processing java-native-interface