cronz / cnlite_cs

cnlite is a c# library to do chinese ocr based on project chineseocr_lite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

短边自适应

blyema opened this issue · comments

image
private Tensor GetInputs(string imageFilePath, Mat rgbMat, out int new_h, out int new_w)
{

        // Read image
        Mat imageMat = CvInvoke.Imread(imageFilePath, ImreadModes.Color);
        CvInvoke.CvtColor(imageMat, rgbMat, ColorConversion.Bgr2Rgb);
        imageMat.Dispose();

        //取图片最小的边做为短边
        int short_size = rgbMat.Height < rgbMat.Width ? rgbMat.Height : rgbMat.Width;
        //测试发现如果短边小于96,会造成识别不出来或者识别不完全,要保证短边不小于96
        if (short_size < 96)
        {
            short_size = 96;
        }
        //短边 取整为32的倍数
        short_size = 32 * (short_size / 32);

        double scale_h = 0, tar_w = 0, scale_w = 0, tar_h = 0;

        if (rgbMat.Height < rgbMat.Width)
        {
            scale_h = short_size * 1.0 / rgbMat.Height;
            tar_w = rgbMat.Width * scale_h * 1.0;
            tar_w = tar_w - tar_w % 32;
            tar_w = Math.Max(32, tar_w);
            scale_w = tar_w / rgbMat.Width;
        }
        else
        {
            scale_w = short_size * 1.0 / rgbMat.Width;
            tar_h = rgbMat.Height * scale_w * 1.0;
            tar_h = tar_h - tar_h % 32;
            tar_h = Math.Max(32, tar_h);
            scale_h = tar_h / rgbMat.Height;
        }

        new_h = (int)(scale_h * rgbMat.Height);
        new_w = (int)(scale_w * rgbMat.Width);

        Mat imgResized = new Mat(new_h, new_w, DepthType.Cv32F, 3);
        CvInvoke.Resize(rgbMat, imgResized, new Size(new_w, new_h));

        return GetTensorInputFromImg(imgResized);
    }