예제 #1
0
 public static double average(Matrix1 img)
 {
     int nd, nc; nd = img.NoRows; nc = img.NoCols;
     double tt = 0;
     for (int i = 0; i < nd; i++)
         for (int j = 0; j < nc; j++)
         {
             tt += img[i, j];
         }
     return tt/nd/nc;
 }
예제 #2
0
 public Matrix1(Matrix1 Mat)
 {
     //this.in_Mat = (double[,])Mat.Clone();
     int m, n;
     m = Mat.NoRows;
     n = Mat.NoCols;
     double[,] tem = new double[m, n];
     for (int i = 0; i < m; i++)
         for (int j = 0; j < n; j++)
             tem[i, j] = Mat[i, j];
     this.in_Mat = (double[,])tem.Clone();
 }
예제 #3
0
        public static double biMatch(Matrix1 imgt, Matrix1 imgq)
        {
            int nd, nc; nd = imgq.NoRows; nc = imgq.NoCols;
            int nd1, nc1; nd1 = imgq.NoRows-1; nc1 = imgq.NoCols-1;
            double gt, gq; int dt, dq;
            gt = gq = 0;  double d=0;
            dt = dq = 0;
            double kch = 0;
            for (int i = 1; i < nd1; i++)
                for (int j = 1; j < nc1; j++)
                {

                    d = 0;
                    if (imgt[i, j] == 1)
                    {
                        d += imgq[i, j] +imgq[i - 1, j] + imgq[i + 1, j] + imgq[i, j - 1] + imgq[i, j + 1];
                        d += imgq[i - 1, j-1] + imgq[i + 1, j+1] + imgq[i+1, j - 1] + imgq[i-1, j + 1];
                        if (d > 0) gt++;
                        dt++;
                    }
                    d = 0;
                    if (imgq[i, j] == 1)
                    {
                        d += imgt[i, j] +imgt[i - 1, j] + imgt[i + 1, j] + imgt[i, j - 1] + imgt[i, j + 1];
                        d += imgt[i - 1, j - 1] + imgt[i + 1, j + 1] + imgt[i + 1, j - 1] + imgt[i - 1, j + 1];
                        if (d > 0) gq++;
                        dq++;
                    }
                    //if (imgt[i, j] != imgq[i, j]) kch++;
                    //if (imgt[i, j] == 1 || imgq[i, j] == 1) d++;

                }

            gt = gt / dt; gq = gq / dq;

            return (gt+gq)/2;
        }
예제 #4
0
        public static double biMatch1(Matrix1 imgt, Matrix1 imgq)
        {
            int nd, nc; nd = imgq.NoRows; nc = imgq.NoCols;
            int nd1, nc1; nd1 = imgq.NoRows - 1; nc1 = imgq.NoCols - 1;
            double gt, gq; int dt, dq;
            gt = gq = 0; double d = 0; double d1 = 0;
            dt = dq = 0;
            double kch = 0;
            for (int i = 1; i < nd1; i++)
                for (int j = 1; j < nc1; j++)
                {

                    d = 0;
                    d1 = 0;
                    if (imgt[i, j] == 1)
                    {
                        d += imgq[i, j] + imgq[i - 1, j] + imgq[i + 1, j] + imgq[i, j - 1] + imgq[i, j + 1];
                        d += imgq[i - 1, j - 1] + imgq[i + 1, j + 1] + imgq[i + 1, j - 1] + imgq[i - 1, j + 1];

                        if (d == 0) continue;
                        d1 += imgt[i, j] + imgt[i - 1, j] + imgt[i + 1, j] + imgt[i, j - 1] + imgt[i, j + 1];
                        d1 += imgt[i - 1, j - 1] + imgt[i + 1, j + 1] + imgt[i + 1, j - 1] + imgt[i - 1, j + 1];

                       // if (d1/d > 0) gt++;
                        //dt++;
                        gt += (d1 - d)/9;
                        dt++;
                    }
                    //if (imgt[i, j] != imgq[i, j]) kch++;
                    //if (imgt[i, j] == 1 || imgq[i, j] == 1) d++;

                }

            gt = gt / dt;

            return 1 - gt;
        }
예제 #5
0
 internal static void Eigen(Matrix1 A, double[,] eig_val, double[,] eig_vec)
 {
     throw new NotImplementedException();
 }
예제 #6
0
        public static double MatchO(Matrix1 imgt, int x, int y, Matrix1 imgq, int u, int v)
        {
            double gt;
            gt = 0;
            double gq;
            gq = 0;
            int w = 10;
            for (int i = -w; i < w; i += 2)
                for (int j = -w; j < w; j += 2)
                {
                    //tinh khop tu anh t
                    if (imgq[u + i, v + j] == imgt[x + i, y + j]
                        ||
                        imgq[u + i - 1, v + j] == imgt[x + i, y + j] ||
                        imgq[u + i + 1, v + j] == imgt[x + i, y + j] ||
                        imgq[u + i, v + j - 1] == imgt[x + i, y + j] ||
                        imgq[u + i, v + j + 1] == imgt[x + i, y + j]
                        )
                    {

                        gt++;
                    }
                    // if (imgt[u+i, v+j] == imgt[x+i, y+j]        ||
                    //    imgt[u + i - 1, v + j] == imgq[x + i, y + j] ||
                    //    imgt[u + i + 1, v + j] == imgq[x + i, y + j] ||
                    //    imgt[u + i, v + j - 1] == imgq[x + i, y + j] ||
                    //    imgt[u + i, v + j + 1] == imgq[x + i, y + j])
                    //{
                    //    gq++;
                    //}

                }
            //gt = Math.Max(gt,gq) / w/w;
            gt = gt / w / w;

            return gt;
        }
예제 #7
0
        public static double biMatchO(Matrix1 imgt, Matrix1 imgq,Matrix1 dirt,Matrix1 dirq)
        {
            int nd, nc; nd = imgq.NoRows; nc = imgq.NoCols;
            int nd1, nc1; nd1 = imgq.NoRows - 1; nc1 = imgq.NoCols - 1;
            double gt, gq; int dt, dq;
            gt = gq = 0; double d = 0;
            dt = dq = 0;
               // double kch = 0;
            for (int i = 1; i < nd1; i++)
                for (int j = 1; j < nc1; j++)
                {
                    if (imgt[i, j] == 255 || imgq[i, j] == 255) continue;
                    d = 0;
                    if (imgt[i, j] == 1)
                    {
                        dt++;
                        if (dirq[i, j] * dirt[i, j] < 0) continue;
                        //if (Math.Abs(dirt[i, j] - dirq[i, j]) > 45) continue;

                            d += imgq[i, j] + imgq[i - 1, j] + imgq[i + 1, j] + imgq[i, j - 1] + imgq[i, j + 1];
                        if (d > 0) gt++;

                    }
                    d = 0;
                    if (imgq[i, j] == 1)
                    {
                        dq++;
                        if (dirq[i, j] * dirt[i, j] < 0) continue;
                        //if (Math.Abs(dirt[i, j] - dirq[i, j]) > 45) continue;

                            d += imgt[i, j] + imgt[i - 1, j] + imgt[i + 1, j] + imgt[i, j - 1] + imgt[i, j + 1];
                        if (d > 0) gq++;

                    }
                    //if (imgt[i, j] != imgq[i, j]) kch++;
                    //if (imgt[i, j] == 1 || imgq[i, j] == 1) d++;

                }
            //gt = gt / dt; gq = gq / dq;
            gt = gt / dq; gq = gq / dt;
            //if (gt < gq)
            //    return gt;
            //return gq;
            return (gt + gq) / 2;
            //return 1 - kch / d;// nd1 / nc1;
        }
예제 #8
0
        // String file_name)
        //in: image name
        //out: 1D vector cot
        public static Matrix1 image_2_matrix(Bitmap bmp1)
        {
            //tao anh bitmap tam
            Bitmap bmp = bmp1;// new Bitmap(file_name);

            //khai bao ma tran anh

            Matrix1 matrananhgoc = new Matrix1(bmp.Height, bmp.Width);

            BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            int stride = bmData.Stride;
            int nOffset = stride - bmp.Width * 3;
            //nOffset chính là cái rìa của bức ảnh, khi con trỏ xử lý đến pixel cuối cùng của hàng, thì muốn xuống
            //hàng kế tiếp, ta phải bỏ qua cái rìa này bằng cách cộng thêm địa chỉ con trỏ với nOffset
            unsafe
            {
                byte* p = (byte*)bmData.Scan0;
                //p sẽ trỏ đến địa chỉ đẩu của ảnh
                int x, y;
                for (y = 0; y < bmp.Height; y++)
                {
                    for (x = 0; x < bmp.Width; x++)
                    {

                        matrananhgoc[y, x] = (int)p[0];
                        //Chuyển con trỏ sang pixel kế tiếp
                        p += 3; // 2 pixel kế tiếp cách nhau 3 bytes
                    }//Xử lý xong 1 hàng
                    //Chuyển con trỏ xuông hàng kế tiếp
                    p += nOffset;
                }
            }
            bmp.UnlockBits(bmData);//giải phóng biến BitmapData
            return matrananhgoc;
        }
