我已经使用 CvCameraViewListener2
实现了一个摄像头,当我执行 imwrite 时,图片保存变成蓝色
这是我的代码
public Mat onCameraFrame(final CvCameraViewFrame inputFrame) {
img_rgb = inputFrame.rgba();
return img_rgb;
}
public void captureImage(View v){
Mat mInter= new Mat(img_rgb.width(),img_rgb.height(),CvType.CV_32FC3);
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String filename = "temp.jpg";
File file = new File(path, filename);
Boolean bool = null;
filename = file.toString();
if(img_rgb.height() > img_rgb.width()){
Core.flip(img_rgb,mInter,1);
bool = Imgcodecs.imwrite(filename, mInter);
}
else{
bool = Imgcodecs.imwrite(filename,mInter);}
if (bool == true)
Log.i(TAG, "SUCCESS writing image to external storage");
else
Log.i(TAG, "Fail writing image to external storage");
String PathName = "";
Intent i = new Intent(getApplication(),Result.class);
i.putExtra(EXTRA_MESSAGE ,filename);
startActivity(i);
结果:
我找不到问题,我尝试将垫子转换成其他颜色但没有结果。
最佳答案
根据翻转功能的文档:http://docs.opencv.org/java/2.4.2/org/opencv/core/Core.html#flip(org.opencv.core.Mat , org.opencv.core.Mat, 整数) mInter 应与 img_rgb 具有相同的类型,其类型为 CvType.CV_32FC4。 所以正确的初始化将是
Mat mInter = new Mat(img_rgb.width(), img_rgb.height(), CvType.CV_32FC4);
如果 if(img_rgb.height() > img_rgb.width()) 为假,mInter 包含空图像。所以你应该保存 img_rgb :
else bool = Imgcodecs.imwrite(filename, img_rgb);
Imwrite 文档 http://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html :
Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function. If the format, depth or channel order is different, use Mat::convertTo() , and cvtColor() to convert it before saving.
因此您需要使用 convert 将其从 rgba 转换为 bgr:
cv::cvtColor (input, input_bgr, CV_RGBA2BGR)
- img_rgb 可以从不同的线程使用,所以应该添加同步。
关于java - OpenCV Android imwrite 给我一个蓝色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36402088/