I'm newbie OCV and android developer. I want Imgproc.GaussianBlur filter in my app. When i use it application send "application stopped". I only add 3 lines to "OpenCV Tutorial 3 - Camera Control":
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat mat = inputFrame.gray();
org.opencv.core.Size s = mat.size();
Imgproc.GaussianBlur(mat, mat, s, 2);
return mat;
}
What could be wrong? I have Lenovo A820 Android 4.1.2 and tried it on OpenCV 2.4.4, 2.4.5 and 2.4.6. I tried different API. The Imgproc.Sobel(mat,mat,-1,1,1); filter works good.
look at the docs for GaussianBlur
it says: "ksize – Gaussian kernel size. ksize.width and ksize.height can differ but they both must be positive and odd."
so, imho, you confused the kernel size with the Mat's size, try something like:
Mat mat = inputFrame.gray();
org.opencv.core.Size s = new Size(3,3);
Imgproc.GaussianBlur(mat, mat, s, 2);
return mat;
Related
i'm currently developing an android application to recognize digital numbers of an electricity meter. i've done most of the work but i still not getting a good result. 80% of the time i get a false one.
This is an example (i'm testing with a kitchen scale which is very similar to the meter) :
Original photo :
image after cropping and processing with OpenCV :
image after OCR (expected result that was obtained after several shots) :
image after OCR (unexpected result which is obtained often) :
Method used to process the image with OpenCV :
public Bitmap Bildverarbeitung (Bitmap image){
Mat tmp = new Mat (image.getWidth(), image.getHeight(), CvType.CV_8UC1);
Utils.bitmapToMat(image, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
Imgproc.GaussianBlur(tmp, tmp, new Size(3, 3), 0);
Imgproc.threshold(tmp, tmp, 0, 255, Imgproc.THRESH_OTSU);
Utils.matToBitmap(tmp, image);
return image;
}
I used two trained data but only one works better :
traineddata that works good
traineddata that doesn't work
can anyone help me get better results.. Is there any changes that i can do? or other methods that i can apply ? thanks in advance
I am trying out OpenCV. In my demo, I want to show the camera preview frames as plain black and white. My demo compiles and runs fine (I could see the black and white preview frames) but crashes after running for a few seconds:
OpenCV Error: Insufficient memory (Failed to allocate 11059200 bytes)
...
cv::Mat::create(int, const int*, int)]
at org.opencv.core.Mat.n_Mat(Native Method)
at org.opencv.core.Mat.<init>(Mat.java:50)
my code:
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Mat src = inputFrame.rgba();
// insufficient memory error here?
Mat dest = new Mat(src.size(), src.type());
Imgproc.threshold(src, dest, 30, 255, Imgproc.THRESH_BINARY);
return dest;
}
What could be wrong?
I have resolved this issue by adding this line.
Mat.release()
to every OpenCV Matrix object at of course locations where each object was no longer needed
I posted a little part of my code, cause i keep getting a strange error that I can't seem to get rid of. The problem can be found on this line: Imgproc.GaussianBlur(mGray, mGray, new Size (5,5), 2.2, 2);
public Mat onCameraFrame(Mat inputFrame) {
mGray = new Mat();
Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);
// doing a gaussian blur prevents getting a lot of false hits
Imgproc.GaussianBlur(mGray, mGray, new Size (5,5), 2.2, 2);
// Values 3 and 4are the LowerThreshold and UpperThreshold.
Imgproc.Canny(inputFrame, mIntermediateMat, 80, 100);
Imgproc.cvtColor(mIntermediateMat,mRgba, Imgproc.COLOR_GRAY2BGRA, 4);
return mIntermediateMat;
}
The error i get from Eclipse is:
The method GaussianBlur(Mat,Mat,Size,double,double) in
the type imgproc is not applicable for the arguments (Mat,Mat,CameraSize,int,int)
I am using an edited version of tutorial3 Camera-control (OpenCV for Android version 2.4.4) where the output is shown as Canny's edge detection. I need the GaussianBlur to get rid of some of the smaller details. Does anyone know what exactly is wrong in this line of the code?
This code works fine. Just reorder the parameters as you need.
Imgproc.GaussianBlur(mGray, mGray, new Size(15,15),50);
Size means that you will use it as kernel size. Also kernel size must be odd! 50 shows the kernel standard deviation in the X direction.
Formula : sigma = 0.3 * ((kSize-1)*0.5 - 1) + 0.8
Here sigma is passed 50 so sigmaX = sigmaY = 50
I got this solution from Alexander Smorkalov, and it worked. Just change the Imgproc.GaussianBlur(mGray, mGray, new Size (5,5), 2.2, 2); to Imgproc.GaussianBlur(mGray, mGray, new org.opencv.core.Size (5,5), 2.2, 2);
I am developing an Android OpenCV app based on Opencv4android SDK tutorial 2 - Mixed Processing.
in the frame processing function public Mat onCameraFrame(CvCameraViewFrame inputFrame) {}
The frame is RGBA and I want to make RGB by doing this:
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
Mat mRgb=new Mat(640,480,CvType.CV_8UC3);
mRgba.convertTo(mRgb, CvType.CV_8UC3);
//Imgproc.cvtColor(mRgba, mRgb, CvType.CV_8UC3);
PinkImage(mRgb.dataAddr());
But when I debug and log the things I passed to the JNI part, I find it's not working at all.
mRgb is CV_8UC4 even after calling converto()
What is the cause of this?
OK, the answer is here
Imgproc.cvtColor(mRgba,mRgb,Imgproc.COLOR_RGBA2RGB);
instead of
mRgba.convertTo(mRgb, CvType.CV_8UC3);
Thanks a lot!!
You never use the converted data. You still pass mRgba.dataAddr() to PinkImage(), which is the unmodified RGBA image. You need to pass in the modified data:
PinkImage(mRgb.dataAddr());
Currently trying
<code>
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
Imgproc.Canny(mRgba, markers, 80, 90);
Mat threeChannel = new Mat();
Imgproc.cvtColor(mRgba, threeChannel, Imgproc.COLOR_BGR2GRAY);
Imgproc.watershed(threeChannel, markers);
return threeChannel;
}
</code>
However, it fails with
CvException [org.opencv.core.CvException: /home/reports/ci/slave/50-SDK/opencv/modules/imgproc/src/segmentation.cpp:147: error: (-210) Only 8-bit, 3-channel input images are supported in function void cvWatershed(const CvArr*, CvArr*)
Could you advise how to appropriately use the markers from a Canny/Sobel edge detection to feed a Watershed algorithm? Android-specifics would be greatly helpful as this is my first Android project.
The error states that the input image for watershed() must be an 8-bit 3-channels image. After calling cvtColor(), print the number of channels of threeChannel. Don't be surprised if it outputs 1.
Pass mRgba directly to watershed() and see what happens. One of my previous answers have working code using watershed, you can use that for testing.
You need to just convert your image from 4 channel to 3 channels.
For example
Imgproc.cvtColor(mat , mat, Imgproc.COLOR_BGRA2BGR);