예제 #9
0
 //in: ma tran cot ban dau va vector cot mean
 //out: ma tran cot ma moi cot da tru di vector cot
 public static Matrix1 differ_matrix(Matrix1 mat, double[] mean)
 {
     Matrix1 res = new Matrix1(mat.NoRows, mat.NoCols);
     for (int i = 0; i < mat.NoRows; i++)
         for (int j = 0; j < mat.NoCols; j++)
             res[i, j] = mat[i, j] - mean[i];
     return res;
 }
예제 #10
0
 /// <summary>
 /// Returns the Eigenvalues and Eigenvectors of a real symmetric
 /// Matrix1, which is of dimensions [n,n]. In case of an error the
 /// error is raised as an exception.
 /// Note: This method is based on the 'Eigenvalues and Eigenvectors of a TridiagonalMatrix1'
 /// section of Numerical Recipes in C by William H. Press,
 /// Saul A. Teukolsky, William T. Vetterling and Brian P. Flannery,
 /// University of Cambridge Press 1992.
 /// </summary>
 /// <param name="Mat">
 /// The Matrix1 object whose Eigenvalues and Eigenvectors are to be found
 /// </param>
 /// <param name="d">A Matrix1 object where the eigenvalues are returned</param>
 /// <param name="v">A Matrix1 object where the eigenvectors are returned</param>
 public static void Eigen(Matrix1 Mat, out Matrix1 d, out Matrix1 v)
 {
     double[,] D, V;
     Eigen(Mat.in_Mat, out D, out V);
     d = new Matrix1(D);
     v = new Matrix1(V);
 }
예제 #11
0
 /// <summary>
 /// Returns the dot product of two vectors whose
 /// dimensions should be [3] or [3,1].
 /// In case of an error the error is raised as an exception.
 /// </summary>
 /// <param name="V1">First Matrix1 object (dimension [3,1]) in the dot product</param>
 /// <param name="V2">Second Matrix1 object (dimension [3,1]) in the dot product</param>
 /// <returns>Dot product of V1 and V2</returns>
 public static double DotProduct(Matrix1 V1, Matrix1 V2)
 {
     return (DotProduct(V1.in_Mat, V2.in_Mat));
 }
예제 #12
0
 /// <summary>
 /// Returns the determinant of a Matrix1 with [n,n] dimension.
 /// In case of an error the error is raised as an exception. 
 /// </summary>
 /// <param name="Mat">
 /// Matrix1 object with [n,n] dimension whose determinant is to be found
 /// </param>
 /// <returns>Determinant of the Matrix1 object</returns>
 public static double Det(Matrix1 Mat)
 {
     return Det(Mat.in_Mat);
 }
예제 #13
0
 /// <summary>
 /// Returns the cross product of two vectors whose
 /// dimensions should be [3] or [3x1].
 /// In case of an error the error is raised as an exception.
 /// </summary>
 /// <param name="V1">First Matrix1 (dimensions [3,1]) in the cross product</param>
 /// <param name="V2">Second Matrix1 (dimensions [3,1]) in the cross product</param>
 /// <returns>Cross product of V1 and V2 as a Matrix1 (dimension [3,1])</returns>
 public static Matrix1 CrossProduct(Matrix1 V1, Matrix1 V2)
 {
     return (new Matrix1((CrossProduct(V1.in_Mat, V2.in_Mat))));
 }
