繁体   English   中英

显示图像并转换为灰度 - OpenCV for Android,Java API

[英]Displaying image and converting to grayscale - OpenCV for Android, Java API

我在Eclipse中编写了一个使用OpenCV4Android API的Android应用程序。 如何轻松显示Mat图像,仅用于调试? 在C ++中,根据OpenCV教程,您可以执行以下操作:

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

但是Java API for Android似乎没有在org.opencv.highgui.Highgui使用namedWindow函数。

另外,我想将图像加载为灰度。 在C ++中,根据imread不在Opencv中工作 ,你会做:

imread("blackandwhite.jpg", 0);

但Java API的Highgui.imread()只有filename参数。

摘要:

将图像转换为灰度: Imgproc.cvtColor(image, image, Imgproc.COLOR_BGR2GRAY);

显示图片:见此处此处

这是一个示例代码,它将使用OpenCV在imageView中显示图像(必须具有可绘制文件夹中的图像):

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

和xml必须具有如下ImageView:

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

希望这有助于解决您的问题..

但是Java API for Android似乎没有在org.opencv.highgui.Highgui中使用namedWindow函数。

因为您必须在View上显示图像。 看看来自WEB的样本。

另外,我想将图像加载为灰度。

使用cvCvtColor代码CV_BGR2GRAY进行此类转换。

虽然这不涉及图像的显示,但这里有一个快速的纯java静态方法,用于通过文件路径读取图像,然后将其转换(并将其写入)灰度。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM