Exemple #1
0
        public static void WriteZip(this PixelInt p, string zipfilename, string statusname = "000", bool append = false, CompressionLevel level = CompressionLevel.Fastest)
        {
            XML.PixelModelDependent.ModelDataXML buf = new XML.PixelModelDependent.ModelDataXML();

            buf.Width    = p.Width;
            buf.Height   = p.Height;
            buf.FileName = new List <string> {
                $"^{statusname}.bin$"
            };
            buf.FileSize = -1;
            buf.SetFileType(typeof(PixelInt));
            //buf.SetFileType(p.GetType());

            ZipArchiveMode mode = append ? ZipArchiveMode.Update : ZipArchiveMode.Create;

            using (var archive = ZipFile.Open(zipfilename, mode))
            {
                using (var z = archive.CreateEntry($"PixelsModel\\{statusname}.model", level).Open())
                {
                    Pixels.IO.XMLSetter.Write <PixelModelDependent.ModelDataXML>(buf, z);
                }
                using (var z = archive.CreateEntry($"{statusname}.bin").Open())
                {
                    PixelStream.WriteBin(z, p);
                }
            }
        }
Exemple #2
0
        public static BitmapSource ToDemosaicRGBW3(this PixelInt pix)
        {
            var c = RGBWDemosaicToMat(pix);


            using (var Rmat = new Mat(pix.Height, pix.Width, MatType.CV_8UC1, c[0]))
                using (var Gmat = new Mat(pix.Height, pix.Width, MatType.CV_8UC1, c[1]))
                    using (var Bmat = new Mat(pix.Height, pix.Width, MatType.CV_8UC1, c[2]))
                        using (var Wmat = new Mat(pix.Height, pix.Width, MatType.CV_8UC1, c[3]))
                        {
                            var rgbmat = new Mat(pix.Height, pix.Width, MatType.CV_8UC3);
                            //var Retmat = new Mat();
                            Cv2.Merge(new Mat[]
                            {
                                Bmat, Gmat, Rmat
                            }, rgbmat);

                            var retmat = new Mat();
                            Cv2.CvtColor(rgbmat, retmat, ColorConversionCodes.BGR2HSV_FULL);

                            var splitmat = retmat.Split();
                            Cv2.FastNlMeansDenoising(splitmat[0], splitmat[0], 20);
                            Cv2.FastNlMeansDenoising(splitmat[1], splitmat[1], 20);

                            //Cv2(splitmat[0], splitmat[0],10,35);
                            //Cv2.PyrMeanShiftFiltering(splitmat[1], splitmat[1], 10, 35);

                            //Cv2.FastNlMeansDenoising(Wmat, Wmat);



                            Cv2.Merge(new Mat[]
                            {
                                splitmat[0],
                                splitmat[1],
                                Wmat
                            }, retmat);

                            Cv2.CvtColor(retmat, rgbmat, ColorConversionCodes.HSV2BGR_FULL);

                            splitmat = rgbmat.Split();
                            Cv2.Merge(new Mat[]
                            {
                                splitmat[0] / 1.6,
                                splitmat[1] / 1.6,
                                splitmat[2] / 1.6,
                            }, rgbmat);

                            var buf = rgbmat.ToBitmapSource();
                            buf.Freeze();

                            return(buf);
                        }
        }
Exemple #3
0
 public static BitmapSource ToDemosaicRGBG(this PixelInt pix)
 {
     using (var mat = new Mat(pix.Height, pix.Width, MatType.CV_8UC1, pix.Limits(typeof(byte)).ToPixelByte().Stagger(1).ToArray()))
         using (var retmat = new Mat())
         {
             Cv2.CvtColor(mat, retmat, ColorConversionCodes.BayerGR2BGR);
             var buf = retmat.ToBitmapSource();
             buf.Freeze();
             return(buf);
         }
 }
Exemple #4
0
        public static void WriteZipTxt(this PixelInt p, string zipfilename, string statusname = "000", bool append = false, CompressionLevel level = CompressionLevel.Fastest)
        {
            var sta = new StaPara();

            sta.Width    = p.Width;
            sta.Height   = p.Height;
            sta.filename = statusname + ".txt";
            sta.type     = "txt";

            ZipArchiveMode mode = append ? ZipArchiveMode.Update : ZipArchiveMode.Create;

            using (var archive = ZipFile.Open(zipfilename, mode))
            {
                using (var z = archive.CreateEntry(statusname + ext_status, level).Open())
                    WriteStatus(z, sta);
                using (var z = archive.CreateEntry(sta.filename).Open())
                    PixelStream.WriteTxt(z, p);
            }
        }
