【问题标题】:Save images to folder using Emgu CV使用 Emgu CV 将图像保存到文件夹
【发布时间】:2019-12-25 22:29:59
【问题描述】:

我有一些使用Emgu CV 从其他图像中提取的图像。

我已尝试将这些图像保存到文件夹中,但只有最后一张图像与我的代码一起保存到文件夹中。

 Image.ToBitmap().save("filename",imageformat.png)

如何将所有图像保存在一个文件夹中?

【问题讨论】:

  • 看起来您正在覆盖文件。你应该每次都使用不同的文件名.....
  • 但是我已经将提取表分配给图像。不知道如何在一张图像中处理表。所以如何一一迭代它们并给出不同的文件名
  • 修复了一些有问题的语法问题以提高可读性

标签: c# emgucv


【解决方案1】:

此答案适用于 WinForms,但相同的原则应适用于所有 UI 框架。

如果您希望能够在运行时选择文件夹,那么您可以使用以下代码:

 void Unique(Image input)
    {
        string fileName = "filename.jpg";
        string newFileName = null;

        //Crates the dialog window
        var dirDialog = new FolderBrowserDialog();
        if (dirDialog.ShowDialog() == DialogResult.OK)
        {
            newFileName = dirDialog.SelectedPath + fileName;

            for (int i = 1; true; i++)
            {


                //this is so that you can alter the name and keep the file format 
                newFileName = fileName.Split('.')[0] + "_{i}" + fileName.Split('.')[1];

                if (!File.Exists(newFileName))
                {
                    break;
                }

            }
            //save the file
            new Bitmap(input).Save(newFileName, ImageFormat.Png);
        }

        //deletes the dialog window from memory
        dirDialog.Dispose();
    }

请记住,每次您要保存文件时,此代码都会要求您提供文件夹。因此,如果您要一次保存多个文件,我建议您将 dirDialog.SelectedPath 保存在某个 string 变量中。

【讨论】:

    【解决方案2】:

    你可以尝试做这样的事情。我已经取了文件的名称,我在询问该文件是否已经存在,如果存在,它会在文件名中添加数字,并且它以保持文件格式的方式进行。但是,如果您不想以文件格式保存它,您可以不使用fileName.Split();,只需在文件名后面添加数字newFileName = fileName+$"_{i}";

    //if you want you can use this method or just copy the code you need
    void Unique(Image input)
    {
        string fileName = "filename.jpg";
        string newFilename = null;
        for(int i = 1; true; i++)
    
            //this is so that you can alter the name and keep the file format 
            newFileName = filename.Split('.')[0]+"_{i}"+filename.Split('.')[1];
    
            if(!File.Exist(newFileName))
            {   
                break;
            }
        }
        return Image.ToBitmap().Save(newFileName,ImageFormat.Png);
    } 
    

    【讨论】:

    • 如何在此处添加文件夹以将图像保存到文件夹中
    • @SrnuReddy 您希望文件夹始终保持不变,还是希望能够在运行时选择文件夹?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多