3

I'm writing an Android app in Eclipse that uses the OpenCV4Android API. How can I display a Mat image easily, for debugging only? In C++, according to the OpenCV tutorials, you'd do something like:

namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.

but the Java API for Android doesn't seem to have a namedWindow function inside org.opencv.highgui.Highgui.

Also, I'd like to load the image as grayscale. In C++, according to imread not working in Opencv, you'd do:

imread("blackandwhite.jpg", 0);

but the Java API's Highgui.imread() has only the filename argument.

Community
  • 1
  • 1
1''
  • 26,823
  • 32
  • 143
  • 200

4 Answers4

7

Summary:

Convert image to grayscale: Imgproc.cvtColor(image, image, Imgproc.COLOR_BGR2GRAY);

Display image: see here and here.

Community
  • 1
  • 1
1''
  • 26,823
  • 32
  • 143
  • 200
3

This is a sample code which would display an image (must have the image in drawable folder) in imageView using OpenCV:

 ImageVIew imgView = (ImageView) findViewById(R.id.sampleImageView);
        Mat mRgba = new MAt();
        mRgba = Utils.loadResource(MainAct.this, R.drawable.your_image,Highgui.CV_LOAD_IMAGE_COLOR);
        Bitmap img = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(),Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(mRgba, img);
        imgView.setImageBitmap(img);

and xml must have an ImageView as below :

<ImageView
        android:id="@+id/sampleImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>

Hope this helps in solving your problem..

Rekha
  • 901
  • 1
  • 11
  • 20
2

but the Java API for Android doesn't seem to have a namedWindow function inside org.opencv.highgui.Highgui.

Because you have to show your image on View. Take a look at samples from WEB.

Also, I'd like to load the image as grayscale.

Use cvCvtColor with code CV_BGR2GRAY for such type of convertion.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
  • 1
    I'm not using JavaCV, which is a third-party wrapper to OpenCV. Rather, I'm using OpenCV for Android, which has different syntax than C++/JavaCV. Thanks for the tip about cvCvtColor - this led me to the solution for the grayscale conversion, which is `Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2GRAY)`. I don't know what you mean about showing the image on `View`, though. Could you elaborate? – 1'' Aug 13 '12 at 20:24
  • @user1397061 javacv has examples of showing image on device. Also as I already said there are many examples in WEB. – ArtemStorozhuk Aug 13 '12 at 20:26
  • `Highgui.imwrite("/image.jpg", mat)` does the trick but doesn't actually display the image - it just saves it to a file. JavaCV uses canvas frames, which don't seem to exist anywhere else. Would you mind posting a link to an example with OpenCV4Android? – 1'' Aug 13 '12 at 20:34
  • OpenCV package contains folder `samples`. – ArtemStorozhuk Aug 13 '12 at 20:40
0

While this doesn't deal with the display of the image, here is a quick pure-java static method to read an image by filepath and then convert it (and write it) into grayscale.

/**
 * Get an OpenCV matrix from an image path and write the image as grayscale.
 * @param filePath The image path.
 * @return The matrix.
 */
public static Mat loadOpenCvImage(final String filePath) {
    Mat imgMat = Highgui.imread(filePath, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    if (imgMat == null) {
        Log.e(TAG, "error loading file: " + filePath);
    } else {
        Log.d(TAG, "Ht: " + imgMat.height() + " Width: " + imgMat.width());
        final String outPath = filePath + "_gray.jpg";
        Log.d(TAG, outPath);
        Highgui.imwrite(outPath, imgMat);
    }
    return imgMat;
}
treejanitor
  • 1,249
  • 14
  • 17