Exemple #5
0
        public static BitmapSource ToDemosaicRGBW(this PixelInt pix)
        {
            var c = RGBWDemosaicToMat(pix);


            using (var Rmat = new Mat(pix.Height, pix.Width, MatType.CV_8UC1, c[0]))
                using (var Gmat = new Mat(pix.Height, pix.Width, MatType.CV_8UC1, c[1]))
                    using (var Bmat = new Mat(pix.Height, pix.Width, MatType.CV_8UC1, c[2]))
                    {
                        var rgbmat = new Mat(pix.Height, pix.Width, MatType.CV_8UC3);
                        //var Retmat = new Mat();
                        Cv2.Merge(new Mat[]
                        {
                            Bmat, Gmat, Rmat
                        }, rgbmat);

                        //Cv2.CvtColor(rgbmat, Retmat, ColorConversionCodes.);
                        var buf = rgbmat.ToBitmapSource();
                        buf.Freeze();

                        return(buf);
                    }
        }
Exemple #6
0
        /// <summary>
        /// Returns the actual number on the bitmap box
        /// </summary>
        /// <param name="bm">The bitmap image of a single square</param>
        /// <returns></returns>
        public static int Analyze(Bitmap bm, string nameeOfImage)
        {
            SSC.TakeASnap(bm, nameeOfImage);
            int w = bm.Size.Width;
            int h = bm.Size.Height;

            int orgW = w;

            w = w - 4;      // width itterater

            if (PixelInt.B(bm, w / 3, h / 3) == 250 && PixelInt.B(bm, w / 4, h / 4) == 250 && PixelInt.B(bm, (2 * w) / 5, h / 4) == 250)
            {
                return(0);
            }

            for (; w <= orgW + 4; w += 4)
            {
                for (int x = 14; x < h - 10; x += 7)
                {
                    if (PixelInt.B(bm, w / 2, x) > 180 && PixelInt.R(bm, w / 2, x) > 175 && PixelInt.G(bm, w / 2, x) > 180)
                    {
                        continue;
                    }
                    else if ((int)bm.GetPixel(w / 2, x).B > 180 && (int)bm.GetPixel(w / 2, x).R < 90 && (int)bm.GetPixel(w / 2, x).G < 100)
                    {
                        if ((int)bm.GetPixel(orgW / 3, h / 2).R < 170 && PixelInt.G(bm, orgW / 3, h / 2) < 180) //&& PixelInt.B(bm, orgW / 3, h/2) < 210)
                        {
                            return(-8);                                                                         //Blue unopened square
                        }
                        else
                        {
                            return(1);
                        }
                    }
                    else if ((int)bm.GetPixel(w / 2, x).B < 20 && (int)bm.GetPixel(w / 2, x).R < 50 && (int)bm.GetPixel(w / 2, x).G > 100)
                    {
                        return(2);
                    }
                    else if ((int)bm.GetPixel(w / 2, x).B < 20 && (int)bm.GetPixel(w / 2, x).R > 150 && (int)bm.GetPixel(w / 2, x).G < 20)
                    {
                        if ((int)bm.GetPixel(w / 2 - 4, h / 2).B < 10 && (int)bm.GetPixel(w / 2 - 4, h / 2).R > 150 && (int)bm.GetPixel(w / 2 - 4, h / 2).G < 10)
                        {
                            return(3);
                        }
                        else
                        {
                            return(7);
                        }
                    }
                    else if ((int)bm.GetPixel(w / 2, x).B > 120 && (int)bm.GetPixel(w / 2, x).B < 140 && (int)bm.GetPixel(w / 2, x).R < 10 && (int)bm.GetPixel(w / 2, x).G < 10)
                    {
                        return(4);
                    }
                    else if ((int)bm.GetPixel(w / 2, x).B < 10 && (int)bm.GetPixel(w / 2, x).R > 110 && (int)bm.GetPixel(w / 2, x).R < 140 && (int)bm.GetPixel(w / 2, x).G < 10)
                    {
                        return(5);
                    }
                    else if ((int)bm.GetPixel(w / 2, x).B > 110 && (int)bm.GetPixel(w / 2, x).R < 10 && (int)bm.GetPixel(w / 2, x).G > 110)
                    {
                        return(6);
                    }
                    else if ((int)bm.GetPixel(orgW / 3, x).B > 220 && (int)bm.GetPixel(orgW / 3, x).R < 200)
                    {
                        return(-8);          //Blue unopened square
                    }
                }
            }

            return(0);
        }
Exemple #7
0
        public static BitmapSource RawToHDR(this PixelInt pix, int bitshift, int bitwidth, double RGain, double BGain, double Gamma)
        {
            //ToByteArray

            var buf = new byte[pix.Height * pix.Width];
            int Max = (int)Math.Pow(2, bitwidth);

            //make LUT
            var LUT = new byte[Max];

            for (int i = 0; i < Max; i++)
            {
                LUT[i] = (byte)(Math.Pow((double)i / Max, 1.0 / Gamma) * 255.0);
            }


            for (int y = 0; y < pix.Height; y++)
            {
                for (int x = 0; x < pix.Width; x++)
                {
                    //shift
                    double hoge = (double)(pix[x, y] >> bitshift);

                    //gain
                    switch ((x % 2) + 2 * (y % 2))
                    {
                    case 0:
                        //hoge = hoge;
                        break;

                    case 1:
                        hoge = hoge * RGain;
                        break;

                    case 2:
                        hoge = hoge * BGain;
                        break;

                    case 3:
                        //hoge = hoge;
                        break;
                    }

                    //trim
                    if (hoge > Max - 1)
                    {
                        hoge = Max - 1;
                    }
                    else if (hoge < 0)
                    {
                        hoge = 0;
                    }

                    //LUT
                    buf[x + y * pix.Width] = LUT[(int)hoge];
                }
            }


            //ToOpenCVMat

            using (var rawmat = new Mat(pix.Height, pix.Width, MatType.CV_8UC1, buf))
            {
                var mat_output = new Mat(pix.Height, pix.Width, MatType.CV_8UC3);
                Cv2.CvtColor(rawmat, mat_output, ColorConversionCodes.BayerGB2BGR);

                return(mat_output.ToBitmapSource());
            }
        }
Exemple #8
0
        public static List <byte[]> RGBWDemosaicToMat(PixelInt pix)
        {
            byte[] R = new byte[pix.Size];
            byte[] G = new byte[pix.Size];
            byte[] B = new byte[pix.Size];
            byte[] W = new byte[pix.Size];

            int c11 = 0;
            int c12 = 1;
            int c13 = 2;
            int c21 = pix.Width + 0;
            int c22 = pix.Width + 1;
            int c23 = pix.Width + 2;
            int c31 = 2 * pix.Width + 0;
            int c32 = 2 * pix.Width + 1;
            int c33 = 2 * pix.Width + 2;

            for (int i = 0; i < pix.Size - 2 - 2 * pix.Width; i++)
            {
                int rr = 0;
                int gg = 0;
                int bb = 0;
                int ww = 0;

                int hoge = i % 2 + 2 * ((int)(i / pix.Width) % 2);
                switch (hoge)
                {
                case 0:    //G
                    rr = (pix[c12] + pix[c32]) / 2;
                    gg = (pix[c11] + pix[c13] + pix[c31] + pix[c33]) / 4;
                    bb = (pix[c21] + pix[c23]) / 2;
                    ww = pix[c22];
                    break;

                case 1:    //R
                    gg = (pix[c12] + pix[c32]) / 2;
                    rr = (pix[c11] + pix[c13] + pix[c31] + pix[c33]) / 4;
                    ww = (pix[c21] + pix[c23]) / 2;
                    bb = pix[c22];
                    break;

                case 2:    //B
                    ww = (pix[c12] + pix[c32]) / 2;
                    bb = (pix[c11] + pix[c13] + pix[c31] + pix[c33]) / 4;
                    gg = (pix[c21] + pix[c23]) / 2;
                    rr = pix[c22];
                    break;

                case 3:    //W
                    bb = (pix[c12] + pix[c32]) / 2;
                    ww = (pix[c11] + pix[c13] + pix[c31] + pix[c33]) / 4;
                    rr = (pix[c21] + pix[c23]) / 2;
                    gg = pix[c22];
                    break;
                }
                c11++;
                c12++;
                c13++;
                c21++;
                c22++;
                c23++;
                c31++;
                c32++;
                c33++;

                rr   = (int)(1 * rr * 1);
                bb   = (int)(1 * bb * 1);
                gg   = (int)(1 * gg * 1);
                ww   = (int)(1 * ww * 1);
                R[i] = (byte)(rr > 255 ? 255 : rr < 0 ? 0 : rr);
                G[i] = (byte)(gg > 255 ? 255 : gg < 0 ? 0 : gg);
                B[i] = (byte)(bb > 255 ? 255 : bb < 0 ? 0 : bb);
                W[i] = (byte)(ww > 255 ? 255 : ww < 0 ? 0 : ww);
            }

            return(new List <byte[]> {
                R, G, B, W
            });
        }