显示图像并转换为灰度-OpenCV for Android,Java API
我正在Eclipse中编写一个使用API的Android应用程序。如何仅为调试而轻松显示显示图像并转换为灰度-OpenCV for Android,Java API,java,android,image,opencv,Java,Android,Image,Opencv,我正在Eclipse中编写一个使用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. 但是Android的JavaAPI在org.
Mat
图像?在C++中,根据OpenCV教程,你会做一些类似的事情:
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
但是Android的JavaAPI在org.opencv.highgui.highgui
中似乎没有namedWindow
函数
另外,我想加载灰度图像。在C++中,根据,你会做:
imread("blackandwhite.jpg", 0);
但是JavaAPI的Highgui.imread()
只有filename参数
但是Android的JavaAPI似乎没有名字
函数位于org.opencv.highgui.highgui中
因为您必须在视图上显示图像。请看一看来自WEB的示例
另外,我想加载灰度图像
/**
* 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;
}
将cvtcolor
与codeCV\u bgr2 gray
一起用于此类转换。摘要:
将图像转换为灰度:Imgproc.cvtColor(图像,图像,Imgproc.COLOR\u 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静态方法,可以通过文件路径读取图像,然后将其转换(并写入)为灰度
/**
* 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;
}
我没有使用JavaCV,它是OpenCV的第三方包装。相反,我使用的是针对Android的OpenCV,它的语法与C++/JavaCV不同。感谢您提供有关cvtColor的提示-这让我找到了灰度转换的解决方案,即Imgproc.cvtColor(mat,mat,Imgproc.COLOR\u RGB2GRAY)
。不过,我不知道你在视图上显示图像是什么意思。您能详细说明一下吗?@user1397061 javacv提供了在设备上显示图像的示例。同样,正如我已经说过的,WEB中有很多例子。Highgui.imwrite(“/image.jpg”,mat)
完成了这个技巧,但实际上并没有显示图像——它只是将图像保存到一个文件中。JavaCV使用画布框架,这似乎在其他任何地方都不存在。您介意发布一个指向OpenCV4Android示例的链接吗?OpenCV包包含文件夹samples
。