Ask Your Question
0

Bitmap in Android and Mat in OpenCV

asked 2012-10-19 22:22:37 -0600

Droidkie gravatar image

updated 2013-04-09 06:28:18 -0600

Andrey Pavlenko gravatar image

I have this code in OpenCV C++:

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main(){
    Mat src = imread("image.jpg");
    if (src.empty())
        return -1;
    pyrMeanShiftFiltering(src,src,10,20,1);
    imshow("src", src);
    waitKey(0);
    return 0;
}

And there is a Sample1View.java class from OpenCV 2.4.2 as a sample named "OpenCV Tutorial 1 - Add OpenCV". Now I want my code to work on Android so I replaced this line:

case VIEW_MODE_GRAY:
    Imgproc.cvtColor(mGraySubmat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);
    break;

to this:

case VIEW_MODE_GRAY:
    Imgproc.cvtColor(mGraySubmat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);
    Imgproc.pyrMeanShiftFiltering(mRgba,mRgba,10,20);
    break;

but I get this:

10-20 11:08:45.929: I/Sample::Activity(12846): Menu Item selected Preview GRAY
10-20 11:08:46.049: W/dalvikvm(12846): threadid=9: thread exiting with uncaught exception (group=0x4013a560)
10-20 11:08:46.059: E/AndroidRuntime(12846): FATAL EXCEPTION: Thread-10
10-20 11:08:46.059: E/AndroidRuntime(12846): CvException [org.opencv.core.CvException: ..\..\modules\imgproc\src\segmentation.cpp:345: error: (-210) Only 8-bit, 3-channel images are supported in function void cvPyrMeanShiftFiltering(const CvArr*, CvArr*, double, double, int, CvTermCriteria)
10-20 11:08:46.059: E/AndroidRuntime(12846): ]
10-20 11:08:46.059: E/AndroidRuntime(12846):    at org.opencv.imgproc.Imgproc.pyrMeanShiftFiltering_1(Native Method)
10-20 11:08:46.059: E/AndroidRuntime(12846):    at org.opencv.imgproc.Imgproc.pyrMeanShiftFiltering(Imgproc.java:7247)
10-20 11:08:46.059: E/AndroidRuntime(12846):    at com.duckie.a.Sample1View.processFrame(Sample1View.java:80)
10-20 11:08:46.059: E/AndroidRuntime(12846):    at com.duckie.a.SampleViewBase.run(SampleViewBase.java:185)
10-20 11:08:46.059: E/AndroidRuntime(12846):    at java.lang.Thread.run(Thread.java:1019)

What seems to be the problem? How can I get my code to work in Android? And, how do I do this without having a delay?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-04-09 10:40:03 -0600

OpenCV operates with RGBA (8-bit, 4 channel) images on Android platform by default. Most OpenCV algorithms are implemented for BGR (8-bit, 3-channel) matrices (see exception message). You need to convert RBGA image to BGR and run pyrMeanShiftFiltering on it.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-10-19 22:22:37 -0600

Seen: 2,642 times

Last updated: Apr 09 '13