예제 #14
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="matrix1s">danh sách ma trận</param>
        /// <param name="labels">danh sách tên của ma trận</param>
        /// <param name="mtthem">ma trận truyền vào để so sánh</param>
        /// <param name="max"> (out)giá trị lớn nhất của việc so khớp</param>
        /// <param name="name">(out) tên ma trận tìm được giống nhất theo giá trị max</param>
        /// <param name="avg">(out) giá trị trung bình lớn nhất của việc so khớp</param>
        /// <param name="nameAvg">(out)tên ma trận tìm được giống nhất theo giá trị trung bình</param>
        /// <param name="khs">(out)số lượng ma trận tính trung bình</param>
        /// <param name="Index_Maxtrix_Max">(out)ma trận tìm thấy lớn nhất và vị trí ban đầu. =null khi unknown</param>
        public static void Compare(List<Matrix1> matrix1s, List<string> labels, Matrix1 mtthem, out double max, out string name, out double maxavg, out string nameAvg, int khs,out List<double> Index_Maxtrix_Max)
        {
            List<double> Index_Matrix = new List<double>();
            List<List<double>> Index_Maxtrix_All = new List<List<double>>();
            int dem = 0, demy = 0;
            double[] avgs = new double[matrix1s.Count / 10];
            double[] dgt1 = new double[10];
            Index_Maxtrix_Max = null;
            double gt = 0, sum = 0;
            name = ""; nameAvg = "";
            max = 0; maxavg = 0;

            for (int i = 1; i <= matrix1s.Count; i++)
            {
                //dem = 0;
                gt = Match.MatchO(matrix1s[i-1], mtthem);
                sum += gt;
                if (gt.CompareTo(max) == 1)
                {

                    max = gt;
                    name = labels[i-1];
                }
                //them % giong cua 1 ng vao mang 1 chieu
                dgt1[demy] = gt;
                demy++;
                //xét đủ  10 ảnh 1 người trong 1 mảng
                if (i % 10 == 0 & i != 1)
                {
                    Console.WriteLine(matrix1s.Count+"   "+dem.ToString());
                    double sumkhs = sapxep_tinhtrungbinh(dgt1, khs,out Index_Matrix);
                    Index_Maxtrix_All.Add(Index_Matrix);
                    avgs[dem] = sumkhs;
                    //lay trung binh 10 anh 1 nguoi
                    // avgs[dem] = sum / 10;
                    sum = 0;
                    dem++;
                    demy = 0;
                }
            }
            //tim max trong mang cac phan tu trung binh
            for (int i = 0; i < matrix1s.Count / 10; i++)
            {
                if (maxavg < avgs[i])
                {
                    maxavg = avgs[i];
                    nameAvg = labels[i * 10];
                    Index_Maxtrix_Max = Index_Maxtrix_All[i];
                }
            }
            if (max < 0.8) name = "Unknown";
            if (maxavg < 0.7) { nameAvg = "Unknown"; Index_Maxtrix_Max = null; }
        }
예제 #15
0
 /// <summary>
 /// Returns the summation of two matrices with compatible 
 /// dimensions.
 /// In case of an error the error is raised as an exception.
 /// </summary>
 /// <param name="Mat1">First Matrix1 in the summation</param>
 /// <param name="Mat2">Second Matrix1 in the summation</param>
 /// <returns>Sum of Mat1 and Mat2 as a Matrix1 object</returns>
 public static Matrix1 Add(Matrix1 Mat1, Matrix1 Mat2)
 {
     return new Matrix1(Add(Mat1.in_Mat, Mat2.in_Mat));
 }
예제 #16
0
        public static Matrix1 cov_compute2(string[] str_file_name)
        {
            //cac bien local
            int i;
            int n = str_file_name.Length;// so anh huan luyen

            //khai bao ma tran hiep phuong sai
            Matrix1 cov_matrix = new Matrix1(h, w);
            //
            int x, y,k,r;
            Matrix1 tam;
            Matrix1 tam2;
            mt_mean = Matrix1.Transpose(mt_mean);
            double[] col_sum = new double[w];
            for (i = 0; i < ns; i++)
            {
                //chuyen anh thu i thanh ma tran
                Bitmap bmtam = new Bitmap(str_file_name[i].ToString());
                tam = image_2_matrix(bmtam);

                tam = Matrix1.Transpose(tam);

                //tru cho ma tran trung binh
                tam = Matrix1.Subtract(tam, mt_mean);
                tam2=new Matrix1(tam);

                for (k = 0; k < tam2.NoCols; k++)
                {
                    double tong = 0;
                    for ( r = 0; r < tam2.NoRows; r++)
                        tong += tam2[r, k];
                    col_sum[k] = tong;
                }
                for (k = 0; k < tam2.NoCols; k++)
                {
                   // double tong = 0;
                    for (r = 0; r < tam2.NoRows; r++)
                        tam2[r, k] = col_sum[k];
                    //col_sum[k] = tong;
                }
                Matrix1 mat = Matrix1.Multiply(Matrix1.Transpose(tam2), tam);

                cov_matrix = Matrix1.Add(cov_matrix, mat);
            }
            for (y = 0; y < h; y++)
                for (x = 0; x < w; x++)
                    cov_matrix[y, x] /= (sn - 1);

            return cov_matrix;
        }
예제 #17
0
        public static Matrix1 cov_computew()
        {
            //cac bien local
            int i;
            //khai bao ma tran hiep phuong sai
            Matrix1 cov_matrix = new Matrix1(w,w);
            //
            int x, y;
            Matrix1 tam= new Matrix1(w, w);;
            int k = 0;
            for (i = 0; i < ns; i++)
            {

                //tru cho ma tran trung binh
                tam = Matrix1.Subtract(A[i], AT[k]);
                //phan thu nghiem weigh 2DLDA
                //for (y = 0; y < w; y++)
                //    for (x = 0; x < w; x++)
                //        tam[y, x] = (1 + 0.5 / (A[i][y, x] - mt_mean[y, x]) / (A[i][y, x] - mt_mean[y, x]) * hamLoi((A[i][y, x] - mt_mean[y, x]) * 0.5 * Math.Sqrt(2))) * (A[i][y, x] - mt_mean[y, x]);

                Matrix1 mat = Matrix1.Multiply(Matrix1.Transpose(tam), tam);

                cov_matrix = Matrix1.Add(cov_matrix, mat);
                if ((i + 1) % sn == 0)
                {
                    k++;  //xet anh trung binh cua nguoi tiep theo
                }
            }
            for (y = 0; y < w; y++)
            {
                for (x = 0; x < w; x++)
                {
                    cov_matrix[y, x] /= (ns - 1);
                    //cov_matrix[y, x] *= sn;
                    //Console.Write(cov_matrix[y, x] + "  ");
                }
                //Console.WriteLine("");
            }
            //Console.ReadLine();

            return cov_matrix;
        }
예제 #18
0
 public static double Find_Min(Matrix1 Mat1)
 {
     return (Find_Min(Mat1.in_Mat));
 }
예제 #19
0
 //ham tinh khoang cach Euclid giua 2 ma tran
 public static double Euclid_distance_matrix(Matrix1 a, Matrix1 b)
 {
     double sol = 0;
     if (a.NoCols != b.NoCols || a.NoRows != b.NoRows)
     {
         MessageBox.Show("2 ma tran khac so chieu");
         return sol;
     }
     int i, j;
     for (i = 0; i < a.NoRows; i++)
         for (j = 0; j < a.NoCols; j++)
             sol += (a[i, j] - b[i, j]) * (a[i, j] - b[i, j]);
     //sol = Math.Sqrt(sol);
     //sol /= a.NoCols * a.NoRows;
     //sol /= 255;
     return sol;
 }
예제 #20
0
 /// <summary>
 /// Returns the inverse of a Matrix1 with [n,n] dimension 
 /// and whose determinant is not zero.
 /// In case of an error the error is raised as an exception. 
 /// </summary>
 /// <param name="Mat">
 /// Matrix1 object with [n,n] dimension whose inverse is to be found
 /// </param>
 /// <returns>Inverse of the Matrix1 as a Matrix1 object</returns>
 public static Matrix1 Inverse(Matrix1 Mat)
 {
     return new Matrix1(Inverse(Mat.in_Mat));
 }
예제 #21
0
 public static Bitmap matrix_2_image(Matrix1 vector, int h, int w)
 {
     Bitmap bmp = new Bitmap(w, h);
     BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
     int stride = bmData.Stride;
     int nOffset = stride - bmp.Width * 3;
     //nOffset chính là cái rìa của bức ảnh, khi con trỏ xử lý đến pixel cuối cùng của hàng, thì muốn xuống
     //hàng kế tiếp, ta phải bỏ qua cái rìa này bằng cách cộng thêm địa chỉ con trỏ với nOffset
     unsafe
     {
         byte* p = (byte*)bmData.Scan0;
         //p sẽ trỏ đến địa chỉ đẩu của ảnh
         int x, y;
         for (y = 0; y < bmp.Height; y++)
         {
             for (x = 0; x < bmp.Width; x++)
             {
                 //Xử lý 3 byte của pixel
                 p[0] = (byte)vector[y, x];
                 p[1] = (byte)vector[y, x];
                 p[2] = (byte)vector[y, x];
                 //Chuyển con trỏ sang pixel kế tiếp
                 p += 3; // 2 pixel kế tiếp cách nhau 3 bytes
             }//Xử lý xong 1 hàng
             //Chuyển con trỏ xuông hàng kế tiếp
             p += nOffset;
         }
     }
     bmp.UnlockBits(bmData);//giải phóng biến BitmapData
     return bmp;
 }
