I'm trying to use Laplacian in my app:
Bitmap result = source.copy(source.getConfig(), true);
Utils.bitmapToMat(source, in);
Imgproc.Laplacian(in, out, 3, 3, 1, 0);
Utils.matToBitmap(out, result);
But I get the following error:
E/cv::error()﹕ OpenCV Error: Assertion failed (src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
According to documentation Utils.matToBitmap accepts only Mat of CV_8U depth. In your example you specify output depth to CV_16S. You should specify output depth for Imgproc.Laplacian, as follows:
Imgproc.Laplacian(in, out, CvType.CV_8U, 3, 1, 0);
Utils.matToBitmap(out, result);
See documentation for Laplacian.
Related
The following are My Code for drawing border in openCV
Mat src = new Mat(imageChosen.getWidth(), imageChosen.getHeight(), CvType.CV_8UC3);
Utils.bitmapToMat(imageChosen, src);
Mat dst = new Mat(imageChosen.getWidth(), imageChosen.getHeight(), CvType.CV_8UC3);
Core.copyMakeBorder(src, dst, 10, 10, 10, 10, Core.BORDER_CONSTANT, new Scalar(0,0,0));
Bitmap b = Bitmap.createBitmap(imageChosen.getWidth()+20, imageChosen.getHeight()+20,Bitmap.Config.ARGB_8888);
Utils.matToBitmap(src, b);
and i don't know why it throw
E/cv::error(): OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp, line 97
E/org.opencv.android.Utils: nMatToBitmap catched cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
Any help/ideas would be greatly appreciated, thanks.
If you take a look at the documentation of Mat, you can see that the constructor expects its params in the following order: rows, columns.
You're passing the width (columns) first and the height (rows) after.
Instead of the following:
Mat src = new Mat(imageChosen.getWidth(), imageChosen.getHeight(), CvType.CV_8UC3);
You should pass the height first:
Mat src = new Mat(imageChosen.getHeight(), imageChosen.getWidth(), CvType.CV_8UC3);
For both src and dst.
I have an application using A JavaCameraView from OpenCV for android that uses the following callback
CameraBridgeViewBase.CvCameraViewListener cvListener = new CameraBridgeViewBase.CvCameraViewListener() {
#Override
public void onCameraViewStarted(int width, int height) {
}
#Override
public void onCameraViewStopped() {
}
#Override
public Mat onCameraFrame(Mat inputFrame) {
Size newSize = new Size(400, 200);
Mat fit = new Mat(newSize, CvType.CV_8UC4);
Imgproc.resize(inputFrame,fit,newSize);
return fit;
}
};
However, when the resizing happens, an error occurs saying
E/CameraBridge: Utils.matToBitmap() throws an exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
E/cv::error(): OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp, line 97
E/org.opencv.android.Utils: nMatToBitmap catched cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
E/CameraBridge: Mat type: Mat [ 244*432*CV_8UC4, isCont=true, isSubmat=false, nativeObj=0x1ade1b0, dataAddr=0x5dbc6010 ]
E/CameraBridge: Bitmap type: 240*160
Nothing happens in the actual UI. I'm not sure why I'm seeing this error, since I'm never calling Utils.bitmapToMat or Utils.matToBitmap. When I take out the Imgproc.resize(inputFrame,fit,newSize); and return the inputFrame, everything works as expected.
For onCameraFrame, I believe you have to return the Mat of the same size as your inputFrame. Since the resolution of the image obtained from your camera is
240*160 (refer this-> E/CameraBridge: Bitmap type: 240*160) you will need to resize your Mat fit back to 240*160 after you done your processing..
A simple fix:
#Override
public Mat onCameraFrame(Mat inputFrame) {
Size newSize = new Size(400, 200);
Mat fit = new Mat(newSize, CvType.CV_8UC4);
Imgproc.resize(inputFrame,fit,newSize);
/**Your processing code */
Imgproc.resize(fit,fit,inputFrame.size()); <- Add this line
return fit;
}
To change the size of the image you receive from camera, you have to call mOpenCvCameraView.setMaxFrameSize(width, height); but the resolution will be chosen from the list of resolutions supported by the camera hardware. To know more why you can't set an arbitrary value for the resolution you'll need to read on Android Camera API, since that's what OpenCV uses to grab the images at the back :)
i want to calculate OpticalFlow and then show that but i get error.
my code:
Bitmap resultBitmap = mResultBitmaps.poll();
Imgproc.cvtColor(image, image, Imgproc.COLOR_BGR2GRAY);
Mat flow = new Mat(image.size(), CvType.CV_8UC1);
Video.calcOpticalFlowFarneback(image, Prev_image, flow,0.5,1, 1, 1, 7,1.5,1);
Utils.matToBitmap(flow, resultBitmap, true);
but i get this error :
Assertion failed (src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
and when I debug flow is :
Mat [ 320*320*CV_32FC2, isCont=true, isSubmat=false, nativeObj=0x77dd5148, dataAddr=0x77c7e010 ]
what can I do?
flow is a 2 channel 32 bit floating point number. As the error says, the input needs to be CV_8U either 1, 3 or 4 channels. You will need to decide how you want to represent the 2 channel data. For example, split the channels and use convertTo to convert each to CV_8U to give you two images.
Can anybody tell me what am I doing wrong or give me example how to create AND DISPLAY histogram from grayscale mat in OpenCV4 Android? There is my code...
Vector<Mat> bgr_planes = new Vector<Mat>();
Core.split(cleanFaceMatGRAY, bgr_planes);
MatOfInt histSize = new MatOfInt(256);
final MatOfFloat histRange = new MatOfFloat(0f, 256f);
boolean accumulate = false;
Mat b_hist = new Mat();
Imgproc.calcHist(bgr_planes, new MatOfInt(0),new Mat(), b_hist, histSize, histRange, accumulate);
And then I try to set it to Image view to see it:
ivPickedPhoto.setImageBitmap(AppTools.createBitmapFromMat(b_hist, Bitmap.Config.ARGB_8888));
Method for display was created on my own:
public class AppTools {
public static Bitmap createBitmapFromMat(Mat mat, Bitmap.Config config) {
Bitmap bmp2 = null;
bmp2 = Bitmap.createBitmap(mat.cols(), mat.rows(), config);
Utils.matToBitmap(mat, bmp2);
return bmp2;
}
This gives me an error in the ivPickedPhoto(...) lane:
12-10 21:12:59.910: E/AndroidRuntime(10301): Caused by: CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:98: error: (-215) src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4 in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
I guess my problem is with the way I want to display the histogram... prolly I cant convert it to bmp. What is a good way to show histogram?
The OpenCV Java Documentation states that the Mat object you are passing should be of type CV_8UC1, CV_8UC3 or CV_8UC4. The documentation of calcHist doesn't really specify which type you should expect your b_Hist to be, but since it is complaining about the Mat type:
error: (-215) src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4
you should probably convert it to either of the three above formats.
I'm getting the following errors using some generic functions within OpenCV for Android
12-05 21:08:55.486: E/cv::error()(6658): OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /home/oleg/sources/opencv/modules/java/generator/src/cpp/utils.cpp, line 107
12-05 21:08:55.486: E/org.opencv.android.Utils(6658): nMatToBitmap catched cv::Exception: /home/oleg/sources/opencv/modules/java/generator/src/cpp/utils.cpp:107: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
12-05 21:08:55.486: E/CameraBridge(6658): Mat type: Mat [ 144*192*CV_8UC3, isCont=true, isSubmat=false, nativeObj=0x1024c0, dataAddr=0x44783010 ]
12-05 21:08:55.486: E/CameraBridge(6658): Bitmap type: 384*288
12-05 21:08:55.486: E/CameraBridge(6658): Utils.matToBitmap() throws an exception: /home/oleg/sources/opencv/modules/java/generator/src/cpp/utils.cpp:107: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
I'm not sure if this is the error itself or if it's caused by another problem.
The assertion error is telling you that one or more of the following tests is failing:
src.dims == 2
info.height == (uint32_t)src.rows
info.width == (uint32_t)src.cols
I'm guessing info contains the dimensions of the destination bitmap. In that case, either your source Mat is not 2 dimensions or the dimensions of the destination bitmap don't match the dimensions of the source Mat.
These two lines
12-05 21:08:55.486: E/CameraBridge(6658): Mat type: Mat [ 144*192*CV_8UC3, isCont=true, isSubmat=false, nativeObj=0x1024c0, dataAddr=0x44783010 ]
12-05 21:08:55.486: E/CameraBridge(6658): Bitmap type: 384*288
suggest that your Mat is 144x192 and your bitmap is 384x288. It looks like one is portrait and the other landscape plus your bitmap is twice the resolution of your Mat.
I don't have enough rep to comment so I'll provide an answer instead:
When working with the 'onCameraFrame' method - this assertion is thrown if the 'mat' you return doesn't match the size of the frame used to display the output.
In other words - if you're resizing for some sort of processing, make sure to return it to the original size before throwing it back to the display.
#Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame
inputFrame) {
mRgba = inputFrame.rgba();
Mat resizeImage = new Mat();
Size sz = new Size(800, 600); // Scale up to 800x600
Imgproc.resize(mRgba, resizeImage, sz);
// Do some sort of processing here on resized image.
Mat afterResize = new Mat();
Size szAfter = new Size(640, 480); // Scale back down to 640x480 (original dim.)
Imgproc.resize(resizeImage, afterResize, szAfter);
return afterResize;
}