gpt4 book ai didi

java - OpenCV Android imwrite 给我一个蓝色图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:10 24 4
gpt4 key购买 nike

我已经使用 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);

结果:

enter image description here

我找不到问题,我尝试将垫子转换成其他颜色但没有结果。

最佳答案

  1. 根据翻转功能的文档: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);
  2. 如果 if(img_rgb.height() > img_rgb.width()) 为假,mInter 包含空图像。所以你应该保存 img_rgb :

    else bool = Imgcodecs.imwrite(filename, img_rgb);
  3. 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)
  1. img_rgb 可以从不同的线程使用,所以应该添加同步。

关于java - OpenCV Android imwrite 给我一个蓝色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36402088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com