예제 #22
0
 /// <summary>
 /// Checks if two matrices of equal dimensions are equal or not.
 /// In case of an error the error is raised as an exception.
 /// </summary>
 /// <param name="Mat1">First Matrix1 in equality check</param>
 /// <param name="Mat2">Second Matrix1 in equality check</param>
 /// <returns>If two matrices are equal or not</returns>
 public static bool IsEqual(Matrix1 Mat1, Matrix1 Mat2)
 {
     return IsEqual(Mat1.in_Mat, Mat2.in_Mat);
 }
예제 #23
0
        public static Matrix1 correlate(Matrix1 imgq,Matrix1 imgt)
        {
            int nd, nc; nd = imgq.NoRows; nc = imgq.NoCols;
            Matrix1 imgm = new Matrix1(nd,nc);
            double tt, qt;
            int n = 2; int yi, xj; int nt=25;
            for(int i=0;i<nd;i++)
                for (int j = 0; j < nc; j++)
                {
                    tt = 0; qt = 0;
                    for (int ii = -n; ii <= n; ii++)
                        for (int jj = -n; jj <= n; jj++)
                        {
                            yi = i + ii; xj = j + jj;
                            if (yi < 0 || yi >= nd || xj < 0 || xj >= nc) continue;
                            tt += imgt[yi, xj]; qt += imgq[yi, xj];
                        }
                    tt = tt / nt; qt = qt /nt;

                    imgm[i,j]=correlate1p(i, j, imgq, imgt,qt,tt,n);
                }
            return imgm;
        }
예제 #24
0
 public void add(Matrix1 mat)
 {
     mt_eigen_vec[n] = new Matrix1(mat);
     n++;
 }
예제 #25
0
 public static double correlate1p(int y,int x,Matrix1 imgq, Matrix1 imgt,double qt,double tt,int n)
 {
     double c = 0;double tc,mc1,mc2; tc=0;mc1=0;mc2=0;
     int i,j;
     for (int ii = -n; ii <= n; ii++)
     {
         i = ii + y;
         if (i < 0 || i >= imgq.NoRows) continue;
         for (int jj = -n; jj <= n; jj++)
         {
             j = jj + x;
             if (j < 0 || j >= imgq.NoCols) continue;
             tc += (imgt[i, j] - tt) * (imgq[i, j] - qt);
             mc1 += (imgt[i, j] - tt) * (imgt[i, j] - tt);
             mc2 += (imgq[i, j] - qt) * (imgq[i, j] - qt);
         }
     }
     if (mc1 == 0 || mc2 == 0) return -1;
     c = tc / Math.Sqrt(mc1 * mc2);
     return c;
 }
예제 #26
0
        public static Matrix1 apDungGabor(Matrix1 img, double goc, double tanso)
        {
            Matrix1 image = new Matrix1(img);
            Matrix1 image2 = new Matrix1(img);

            //luc tinh ham gabor
            int wgs = 5;
            double[,] gV = new double[11, 11];
            //tinh toan ham gabor su dung mat na wg x wg
            for (int v = -wgs; v <= wgs; v++)
                for (int u = -wgs; u <= wgs; u++)
                    gV[v + wgs, u + wgs] = gabor(v, u, goc, tanso);

            //luc ap dung cho tung pixel
            double sum;
            int dx, dy, du, dv;
            dx = 0;
            dy = 0;
            du = 0;
            dv = 0;
            int ndong, ncot;
            ndong = image.NoRows; //-wgs;
            ncot = image.NoCols;// -wgs;
            int d, x, y;
            for (x = 0; x < ndong; x++)
            {
                for (y = 0; y < ncot; y++)
                {
                    //xem xet tung pixel(x,y)
                    //su dung mat na wg x wg
                    sum = 0;
                    d = 0;
                    for (int v = -wgs; v <= wgs; v++)
                    {
                        dv = x + v;
                        if (dv >= ncot || dv < 0)
                        {
                            d = d + 2 * wgs + 1;
                            continue;
                        }
                        for (int u = -wgs; u <= wgs; u++)
                        {
                            du = y + u;
                            if (du >= ndong || du < 0)
                            {
                                d++;
                                continue;
                            }
                            sum += (int)(gV[v + wgs, u + wgs] * image2[dv, du]);
                            d++;
                        }
                    }
                    sum = sum / d;

                    //gan lai muc xam cho anh
                    image[x, y] = sum;
                    //image[x, y] = sum;
                }
            }

            //Bitmap res = new Bitmap(PCA.matrix_2_image(image, image.NoRows, image.NoCols));

            return image;
        }
