现在的位置: 首页 > 综合 > 正文

为OpenCV产生正样本描述文件写的一个小程序

2013年12月03日 ⁄ 综合 ⁄ 共 1212字 ⁄ 字号 评论关闭
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GeneratePOSInfo
{
    class Program
    {
        static string getLine(String fn)
        {
            StringBuilder sb = new StringBuilder();

            System.Drawing.Image img = System.Drawing.Image.FromFile(fn);
            if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg) ||
                img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png) ||
                img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
            {
                sb.Append(fn).Append(" 1 0 0 ");
                sb.Append(img.Width).Append(" ");
                sb.Append(img.Height);
            }

            return sb.ToString();
        }

        /**
         * 功能:为OpenCV产生正样本描述文件
         * 测试环境:VS2008+SP1
         * 备注:负样本描述文件可以用类似“dir /b > neg.txt”这样的命令创建
         * */
        static void Main(string[] args)
        {
            if(args.Length != 2)
            {
                Console.WriteLine("Usage:<正样本图片文件所在的目录> <正样本描述文件全路径>");
                Console.WriteLine("Example:");
                Console.WriteLine("GeneratePOSInfo E:\\OpenCV\\pos E:\\OpenCV\\pos.txt");
                return;
            }

            //允许的文件类型
            string[] exts = new string[3];
            exts[0] = "*.jpg";
            exts[1] = "*.png";
            exts[2] = "*.bmp";           

            //打开文件
            FileStream myFs = new FileStream(args[1], FileMode.Create);
            StreamWriter mySw = new StreamWriter(myFs);

            foreach (string ext in exts)
            {
                string[] files = Directory.GetFiles(args[0], ext);

                foreach (string f in files)
                {
                    string line = getLine(f);
                    if(line.Length>0)
                        mySw.WriteLine(line);//写文件
                }
            }
            mySw.Close();
            myFs.Close();
        }
    }
}

抱歉!评论已关闭.