ariestamirra

prak13

Jun 28th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Praktikum13
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         List<Bitmap> objBitmap = new List<Bitmap>();
  15.         Bitmap objBitmap1;
  16.         Bitmap objBitmap2;
  17.         Bitmap objBitmap3;
  18.         Bitmap objBitmap4;
  19.         int jumlahImage = 0;
  20.    
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private void LoadImage1_Click(object sender, EventArgs e)
  27.         {
  28.             DialogResult d = openFileDialog1.ShowDialog();
  29.             if (d == DialogResult.OK)
  30.             {
  31.                 objBitmap1 = new Bitmap(openFileDialog1.FileName);
  32.                 pictureBox1.Image = objBitmap1;
  33.             }            
  34.         }
  35.  
  36.         private void LoadImage2_Click(object sender, EventArgs e)
  37.         {
  38.             DialogResult d = openFileDialog1.ShowDialog();
  39.             if (d == DialogResult.OK)
  40.             {
  41.                 objBitmap2 = new Bitmap(openFileDialog1.FileName);
  42.                 pictureBox2.Image = objBitmap2;
  43.             }
  44.            
  45.         }
  46.         private int[] hitungHX(Bitmap objBitmap)
  47.         {
  48.             objBitmap3 = new Bitmap(objBitmap);
  49.             int[] hx = new int[objBitmap.Height];
  50.             int counter = 0;
  51.             for (int y = 0; y < objBitmap.Height; y++)
  52.             {
  53.                 for (int x = 0; x < objBitmap.Width; x++)
  54.                 {
  55.                     Color w = objBitmap.GetPixel(x, y);
  56.                     if (w.R == 0) counter++;
  57.                 }
  58.                 hx[y] = counter;
  59.                 counter = 0;
  60.             }
  61.             return hx;
  62.         }
  63.         private int[] hitungHY(Bitmap objBitmap)
  64.         {
  65.             objBitmap3 = new Bitmap(objBitmap);
  66.             int[] hy = new int[objBitmap.Width];
  67.             int counter = 0;
  68.  
  69.             for (int x = 0; x < objBitmap.Width; x++)
  70.             {
  71.                 for (int y = 0; y < objBitmap.Height; y++)
  72.                 {
  73.                     Color w = objBitmap.GetPixel(x, y);
  74.                     if (w.R == 0) counter++;
  75.                 }
  76.                 hy[x] = counter;
  77.                 counter = 0;
  78.             }
  79.             return hy;
  80.         }
  81.  
  82.         private int selisih(int[] hx1, int[]hy1, int[]hx2, int[]hy2)
  83.         {
  84.             int r = 0;
  85.             for (int i = 0; i < hx1.Length; i++)
  86.                 r = r + (Math.Abs(hx1[i] - hx2[i]));
  87.             for (int i = 0; i < hy1.Length; i++)
  88.                 r = r + (Math.Abs(hy1[i] - hy2[i]));
  89.             return r;
  90.         }
  91.  
  92.         private void btnSelisih_Click(object sender, EventArgs e)
  93.         {        
  94.             int[] hx1 = hitungHX(objBitmap1);
  95.             int[] hx2 = hitungHX(objBitmap2);
  96.             int[] hy1 = hitungHY(objBitmap1);
  97.             int[] hy2 = hitungHY(objBitmap2);
  98.             labelSelisih.Text = "Selisih = " + selisih(hx1, hy1,hx2, hy2);
  99.         }
  100.  
  101.         private void btnLoadImage_Click(object sender, EventArgs e)
  102.         {
  103.             DialogResult d = openFileDialog1.ShowDialog();
  104.             if (d == DialogResult.OK)
  105.             {
  106.                 Bitmap obj = new Bitmap(openFileDialog1.FileName);
  107.                 objBitmap.Add(obj);
  108.                 jumlahImage += 1;
  109.                 jmlImage.Text = "Jumlah Gambar : "+ jumlahImage.ToString();
  110.  
  111.                 PictureBox pb = new PictureBox();
  112.                 pb.SizeMode = PictureBoxSizeMode.StretchImage;
  113.                 pb.Height = 100;
  114.                 pb.Width = 100;
  115.                 pb.Image = obj;
  116.                 flowLayoutPanel1.Controls.Add(pb);
  117.             }
  118.         }
  119.  
  120.         private void btnLoadImageBanding_Click(object sender, EventArgs e)
  121.         {
  122.             DialogResult d = openFileDialog1.ShowDialog();
  123.             if (d == DialogResult.OK)
  124.             {
  125.                 objBitmap4 = new Bitmap(openFileDialog1.FileName);
  126.                 pictureBox3.Image = objBitmap4;
  127.             }            
  128.         }
  129.  
  130.         private void btnBanding_Click(object sender, EventArgs e)
  131.         {
  132.             int[] hx = hitungHX(objBitmap4);
  133.             int[] hy = hitungHY(objBitmap4);
  134.             int[] hslSelisih = new int[jumlahImage];
  135.             for (int i = 0; i < jumlahImage; i++)
  136.             {
  137.                 int[] hx1 = hitungHX(objBitmap[i]);
  138.                 int[] hy1 = hitungHY(objBitmap[i]);
  139.                 hslSelisih[i] = selisih(hx, hy, hx1, hy1);
  140.             }
  141.  
  142.             //mengurutkan data ranking
  143.             int[] values = new int[jumlahImage];
  144.             for (int i = 0; i < jumlahImage; i++)
  145.             {
  146.                 values[i] = i;
  147.             }
  148.  
  149.             //melakukan sorting
  150.             Array.Sort(hslSelisih, values);
  151.             foreach (int key in hslSelisih)
  152.             {
  153.                 Console.Write(key);
  154.                 Console.Write(' ');
  155.             }
  156.             Console.WriteLine();
  157.             foreach (int value in values)
  158.             {
  159.                 Console.Write(value);
  160.                 Console.Write(' ');
  161.             }
  162.             Console.WriteLine();
  163.  
  164.             //menampilkan data hasil
  165.             flowLayoutPanel2.Controls.Clear();
  166.             for (int i = 0; i < jumlahImage; i++)
  167.             {
  168.                 FlowLayoutPanel panel = new FlowLayoutPanel();
  169.                 panel.Height = 130;
  170.                 panel.Width = 110;
  171.                 panel.AutoScroll = true;
  172.                 panel.FlowDirection = FlowDirection.BottomUp;
  173.  
  174.                 PictureBox pb = new PictureBox();
  175.                 pb.SizeMode = PictureBoxSizeMode.StretchImage;
  176.                 pb.Height = 100;
  177.                 pb.Width = 100;
  178.                 pb.Image = objBitmap[values[i]];
  179.  
  180.                 Label lb = new Label();
  181.                 lb.Text = "Selisih : " + hslSelisih[i];
  182.  
  183.                 panel.Controls.Add(pb);
  184.                 panel.Controls.Add(lb);
  185.                 flowLayoutPanel2.Controls.Add(panel);
  186.             }
  187.         }  
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment