alphacep / vosk-server

WebSocket, gRPC and WebRTC speech recognition server based on Vosk and Kaldi libraries
Apache License 2.0
882 stars 243 forks source link

Why is the image merge area white #197

Open wizVR-zhangjun opened 1 year ago

wizVR-zhangjun commented 1 year ago

SavedScreen1212 I don't know why two pictures merged, the overlapping area is white I hope the teacher can give me a solution

this my scripts:

private void WarpTriangle(Mat img1, Mat img2, List<Point2f> tri1, List<Point2f> tri2, Point2f[] _points2f) 
    {
        (List<List<Point2f>> inputPoints, List<List<Point2f>> keyPoints) = this.Init(_points2f);
        for (int i = 0; i < inputPoints.Count; i++) //遍历每个点
        {
            tri1 = keyPoints[i];    //一个点就是一个三角形像素
            tri2 = inputPoints[i];

            OpenCvSharp.Rect r1 = Cv2.BoundingRect(tri1);
            OpenCvSharp.Rect r2 = Cv2.BoundingRect(tri2);

            List<Point2f> tri1Cropped = new List<Point2f>(), tri2Cropped = new List<Point2f>();
            List<Point> tri2CroppedInt = new List<Point>();

            for (int ii = 0; ii < 3; ii++)
            {
                tri1Cropped.Add(new Point2f(tri1[ii].X - r1.X, tri1[ii].Y - r1.Y));
                tri2Cropped.Add(new Point2f(tri2[ii].X - r2.X, tri2[ii].Y - r2.Y));

                // fillConvexPoly needs a vector of Point and not Point2f
                tri2CroppedInt.Add(new Point(Mathf.Floor(tri2[ii].X - r2.X), Mathf.Floor(tri2[ii].Y - r2.Y)));
            }

            Mat img1Cropped = new Mat();
            img1Cropped = img1.GetRectSubPix(r1.Size, r1.Center);

            Mat warpMat = Cv2.GetAffineTransform(tri1Cropped, tri2Cropped);
            Mat img2Cropped = Mat.Zeros(size, size, img1Cropped.Type());

            Cv2.WarpAffine(img1Cropped, img2Cropped, warpMat, img2Cropped.Size(), InterpolationFlags.Linear, BorderTypes.Reflect101);

            Mat mask = Mat.Zeros(size, size, MatType.CV_32FC3);
            Cv2.FillConvexPoly(mask, tri2CroppedInt, new Scalar(1.0, 1.0, 1.0), LineTypes.AntiAlias, 0);
            Cv2.Multiply(img2Cropped, mask, img2Cropped);
            //Mat matOut = Mat.Zeros(new Size(size, size), img1.Type());
            //Cv2.Add(matOut, img2Cropped, matOut);

            float tx = tri2.Min(x => x.X);
            float ty = tri2.Min(y => y.Y);
            float[] warp_values = { 1.0f, 0.0f, tx, 0.0f, 1.0f, ty };   //图片偏移
            Mat translation_matrix = new Mat(2, 3, MatType.CV_32F, warp_values);
            Cv2.WarpAffine(img2Cropped, img2Cropped, translation_matrix, new Size(size, size));
            Cv2.Multiply(img2Cropped, new Scalar(1.0, 1.0, 1.0) - mask, img2Cropped);

            Cv2.Add(img2, img2Cropped, img2);

            //float tx = tri2.Min(x => x.X);
            //float ty = tri2.Min(y => y.Y);
            //create the translation matrix using tx and ty
            //float[] warp_values = { 1.0f, 0.0f, tx, 0.0f, 1.0f, ty };   //图片偏移
            //Mat translation_matrix = new Mat(2, 3, MatType.CV_32F, warp_values);
            //Cv2.WarpAffine(matOut, matOut, translation_matrix, new Size(size, size));
            //Cv2.Add(img2, matOut, img2);
        }
        Cv2.Flip(img2, img2, FlipMode.X);   //图片翻转
        Cv2.ImShow("outPut", img2);
        //img2.ConvertTo(img2, MatType.CV_8UC3, 255.0);
        //Texture2D _tex = Unity.MatToTexture(img2);
        //byte[] bytes = _tex.EncodeToPNG();
        //System.IO.File.WriteAllBytes(Application.streamingAssetsPath + "/Texture/SavedScreen.png", bytes);

        Debug.Log("输出图像");
    }
nshmyrev commented 1 year ago

Dear @wizVR-zhangjun, you posted image to the wrong project it seems