예제 #27
0
        public static double MatchO(Matrix1 imgt, Matrix1 imgq)
        {
            int w = 1;// 2;
            int nd, nc; nd = imgq.NoRows; nc = imgq.NoCols;
            int nd1, nc1; nd1 = imgq.NoRows - w - 1; nc1 = imgq.NoCols - w - 1;
            double gt, gq; double dt, dq;
            gt = gq = 0; double d = 0;
            dt = dq = 0;
            double kch = 0; double gtq = 0;
            int u, v; u = 0; v = 0;
            //so khop 8 anh trung tam
            double m0, m1, m2, m3, m4, m5, m6, m7, m8;

            int xc, yc; xc = nd / 2 + w; yc = nc / 2 + w;

            m0 = MatchO(imgt, xc, yc, imgq, xc, yc);
            m1 = MatchO(imgt, xc, yc, imgq, xc - w, yc);
            m2 = MatchO(imgt, xc, yc, imgq, xc + w, yc);
            m3 = MatchO(imgt, xc, yc, imgq, xc - w, yc - w);
            m4 = MatchO(imgt, xc, yc, imgq, xc, yc - w);
            m5 = MatchO(imgt, xc, yc, imgq, xc + w, yc - w);
            m6 = MatchO(imgt, xc, yc, imgq, xc - w, yc + w);
            m7 = MatchO(imgt, xc, yc, imgq, xc, yc + w);
            m8 = MatchO(imgt, xc, yc, imgq, xc + w, yc + w);
            double mm;
            mm = m0;
            int h = 0;
            if (m1 > mm) { mm = m1; u = -w; v = 0; }
            if (m2 > mm) { mm = m2; u = w; v = 0; }
            if (m3 > mm) { mm = m3; u = -w; v = -w; }
            if (m4 > mm) { mm = m4; u = 0; v = -w; }
            if (m5 > mm) { mm = m5; u = w; v = -w; }
            if (m6 > mm) { mm = m6; u = -w; v = w; }
            if (m7 > mm) { mm = m7; u = 0; v = w; }
            if (m8 > mm) { mm = m8; u = w; v = w; }

            w = Math.Max(Math.Abs(u), Math.Abs(v));
            nd1 = nd - w - 1; nc1 = nc1 - w - 1;
            for (int i = w + 1; i < nd1; i++)
                for (int j = w + 1; j < nc1; j++)
                {
                    //tinh khop tu anh t

                    if (imgq[i + u, j + v] == imgt[i, j]
                        ||
                        imgq[i - 1 + u, j + v] == imgt[i, j] ||
                        imgq[i + 1 + u, j + v] == imgt[i, j] ||
                        imgq[i + u, j - 1 + v] == imgt[i, j] ||
                        imgq[i + u, j + 1 + v] == imgt[i, j]
                        )
                    {
                        gt++;
                    }
                    if (imgt[i + u, j + v] == imgq[i, j] ||
                                    imgt[i - 1 + u, j + v] == imgq[i, j] ||
                                    imgt[i + 1 + u, j + v] == imgq[i, j] ||
                                    imgt[i + u, j - 1 + v] == imgq[i, j] ||
                                    imgt[i + u, j + 1 + v] == imgq[i, j])
                    {
                        gq++;
                    }

                    dt++;
                }
            gt = Math.Max(gt, gq) / dt;
            return gt;
        }
