- button1: 讀圖 lena 與 lena_2
- button2: 求 lena 與 lena_2之差異 difference
- button3: 將差異轉成灰階圖
- button4: 利用影像增強手法將 button3 之差異強化
##ReadMore##
比較圖一:lena
比較圖二:lena_2
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using AForge.Imaging; using AForge.Imaging.Formats; using AForge.Imaging.Filters; using AForge.Video; using System.Diagnostics; using System.Drawing.Imaging; using AForge.Math; namespace examination_4 { public partial class exam4 : Form { Bitmap img1, img2, catchImg, grayImg; public exam4() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "所有檔案(*.*)|*.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { img1 = ImageDecoder.DecodeFromFile(openFileDialog1.FileName); pictureBox1.Image = img1; } } private void button2_Click(object sender, EventArgs e) { openFileDialog2.Filter = "所有檔案(*.*)|*.*"; if (openFileDialog2.ShowDialog() == DialogResult.OK) { img2 = ImageDecoder.DecodeFromFile(openFileDialog2.FileName); pictureBox2.Image = img2; } } private void Form1_Load(object sender, EventArgs e) { img1 = (Bitmap)pictureBox1.Image; img2 = (Bitmap)pictureBox2.Image; } private void button3_Click(object sender, EventArgs e) { // 取兩圖長、寬最小值 int imgWidth = Math.Min(img1.Width, img2.Width); int imgHeight = Math.Min(img1.Height, img2.Height); int minX = imgWidth; int minY = imgHeight; int maxX = 0; int maxY = 0; // 比較的 Pixel 範圍 for (int x = 0; x < imgWidth; x++) { for (int y = 0; y < imgHeight; y++) { // 比較兩圖 Pixel if(img1.GetPixel(x,y).Equals(img2.GetPixel(x,y))) { // 相同 Pixel ,維持原圖 } else { // 不相同 // 取差異圖最左上點 if (minX > x) minX = x; if (minY > y) minY = y; // 取差異圖最右下點 if (maxX < x) maxX = x; if (maxY < y) maxY = y; } } } catchDiff(minX, minY, maxX, maxY); drawDiffFrame(minX, minY, maxX, maxY); pictureBox1.Image = img1; pictureBox2.Image = img2; } // 補捉兩圖不同點於 pictureBox3 private void catchDiff(int topLeft_X, int topLeft_Y, int bottomRight_X, int bottomRight_Y) { Crop cropFilter = new Crop(new Rectangle(topLeft_X - 5, topLeft_Y - 5, bottomRight_X - topLeft_X + 10, bottomRight_Y - topLeft_Y + 10)); catchImg = cropFilter.Apply(img2); pictureBox3.Image = catchImg; } // 於原圖及比對圖畫出框,框出差異 private void drawDiffFrame(int topLeft_X, int topLeft_Y, int bottomRight_X, int bottomRight_Y) { using (Graphics g = Graphics.FromImage(img1)) { g.DrawRectangle(new Pen(Color.Red), topLeft_X - 6, topLeft_Y - 6, bottomRight_X - topLeft_X + 12, bottomRight_Y - topLeft_Y + 12); } using (Graphics g = Graphics.FromImage(img2)) { g.DrawRectangle(new Pen(Color.Red), topLeft_X - 6, topLeft_Y - 6, bottomRight_X - topLeft_X + 12, bottomRight_Y - topLeft_Y + 12); } } // 影像灰階 private void button4_Click(object sender, EventArgs e) { Grayscale grayFilter = new Grayscale(0.2125, 0.7154, 0.0721); grayImg = grayFilter.Apply(catchImg); pictureBox3.Image = grayImg; } // 續影像灰階,再影像強化處理 private void button5_Click(object sender, EventArgs e) { Bitmap thsImg; Threshold thFilter = new Threshold(23); thsImg = thFilter.Apply(grayImg); pictureBox3.Image = thsImg; } } }
沒有留言:
張貼留言