resize() fail when i pass newSize parameter in open cv - android

In android i am trying to scale my mat using following code.
Mat destMat = new Mat();
Size newSize=new Size(output.width()/6.0f, output.height()/6.0f);
Imgproc.resize(output, destMat, newSize, 0, 0, Imgproc.INTER_CUBIC);
When it try to execute the resize method i get following exception. My code is in onCameraFrame().
MatToBitmap catched cv::Exception:
/home/reports/ci/slave_desktop/50-SDK/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)

Where the variable 'newSize' is, you actually want the original size of destMat. You can scale it down by using the fx and fy parameters. From the javadocs:
The function resize resizes the image src down to or up to the
specified size.Note that the initial dst type or size are not taken
into account. Instead, the size and type are derived from the
src,dsize,fx, and fy.
Later on:
// explicitly specify dsize=dst.size(); fx and fy will be computed
from that.

Related

Mat to Bitmap exception thrown

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.

OpenCV resize failing

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 :)

OpenCV4Android - find/drawContour error

What I'm attempting to do is take a thresholded image and perform findContours on it then draw it out to a rotation corrected image. The rotation corrected image and the thresholded image work as expected so I'm at a bit of a loss figuring out why this would crash. The thersholded image is a grey version of the rotation corrected image that has had a binary threshold applied to it.
public void findImageContours(Mat passedThreshInt, Mat passedRotatedInit)
{
/* Get Thresholded input image */
Mat initThresh = passedThreshInt.clone();
/* Get image to draw Contours on */
Mat initRotated = passedRotatedInit.clone();
/* Get contours from threshold image */
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(initThresh, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
/* Draw Contours */
Scalar CONTOUR_COLOR = new Scalar(255,0,0,255);
Log.e(TAG, "Contours count: " + contours.size());
Imgproc.drawContours(initRotated, contours, -1, CONTOUR_COLOR);
/* Save Output */
contouredInit = initRotated.clone(); //ContouredInit is Global
Utils.matToBitmap(contouredInit, contouredBitmapInit); // contouredBitmapInit is Global
}
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 /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 97
nMatToBitmap catched cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/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)
And after that a JDI Dispatch Error.
Dimensions of Bitmap has to match the dimensions of OpenCV Mat. I suggest you create Bitmap as below.
Bitmap bitmapImg = Bitmap.createBitmap( matImg.cols(), matImg.rows(), Bitmap.Config.ARGB_8888 );
For future errors of these kind, a look into opencv github repository(1) might help you.

Assertion failed src.type in Utils.matToBitmap

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.

OpenCV nMatToBitmap Assertion Failed

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;
}

Categories

Resources