예제 #28
0
        public static Matrix1 apDungWaveletGabors(Matrix1 img, double goc, double tanso,int s)
        {
            Matrix1 image = new Matrix1(img.NoRows/s,img.NoCols/s);
            Matrix1 image2 = new Matrix1(img);
            //image = Matrix.Transpose(image);
            //image2 = Matrix.Transpose(image2);
            //luc tinh ham gabor
            double[] fre = new double[10] { 0.25, 0.5, 1, 2, 4, 6, 8, 10, 12, 14 }; //{2, 3, 4, 5, 6, 7, 8, 9,10,11,12};
            double[] rad = new double[8] { -4, 2, 4, 1, 3, -3, -6, 6 };

            int sh = 8;
            int wgs = 5;
            double[, ,] gV = new double[17, 11, 11];
            //for (int j = 0; j < 4; j++)

            {
                // tanso = 1.0 / Convert.ToDouble(fre[j]);
                for (int i = 0; i < sh; i++)
                {
                    goc = Math.PI / Convert.ToDouble(rad[i]);
                    //tinh toan ham gabor su dung mat na wg x wg
                    for (int v = -wgs; v <= wgs; v++)
                        for (int u = -wgs; u <= wgs; u++)
                            gV[i, v + wgs, u + wgs] = waveletGabor(v, u, goc, tanso);
                    //gV[i, v + wgs, u + wgs] = gabor(v, u, goc, tanso);
                }
            }

            //luc ap dung cho tung pixel
            double sum;
            int dx, dy, du, dv;
            dx = 0;
            dy = 0;
            du = 0;
            dv = 0;
            int ndong, ncot;
            ndong = image.NoRows; //-wgs;
            ncot = image.NoCols;// -wgs;
            int d, x, y;
            double sm =0, im = 0, sm1 = 0, im1 = 0;
            int dem = 0;
            //
            j = 0;
            for (x = 0; x < ndong; x+=s)
            {
                for (y = 0; y < ncot; y+=s)
                {
                    sm = Double.MaxValue;
                    sm1 = Double.MinValue;                      ///
                    //  for (int j = 0; j < 4; j++)

                    for (int i = 0; i < sh; i++)
                    {

                        //xem xet tung pixel(x,y)
                        //su dung mat na wg x wg
                        sum = 0;
                        d = 0;
                        for (int v = -wgs; v <= wgs; v++)
                        {
                            dv = x + v;
                            if (dv >= ncot || dv < 0)
                            {
                                d = d + 2 * wgs + 1;
                                continue;
                            }
                            for (int u = -wgs; u <= wgs; u++)
                            {
                                du = y + u;
                                if (du >= ndong || du < 0)
                                {
                                    d++;
                                    continue;
                                }
                                sum += (int)(gV[i, v + wgs, u + wgs] * image2[dv, du]);
                                d++;
                            }
                        }
                        //sum = sum / d;
                        if (sum < sm) { sm = sum; im = i; }
                        if (sum > sm1) { sm1 = sum; im1 = i; }              /////
                        dem++;
                    }
                    //                 Console.WriteLine(sm.ToString()+"__"+ im.ToString());
                    //gan lai muc xam cho anh
                    //gan huong min cho anh
                    image[x / s, y / s] = im;// sm;// sum;
                    //gan sum min cho anh
              //      image[x/s, y/s] = sm;
                    //image[x, y] = (sm + sm1)/2;
                }
            }
             //   Console.WriteLine(dem);
            // MessageBox.Show("sm+im: " + sm.ToString() + " & " + im.ToString());

            //10 scale : sum min + i min
            max_sum = sm1;
            max_i = im1;

            min_sum = sm;
            min_i = im;

               // Bitmap res = new Bitmap(PCA.matrix_2_image(image, image.NoRows, image.NoCols));

            return image;
        }
예제 #29
0
        public static Matrix1 cov_compute()
        {
            //cac bien local
            int i;
            //khai bao ma tran hiep phuong sai
            Matrix1 cov_matrix = new Matrix1(w, w);
            //
            int x, y;
            Matrix1 tam;

            for (i = 0; i < ns; i++)
            {

                //tru cho ma tran trung binh
                tam = Matrix1.Subtract(A[i], mt_mean);
                //phan thu nghiem weigh 2DLDA
                //for (y = 0; y < w; y++)
                //    for (x = 0; x < w; x++)
                //        tam[y, x] = (1 + 0.5 / (AT[i][y, x] - mt_mean[y, x]) / (AT[i][y, x] - mt_mean[y, x]) * hamLoi((AT[i][y, x] - mt_mean[y, x])*0.5*Math.Sqrt(2))) * (AT[i][y, x] - mt_mean[y, x]);

                Matrix1 mat = Matrix1.Multiply(Matrix1.Transpose(tam), tam);

                cov_matrix = Matrix1.Add(cov_matrix, mat);
            }
            for (y = 0; y < w; y++)
                for (x = 0; x < w; x++)
                    cov_matrix[y, x] /= (ns - 1);

            return cov_matrix;
        }
예제 #30
0
 /// <summary>
 ///  Returns the magnitude of a vector whose dimension is [3] or [3,1].
 ///  In case of an error the error is raised as an exception.
 /// </summary>
 /// <param name="V">Matrix1 object (dimension [3,1]) whose magnitude is to be found</param>
 /// <returns>The magnitude of the Matrix1 object</returns>
 public static double VectorMagnitude(Matrix1 V)
 {
     return (VectorMagnitude(V.in_Mat));
 }