Ask Your Question
0

Displaying Mat Image in Android Screen

asked 2013-04-13 01:19:08 -0500

baharsan gravatar image

updated 2013-04-14 17:18:08 -0500

Hei, I have a little problem when i try to convert opencv c++ to opencv4android. here it is :

Android Source Code -- Online Processing

    for (Rect r : faces){
            Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(0, 255, 0, 255), 3);
            Mat roi = new Mat();
            roi = mRgba.submat(r);

            Mat gray = new Mat();
            Imgproc.cvtColor(roi, gray, Imgproc.COLOR_BGR2GRAY,1);

            Mat gray_f = new Mat();
            gray.convertTo(gray_f, CvType.CV_32F, 1.0/255, 0);

            Imgproc.cvtColor(gray_f, roi, Imgproc.COLOR_GRAY2BGR,0);
            Imgproc.cvtColor(roi, roi, Imgproc.COLOR_BGR2BGRA,0);
            Mat kotak = new Mat();
            kotak = mRgba.colRange(r.x, r.x+r.width).rowRange(r.y, r.y+r.height);
            roi.copyTo(kotak);
        }

C++ Source Code -- Offline Processing

 cv::Mat image = cv::imread("hand.jpg",1);
 cv::imshow("Src", image);
 cv::cvtColor(image, src, CV_BGR2GRAY);
 src.convertTo(src_f, CV_32F, 1.0/255, 0);
 cv::imshow("src", src);
 cv::imshow("src_f", src_f);

This the result of C++ :

image description

This the result of Android :

image description

I Think that's above code have the same result, but when i run above code in pc and in andrioid, they haven't same result. i don't know way, any body help me please to explain it. Thanks in Advance... your little help should make my life better.. :)

UPDATED

After some hard of research, i was find my final problem, i think i wrong to display Mat image, just look at this : http://stackoverflow.com/a/9471411/1593269, if i want to convert BGR to Gray image and then display it on the android screen i need this :

    Imgproc.cvtColor(image, dst4, Imgproc.COLOR_BGR2GRAY); //Convert BGR to GRAY
    Imgproc.cvtColor(dst4, image, Imgproc.COLOR_GRAY2BGR); **//Confuse with This**
    Imgproc.cvtColor(image, image, Imgproc.COLOR_BGR2BGRA); //To compate with default android standard (4 channel), so we need to change from 1 channel to 4 channel before we display

Somebody please help me to understand what the mean :

Imgproc.cvtColor(dst4, image, Imgproc.COLOR_GRAY2BGR); **//Confuse with This**

and For what this row of code ? if i don't write that's code, it will always force close. Once again, I am trying to convert mat of gray image to CV_32F, then i want to display it on the screen, and i am trying to figure it out by this :

Imgproc.cvtColor(imgmat, src, Imgproc.COLOR_RGB2GRAY,0);
src.convertTo(src_f, src_f.type(), 1.0/255, 0);
Imgproc.cvtColor(dest_mag, imgmat, Imgproc.COLOR_GRAY2RGB, 0);          
Imgproc.cvtColor(imgmat, imgmat, Imgproc.COLOR_RGB2RGBA, 0);

it will run smoothly on the screen but without desired result.(desired result : gray image with CV_32F (java primitive array))

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2013-04-15 01:57:47 -0500

Daniil Osokin gravatar image

Hi! In Android you have RGBA image (but in c++ BGR), so use Imgproc.COLOR_RGBA2GRAY code for grayscale conversion.

edit flag offensive delete link more

Comments

i already use COLOR_RGB2GRAY and it run smoothly , i can deal with that, what i am still confusing is when i was trying to convert gray image to different mat of type, in this case CV_32F, then display it in android screen, it will no result, but NOT forceclose. I still cannot Convert. may be you can help me buddy.

baharsan gravatar imagebaharsan ( 2013-04-15 08:36:36 -0500 )edit
1

answered 2013-04-28 10:27:08 -0500

baharsan gravatar image

After some research and experiment, finally i find the light, here it is. you just need to get the image back to the default type. like this :

for (Rect r : faces){
        Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(0, 255, 0, 255), 3);
        Mat roi = new Mat();
        roi = mRgba.submat(r);

        Mat gray = new Mat();
        Imgproc.cvtColor(roi, gray, Imgproc.COLOR_RGB2GRAY,1);

        Mat gray_f = new Mat();
        gray.convertTo(gray_f, CvType.CV_32F, 1.0/255, 0);

        Mat final = new Mat();
        gray_f.convertTo(final, CvType.CV_8U, 255, 0);

        Imgproc.cvtColor(final, roi, Imgproc.COLOR_GRAY2RGB,0);
        Imgproc.cvtColor(roi, roi, Imgproc.COLOR_RGB2RGBA,0);
        Mat kotak = new Mat();
        kotak = mRgba.colRange(r.x, r.x+r.width).rowRange(r.y, r.y+r.height);
        roi.copyTo(kotak);
    }     }

This is the Queen of This Answer :

        Mat final = new Mat();
        gray_f.convertTo(final, CvType.CV_8U, 255, 0);
edit flag offensive delete link more
0

answered 2013-04-13 05:22:09 -0500

updated 2013-04-15 01:25:58 -0500

Indeed this seems weird :) I have checked the functionality provided for android and I am finding the exact functions.

However, can you check that you are visualizing the correct things. I am seeing that you do a grayscale conversion to src. But then you convert to src_f. Sure you are not just visualizing the wrong image?

UPDATE : Think I might have found the solution.

If you check the cvtColor functionality for the android port of openCV, you can see that the Java wrapper does not yet provide the cvtColor function. Look at the imgproc library and see that there is in fact no port. This means that in C++ you can call the function, but in Java for android, there is simply no function implemented. This will result in no conversion and thus keeping you with the original file.

Also when looking at the C++ cvtColor information, I can find the following transformations (http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=cvtcolor#cvtcolor

They do not list the BGR2GRAY only the RGB2GRAY, which is weird since they do provide the functionality. Maybe an edit should be requested.

Cheerz!

edit flag offensive delete link more

Comments

please check my edited code above, i hope you can help me out from this. i just wanna know why these code run smoothly in opencv c++ and can't run in opencv4android. thank you steven for quick answer. and how to implement rightly convertTo in android.

baharsan gravatar imagebaharsan ( 2013-04-13 16:32:28 -0500 )edit

Basically be patient for others to help, I am not an android develloper myself, so no experience at that language. But I will take a look at it. A gues : try to explicitly define the size of your source matrix before using convertTo. I think that could be your problem.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-14 03:19:17 -0500 )edit

Hi Steven, thank you for your updated answer. In fact, i was actually can do cvtcolor using opencv 2.3.1 in Android to convert RGB to GRAY and display it in Android Screen it's no problem for me. my problem is when i try to convert gray image from default type of mat (i don't know what the default type of mat) to CV_32F and display it on the screen, the result is RGB image. in the other hand, i try do same thing in opencv C++ and the result is gray image with type of mat CV_32F. i Think, there is something wrong when i try to convert, but why ? i don't know, i am really confused with this. i hope you can help me out from my problem.

i don't know why this happen, do you have a friend or someone who expert ...(more)

baharsan gravatar imagebaharsan ( 2013-04-15 07:36:21 -0500 )edit

I guess @Daniil Osokin just provided the answer for you :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-15 08:08:33 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-04-13 01:19:08 -0500

Seen: 13,067 times

Last updated: Apr 28 '13