Пример #1
0
        private void btnExtract_Click(object sender, EventArgs e)
        {
            FeatureController objCtrl = new FeatureController(mPath);
            Bitmap            bmpTemp;

            //duyệt mảng file --> tạo Object
            for (int i = 0; i < FileArray.Count; i++)
            {
                //Lay lan luot tung anh
                bmpTemp = (Bitmap)Bitmap.FromFile(FileArray[i].ToString());

                //Tách đối tượng ra khỏi ảnh --> tạo ảnh mới chỉ chứa khít đối tượng (shape)
                Bitmap bmpExtracted = ZinImageGrid.ExtractShape(bmpTemp);

                //A. Hòa: tìm x1, y1, x2, y2 của trục chính (dài nhất)
                int x1, y1, x2, y2;
                ImageFuncLib.getPoint(out x1, out y1, out x2, out y2, bmpExtracted);

                //Tìm góc --> xoay. Sau khi xoay sẽ xuất hiện nền thừa --> tìm HCN cơ sở
                double angle      = ZinImageGrid.AngleMajorAndX(x1, y1, x2, y2);         //!!! Important
                Bitmap bmpRotated = ZinImageLib.RotateImage(bmpExtracted, (float)angle); //Hàm Rotate2: ko làm thay đổi size --> Ko ổn
                ZinImageGrid.TransparentToWhite(bmpRotated);
                bmpRotated = ZinImageGrid.ExtractShape(bmpRotated);

                //Sau khi xoay xong thì mới Resize về kích thước cố định
                Bitmap bmpResized = ZinImageLib.Resize(bmpRotated, ZinImageGrid.WidthStandard, ZinImageGrid.WidthStandard * bmpRotated.Height / bmpRotated.Width, true);

                //Tô hình dạng thành đen đặc trước khi đếm
                Bitmap bmpBlackFill = (Bitmap)bmpResized.Clone();
                ZinImageGrid.FillSolidBlack(bmpBlackFill);
                //Luu XML ============================
                FeatureInfo objInfo = new FeatureInfo();
                objInfo.BitSequence = ZinImageGrid.GetBitString(bmpBlackFill);
                objInfo.MinorAxis   = ZinImageGrid.GetMinorAxisLen(bmpBlackFill);
                objInfo.ImagePath   = FileArray[i].ToString();

                //đưa vào  XML
                objCtrl.Add(objInfo);
            }

            objCtrl.WriteXML(); //ghi XML ra mPath

            MessageBox.Show("Đã trích chọn xong đặc trưng của ảnh & ghi vào file XML");
        }
Пример #2
0
        private void DrawImageProcessed()
        {
            Graphics gr = CreateGraphics();// Khởi tạo đồ hoạ trên form chính

            gr.Clear(this.BackColor);

            Bitmap bmpOrigin = (Bitmap)Bitmap.FromFile(pbImageInDB.ImageLocation);

            //Tách đối tượng ra khỏi ảnh --> tạo ảnh mới chỉ chứa khít đối tượng (shape)
            Bitmap bmpExtracted = ZinImageGrid.ExtractShape(bmpOrigin);

            pbExtractedObject.Image = bmpExtracted;

            //A. Hòa: tìm x1, y1, x2, y2 của trục chính (dài nhất)
            int x1, y1, x2, y2;

            ImageFuncLib.getPoint(out x1, out y1, out x2, out y2, bmpExtracted);
            //Vẽ ảnh (để vẽ đường, ko dùng đc PicBox)
            gr.DrawImage(bmpExtracted, 253, 414);
            //Ve duong truc chinh
            Pen myPen = new Pen(Color.Red, 2);

            gr.DrawLine(myPen, x1 + 253, y1 + 414, x2 + 253, y2 + 414);

            //Tìm góc --> xoay. Sau khi xoay sẽ xuất hiện nền thừa --> tìm HCN cơ sở
            double angle      = ZinImageGrid.AngleMajorAndX(x1, y1, x2, y2);         //!!! Important
            Bitmap bmpRotated = ZinImageLib.RotateImage(bmpExtracted, (float)angle); //Hàm Rotate2: ko làm thay đổi size --> Ko ổn

            //pbImageRotated.Image = bmpRotated;
            ZinImageGrid.TransparentToWhite(bmpRotated);
            bmpRotated = ZinImageGrid.ExtractShape(bmpRotated);
            gr.DrawImage(bmpRotated, 491, 414);

            //Sau khi xoay xong thì mới Resize về kích thước cố định
            Bitmap bmpResized = ZinImageLib.Resize(bmpRotated, ZinImageGrid.WidthStandard, ZinImageGrid.WidthStandard * bmpRotated.Height / bmpRotated.Width, true);

            //Bitmap bmpResized = ZinImageLib.ResizeImage(bmpRotated, ZinImageLib.WidthStandard, ZinImageLib.WidthStandard * bmpRotated.Height / bmpRotated.Width); //Bị thay đổi -> nguy hiểm
            gr.DrawImage(bmpResized, 491, 196);

            //Phủ lưới lên
            for (int i = 0; i < bmpResized.Width; i++)
            {
                if (i % ZinImageGrid.CellWidth == 0)
                {
                    gr.DrawLine(Pens.Red, i + 491, 0 + 196, i + 491, bmpResized.Height + 196);
                }
            }

            for (int j = 0; j < bmpResized.Height; j++)
            {
                if (j % ZinImageGrid.CellHeight == 0)
                {
                    gr.DrawLine(Pens.Red, 0 + 491, j + 196, bmpResized.Width + 491, j + 196);
                }
            }


            //Tô hình dạng thành đen đặc trước khi đếm
            Bitmap bmpBlackFill = (Bitmap)bmpResized.Clone();

            ZinImageGrid.FillSolidBlack(bmpBlackFill);

            //Hien thi chuoi bit
            lblBitString.Text = ZinImageGrid.DisplayBitString(ZinImageGrid.GetBitString(bmpBlackFill));

            //gr.Dispose();
        }
