我正在尝试将 OpenCV 图像保存到硬盘。
这是我尝试过的:
public void SaveImage (Mat mat) {
Mat mIntermediateMat = new Mat();
Imgproc.cvtColor(mRgba, mIntermediateMat, Imgproc.COLOR_RGBA2BGR, 3);
File path =
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
String filename = "barry.png";
File file = new File(path, filename);
Boolean bool = null;
filename = file.toString();
bool = Highgui.imwrite(filename, mIntermediateMat);
if (bool == true)
Log.d(TAG, "SUCCESS writing image to external storage");
else
Log.d(TAG, "Fail writing image to external storage");
}
}
任何人都可以展示如何使用 OpenCV 2.4.3 保存该图像吗?
最佳答案
你的问题有点令人困惑,因为你的问题是关于桌面上的 OpenCV,但你的代码是针对 Android 的,你问的是 IplImage,但你发布的代码使用的是 C++ 和 Mat。假设您在使用 C++ 的桌面上,您可以执行以下操作:
cv::Mat image;
std::string image_path;
//load/generate your image and set your output file path/name
//...
//write your Mat to disk as an image
cv::imwrite(image_path, image);
...或者更完整的示例:
void SaveImage(cv::Mat mat)
{
cv::Mat img;
cv::cvtColor(...); //not sure where the variables in your example come from
std::string store_path("..."); //put your output path here
bool write_success = cv::imwrite(store_path, img);
//do your logging...
}
图像格式是根据提供的文件名选择的,例如如果您的 store_path
字符串是“output_image.png”,那么 imwrite 会将其保存为 PNG 图像。您可以在 the OpenCV docs 查看有效扩展列表。 .
使用 OpenCV 将图像写入磁盘时需要注意的一个注意事项是缩放比例会因 Mat 类型而异;也就是说,对于 float ,图像预计在 [0, 1] 范围内,而对于无符号字符,它们将来自 [0, 256)。
对于 IplImages,我建议只切换到使用 Mat,因为旧的 C 接口(interface)已被弃用。您可以通过 cvarrToMat
将 IplImage 转换为 Mat,然后使用 Mat,例如
IplImage* oldC0 = cvCreateImage(cvSize(320,240),16,1);
Mat newC = cvarrToMat(oldC0);
//now can use cv::imwrite with newC
或者,您可以将 IplImage 转换为 Mat
Mat newC(oldC0); //where newC is a Mat and oldC0 is your IplImage
另外我刚刚注意到this tutorial在 OpenCV 网站上,它为您提供了在(桌面)环境中加载和保存图像的演练。
关于opencv - 如何在 OpenCV 2.4.3 中保存(cvWrite 或 imwrite)图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16069407/