Пример #3
0
        private void btnBrowseImage_Click(object sender, EventArgs e)
        {
            if (ofdBrowseImage.ShowDialog(this) == DialogResult.OK)
            {
                Graphics gr = CreateGraphics();// Khởi tạo đồ hoạ trên form chính
                gr.Clear(this.BackColor);

                //Hien thi anh len pbQueryImage
                Bitmap bmpQuery = (Bitmap)Bitmap.FromFile(ofdBrowseImage.FileName);
                pbQueryImage.Image = bmpQuery;

                //Buoc 1: Tách đối tượng ra khỏi ảnh --> tạo ảnh mới chỉ chứa khít đối tượng (shape)
                Bitmap bmpExtracted = ZinImageGrid.ExtractShape(bmpQuery);
                gr.DrawImage(bmpExtracted, 28, 380);

                //Buoc 2: tìm x1, y1, x2, y2 của trục chính (dài nhất)
                int x1, y1, x2, y2;
                ImageFuncLib.getPoint(out x1, out y1, out x2, out y2, bmpExtracted);
                //Vẽ ảnh (để vẽ đường, ko dùng đc PicBox)
                gr.DrawImage(bmpExtracted, 242, 380);
                //Ve duong truc chinh
                Pen myPen = new Pen(Color.Red, 2);
                gr.DrawLine(myPen, x1 + 242, y1 + 380, x2 + 242, y2 + 380);

                //Buoc 3: Tìm góc --> xoay. Sau khi xoay sẽ xuất hiện nền thừa --> tìm HCN cơ sở để cắt bớt
                double angle      = ZinImageGrid.AngleMajorAndX(x1, y1, x2, y2);         //!!! Important
                Bitmap bmpRotated = ZinImageLib.RotateImage(bmpExtracted, (float)angle); //Hàm Rotate2: ko làm thay đổi size --> bị cắt xén ảnh --> Ko ổn
                ///////////////(chu y: ham Xoay co van de --> Fill black bi loi)////////////////
                ZinImageGrid.TransparentToWhite(bmpRotated);
                bmpRotated = ZinImageGrid.ExtractShape(bmpRotated); //cắt bớt nền thừa
                gr.DrawImage(bmpRotated, 458, 380);

                //Buoc 4: Sau khi xoay xong thì mới Resize về kích thước cố định
                Bitmap bmpResized = ZinImageLib.Resize(bmpRotated, ZinImageGrid.WidthStandard, ZinImageGrid.WidthStandard * bmpRotated.Height / bmpRotated.Width, true);
                //Bitmap bmpResized = ZinImageLib.ResizeImage(bmpRotated, ZinImageGrid.WidthStandard, ZinImageGrid.WidthStandard * bmpRotated.Height / bmpRotated.Width); //Bị thay đổi -> nguy hiểm

                //Buoc 5: Lưu 4 trường hợp: lật trái, phải, ngược của ảnh
                Bitmap[] bmpAllCases = new Bitmap[4];
                for (int i = 0; i < 4; i++)
                {
                    bmpAllCases[i] = (Bitmap)bmpResized.Clone(); //Copy thanh 4 anh
                }
                //---- Lật 3 ảnh, giữ lại ảnh gốc -----
                ZinImageLib.Flip(bmpAllCases[1], false, true);
                ZinImageLib.Flip(bmpAllCases[2], true, false);
                ZinImageLib.Flip(bmpAllCases[3], true, true);

                //Buoc 6: Phu luoi len
                for (int i = 0; i < 4; i++)
                {
                    gr.DrawImage(bmpAllCases[i], 28 + i * 220, 517);

                    //Minh hoa: Phủ lưới lên
                    for (int c = 0; c <= bmpAllCases[i].Width; c++)
                    {
                        if (c % ZinImageGrid.CellWidth == 0)
                        {
                            gr.DrawLine(Pens.Red, 28 + i * 220 + c, 517, 28 + i * 220 + c, bmpAllCases[i].Height + 517);
                        }
                    }

                    for (int r = 0; r <= bmpAllCases[i].Height; r++)
                    {
                        if (r % ZinImageGrid.CellHeight == 0)
                        {
                            gr.DrawLine(Pens.Red, 28 + i * 220, r + 517, 28 + i * 220 + bmpAllCases[i].Width, r + 517);
                        }
                    }
                }

                //Buoc 7: dem so o >= 15% --> bit 1
                mFeatureQuery.Clear();
                lblBitString.Text = "";
                for (int i = 0; i < 4; i++)
                {
                    //Tô hình dạng thành đen đặc trước khi đếm
                    Bitmap bmpBlackFill = (Bitmap)bmpAllCases[i].Clone();
                    ZinImageGrid.FillSolidBlack(bmpBlackFill);
                    //show ra xem the nao
                    //gr.DrawImage(bmpBlackFill, 300 + i * 200, 150);

                    string BitSeq = ZinImageGrid.GetBitString(bmpBlackFill);
                    lblBitString.Text += ZinImageGrid.DisplayBitString(BitSeq) + Environment.NewLine;

                    //Gán vào mFeatureQuery
                    FeatureInfo temp = new FeatureInfo();
                    temp.BitSequence = BitSeq;
                    temp.MinorAxis   = ZinImageGrid.GetMinorAxisLen(bmpBlackFill);

                    //Dua ca 4 truong hop anh vao truy van
                    mFeatureQuery.Add(temp);
                }

                //gr.Dispose();
                btnSearch.Enabled   = true;
                lblResultCount.Text = "";
            }
        }