Konvolusi dan Image Filtering

14
1 | Merinda Icha F – 2103131035 – 2 D3 IT B Laporan Resmi Pengolahan Citra Praktikum 8 - Konvolusi dan Image Filtering Source Code : 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 Konvolusi 11. { 12. public partial class Form1 : Form 13. { 14. Bitmap objBitmap1; 15. Bitmap objBitmap2; 16. Bitmap objBitmap3; 17. Bitmap objBitmap4; 18. Bitmap objBitmap5; 19. Bitmap objBitmap6; 20. Bitmap objBitmap7; 21. public Form1() 22. { 23. InitializeComponent(); 24. } 25. 26. private void button1_Click(object sender, EventArgs e) 27. { 28. DialogResult d = openFileDialog1.ShowDialog(); 29. if (d == DialogResult.OK) { 30. objBitmap1 = new Bitmap(openFileDialog1.FileName); 31. pictureBox1.Image = objBitmap1; 32. } 33. } 34. 35. private void button2_Click(object sender, EventArgs e) 36. { 37. float[] a = new float[5]; 38. a[0] = (float)0.2; 39. a[1] = (float)0.2; 40. a[2] = (float)0.2; 41. a[3] = (float)0.2; 42. a[4] = (float)0.2; 43. objBitmap2 = new Bitmap(objBitmap1); 44. for (int x = 1; x < objBitmap1.Width - 1; x++) 45. for (int y = 1; y < objBitmap1.Height - 1; y++) 46. { 47. Color w1 = objBitmap1.GetPixel(x - 1, y); 48. Color w2 = objBitmap1.GetPixel(x + 1, y); 49. Color w3 = objBitmap1.GetPixel(x, y - 1); 50. Color w4 = objBitmap1.GetPixel(x, y + 1); 51. Color w = objBitmap1.GetPixel(x, y); 52. int x1 = (w1.R + w1.G + w1.B) / 3; 53. int x2 = (w2.R + w2.G + w2.B) / 3; 54. int x3 = (w3.R + w3.G + w3.B) / 3; 55. int x4 = (w4.R + w4.G + w4.B) / 3; 56. int xg = (w.R + w.G + w.B) / 3; 57. int xb = (int)(a[0] * xg); 58. xb = (int)(xb + a[1] * x1 + a[2]*x2+a[3]*x3+a[4]*x4); 59. if (xb < 0) xb = 0; 60. if (xb > 255) xb = 255; 61. Color wb = Color.FromArgb(xb, xb, xb);

Transcript of Konvolusi dan Image Filtering

1 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

Laporan Resmi Pengolahan Citra

Praktikum 8 - Konvolusi dan Image Filtering

Source Code :

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 Konvolusi 11. { 12. public partial class Form1 : Form 13. { 14. Bitmap objBitmap1; 15. Bitmap objBitmap2; 16. Bitmap objBitmap3; 17. Bitmap objBitmap4; 18. Bitmap objBitmap5; 19. Bitmap objBitmap6; 20. Bitmap objBitmap7; 21. public Form1() 22. { 23. InitializeComponent(); 24. } 25. 26. private void button1_Click(object sender, EventArgs e) 27. { 28. DialogResult d = openFileDialog1.ShowDialog(); 29. if (d == DialogResult.OK) { 30. objBitmap1 = new Bitmap(openFileDialog1.FileName); 31. pictureBox1.Image = objBitmap1; 32. } 33. } 34. 35. private void button2_Click(object sender, EventArgs e) 36. { 37. float[] a = new float[5]; 38. a[0] = (float)0.2; 39. a[1] = (float)0.2; 40. a[2] = (float)0.2; 41. a[3] = (float)0.2; 42. a[4] = (float)0.2; 43. objBitmap2 = new Bitmap(objBitmap1); 44. for (int x = 1; x < objBitmap1.Width - 1; x++) 45. for (int y = 1; y < objBitmap1.Height - 1; y++) 46. { 47. Color w1 = objBitmap1.GetPixel(x - 1, y); 48. Color w2 = objBitmap1.GetPixel(x + 1, y); 49. Color w3 = objBitmap1.GetPixel(x, y - 1); 50. Color w4 = objBitmap1.GetPixel(x, y + 1); 51. Color w = objBitmap1.GetPixel(x, y); 52. int x1 = (w1.R + w1.G + w1.B) / 3; 53. int x2 = (w2.R + w2.G + w2.B) / 3; 54. int x3 = (w3.R + w3.G + w3.B) / 3; 55. int x4 = (w4.R + w4.G + w4.B) / 3; 56. int xg = (w.R + w.G + w.B) / 3; 57. int xb = (int)(a[0] * xg); 58. xb = (int)(xb + a[1] * x1 + a[2]*x2+a[3]*x3+a[4]*x4); 59. if (xb < 0) xb = 0; 60. if (xb > 255) xb = 255; 61. Color wb = Color.FromArgb(xb, xb, xb);

2 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

62. objBitmap2.SetPixel(x, y, wb); 63. } 64. pictureBox2.Image = objBitmap2; 65. float[] h = new float[256]; 66. for (int x = 0; x < objBitmap1.Width; x++) 67. for (int y = 0; y < objBitmap1.Height; y++) 68. { 69. Color w = objBitmap2.GetPixel(x, y); 70. int xg = (int)(w.R + w.G + w.B) / 3; ; 71. h[xg] = h[xg] + 1; 72. } 73. 74. for (int i = 0; i < 256; i++) 75. { 76. chart1.Series["Series1"].Points.AddXY(i, h[i]); 77. } 78. } 79. 80. private void button3_Click(object sender, EventArgs e) 81. { 82. float[] a = new float[10]; 83. a[1] = (float)0.1; 84. a[2] = (float)0.1; 85. a[3] = (float)0.1; 86. a[4] = (float)0.1; 87. a[5] = (float)0.2; 88. a[6] = (float)0.1; 89. a[7] = (float)0.1; 90. a[8] = (float)0.1; 91. a[9] = (float)0.1; 92. objBitmap3 = new Bitmap(objBitmap1); 93. for (int x = 1; x < objBitmap1.Width - 1; x++) 94. for (int y = 1; y < objBitmap1.Height - 1; y++)

95. {

96. Color w1 = objBitmap1.GetPixel(x - 1, y - 1); 97. Color w2 = objBitmap1.GetPixel(x - 1, y); 98. Color w3 = objBitmap1.GetPixel(x - 1, y + 1); 99. Color w4 = objBitmap1.GetPixel(x, y - 1); 100. Color w5 = objBitmap1.GetPixel(x, y);

101. Color w6 = objBitmap1.GetPixel(x, y + 1);

102. Color w7 = objBitmap1.GetPixel(x + 1, y - 1);

103. Color w8 = objBitmap1.GetPixel(x + 1, y);

104. Color w9 = objBitmap1.GetPixel(x + 1, y + 1);

105. int x1 = (w1.R + w1.G + w1.B) / 3;

106. int x2 = (w2.R + w2.G + w2.B) / 3;

107. int x3 = (w3.R + w3.G + w3.B) / 3;

108. int x4 = (w4.R + w4.G + w4.B) / 3;

109. int x5 = (w5.R + w5.G + w5.B) / 3;

110. int x6 = (w6.R + w6.G + w6.B) / 3;

111. int x7 = (w7.R + w7.G + w7.B) / 3;

112. int x8 = (w8.R + w8.G + w8.B) / 3;

113. int x9 = (w9.R + w9.G + w9.B) / 3;

114. int xb = (int)(a[1]*x1 + a[2]*x2 +a[3]* x3);

115. xb = (int)(xb +a[4]* x4+ a[5]*x5 +a[6]* x6);

116. xb = (int)(xb +a[7]* x7 + a[8]*x8+a[9]* x9);

117. if (xb < 0) xb = 0;

118. if (xb > 255) xb = 255;

119. Color wb = Color.FromArgb(xb, xb, xb);

120. objBitmap3.SetPixel(x, y, wb);

121. }

122. pictureBox3.Image = objBitmap3;

123. float[] h = new float[256];

124. for (int x = 0; x < objBitmap1.Width ; x++)

125. for (int y = 0; y < objBitmap1.Height ; y++)

126. {

127. Color w = objBitmap3.GetPixel(x, y);

128. int xg = (int)(w.R + w.G + w.B) / 3; ;

129. h[xg] = h[xg] + 1;

3 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

130. }

131. for (int i = 0; i < 256; i++)

132. {

133. chart2.Series["Series1"].Points.AddXY(i , h[i]);

134. }

135. }

136.

137. private void button4_Click(object sender, EventArgs e)

138. {

139. float[] a = new float[5];

140. a[0] = (float)-0.5;

141. a[1] = (float)-0.5;

142. a[2] = (float)0;

143. a[3] = (float)0.5;

144. a[4] = (float)0.5;

145.

146. objBitmap4 = new Bitmap(objBitmap1);

147. for (int x = 1; x < objBitmap1.Width - 1; x++)

148. for (int y = 1; y < objBitmap1.Height - 1; y++)

149. {

150. Color w1 = objBitmap1.GetPixel(x - 1, y);

151. Color w2 = objBitmap1.GetPixel(x + 1, y);

152. Color w3 = objBitmap1.GetPixel(x, y - 1);

153. Color w4 = objBitmap1.GetPixel(x, y + 1);

154. Color w = objBitmap1.GetPixel(x, y);

155. int x1 = (w1.R + w1.G + w1.B) / 3;

156. int x2 = (w2.R + w2.G + w2.B) / 3;

157. int x3 = (w3.R + w3.G + w3.B) / 3;

158. int x4 = (w4.R + w4.G + w4.B) / 3;

159. int xg = (w.R + w.G + w.B) / 3;

160. int xb = (int)(a[0] * xg);

161. xb=(int)(xb +a[1]*x1+a[2]* x2+ a[3]*x3+a[4]* x4);

162. if (xb < 0) xb = 0;

163. if (xb > 255) xb = 255;

164. Color wb = Color.FromArgb(xb, xb, xb);

165. objBitmap4.SetPixel(x, y, wb);

166. }

167. pictureBox4.Image = objBitmap4;

168. float[] h = new float[256];

169. for (int x = 0; x < objBitmap1.Width; x++)

170. for (int y = 0; y < objBitmap1.Height ; y++)

171. {

172. Color w = objBitmap4.GetPixel(x, y);

173. int xg = (int)(w.R + w.G + w.B) / 3; ;

174. h[xg] = h[xg] + 1;

175. }

176. for (int i = 0; i < 256; i++)

177. {

178. chart3.Series["Series1"].Points.AddXY(i, h[i]);

179. }

180. }

181.

182. private void button5_Click(object sender, EventArgs e)

183. {

184. float[] a = new float[10];

185. a[0] = (float)1;

186. a[1] = (float)-0.5;

187. a[2] = (float)0;

188. a[3] = (float)-0.5;

189. a[4] = (float)0;

190. a[5] = (float)0.5;

191. a[6] = (float)0;

192. a[7] = (float)0.5;

193. a[8] = (float)1;

194. objBitmap5 = new Bitmap(objBitmap1);

195. for (int x = 1; x < objBitmap1.Width - 1; x++)

196. for (int y = 1; y < objBitmap1.Height - 1; y++)

197. {

4 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

198. Color w1 = objBitmap1.GetPixel(x - 1, y - 1);

199. Color w2 = objBitmap1.GetPixel(x - 1, y);

200. Color w3 = objBitmap1.GetPixel(x - 1, y + 1);

201. Color w4 = objBitmap1.GetPixel(x, y - 1);

202. Color w5 = objBitmap1.GetPixel(x, y);

203. Color w6 = objBitmap1.GetPixel(x, y + 1);

204. Color w7 = objBitmap1.GetPixel(x + 1, y - 1);

205. Color w8 = objBitmap1.GetPixel(x + 1, y);

206. Color w9 = objBitmap1.GetPixel(x + 1, y + 1);

207. int x1 = (w1.R + w1.G + w1.B) / 3;

208. int x2 = (w2.R + w2.G + w2.B) / 3;

209. int x3 = (w3.R + w3.G + w3.B) / 3;

210. int x4 = (w4.R + w4.G + w4.B) / 3;

211. int x5 = (w5.R + w5.G + w5.B) / 3;

212. int x6 = (w6.R + w6.G + w6.B) / 3;

213. int x7 = (w7.R + w7.G + w7.B) / 3;

214. int x8 = (w8.R + w8.G + w8.B) / 3;

215. int x9 = (w9.R + w9.G + w9.B) / 3;

216. int xb=(int)(a[1] * x1 + a[2]*x2 + a[3]*x3);

217. xb =(int)(xb + a[4]* x4 + a[5]* x5 + a[6] *x6);

218. xb =(int)(xb + a[7] * x7 + a[8]* x8 + a[9] *x9);

219. if (xb < 0) xb = 0;

220. if (xb > 255) xb = 255;

221. Color wb = Color.FromArgb(xb, xb, xb);

222. objBitmap5.SetPixel(x, y, wb);

223. }

224. pictureBox5.Image = objBitmap5;

225. float[] h = new float[256];

226. for (int x = 0; x < objBitmap1.Width; x++)

227. for (int y = 0; y < objBitmap1.Height; y++)

228. {

229. Color w = objBitmap5.GetPixel(x,y);

230. int xg = (int)(w.R + w.G + w.B) / 3; ;

231. h[xg] = h[xg] + 1;

232. }

233. for (int i = 0; i < 256; i++)

234. {

235. chart4.Series["Series1"].Points.AddXY(i, h[i]);

236. }

237. }

238.

239. private void button6_Click(object sender, EventArgs e)

240. {

241. float[] a = new float[5];

242. a[0] = (float)-0.5;

243. a[1] = (float)-0.5;

244. a[2] = (float)1;

245. a[3] = (float)0.5;

246. a[4] = (float)0.5;

247.

248. objBitmap6 = new Bitmap(objBitmap1);

249. for (int x = 1; x < objBitmap1.Width - 1; x++)

250. for (int y = 1; y < objBitmap1.Height - 1; y++)

251. {

252. Color w1 = objBitmap1.GetPixel(x - 1, y);

253. Color w2 = objBitmap1.GetPixel(x + 1, y);

254. Color w3 = objBitmap1.GetPixel(x, y - 1);

255. Color w4 = objBitmap1.GetPixel(x, y + 1);

256. Color w = objBitmap1.GetPixel(x, y);

257. int x1 = (w1.R + w1.G + w1.B) / 3;

258. int x2 = (w2.R + w2.G + w2.B) / 3;

259. int x3 = (w3.R + w3.G + w3.B) / 3;

260. int x4 = (w4.R + w4.G + w4.B) / 3;

261. int xg = (w.R + w.G + w.B) / 3;

262. int xb = (int)(a[0] * xg);

263. xb=(int)(xb +a[1]*x1 + a[2]*x2+a[3]* x3+a[4]*x4);

264. if (xb < 0) xb = 0;

265. if (xb > 255) xb = 255;

5 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

266. Color wb = Color.FromArgb(xb, xb, xb);

267. objBitmap6.SetPixel(x, y, wb);

268. }

269. pictureBox6.Image = objBitmap6;

270. float[] h = new float[256];

271. for (int x = 0; x < objBitmap1.Width; x++)

272. for (int y = 0; y < objBitmap1.Height ; y++)

273. {

274. Color w = objBitmap6.GetPixel(x,y);

275. int xg = (int)(w.R + w.G + w.B) / 3; ;

276. h[xg] = h[xg] + 1;

277. }

278. for (int i = 0; i < 256; i++)

279. {

280. chart5.Series["Series1"].Points.AddXY(i, h[i]);

281. }

282. }

283.

284. private void button7_Click(object sender, EventArgs e)

285. {

286. float[] a = new float[10];

287. a[0] = (float)1;

288. a[1] = (float)-0.5;

289. a[2] = (float)0;

290. a[3] = (float)-0.5;

291. a[4] = (float)1;

292. a[5] = (float)0.5;

293. a[6] = (float)0;

294. a[7] = (float)0.5;

295. a[8] = (float)1;

296. objBitmap7 = new Bitmap(objBitmap1);

297. for (int x = 1; x < objBitmap1.Width - 1; x++)

298. for (int y = 1; y < objBitmap1.Height - 1; y++)

299. {

300. Color w1 = objBitmap1.GetPixel(x - 1, y - 1);

301. Color w2 = objBitmap1.GetPixel(x - 1, y);

302. Color w3 = objBitmap1.GetPixel(x - 1, y + 1);

303. Color w4 = objBitmap1.GetPixel(x, y - 1);

304. Color w5 = objBitmap1.GetPixel(x, y);

305. Color w6 = objBitmap1.GetPixel(x, y + 1);

306. Color w7 = objBitmap1.GetPixel(x + 1, y - 1);

307. Color w8 = objBitmap1.GetPixel(x + 1, y);

308. Color w9 = objBitmap1.GetPixel(x + 1, y + 1);

309. int x1 = (w1.R + w1.G + w1.B) / 3;

310. int x2 = (w2.R + w2.G + w2.B) / 3;

311. int x3 = (w3.R + w3.G + w3.B) / 3;

312. int x4 = (w4.R + w4.G + w4.B) / 3;

313. int x5 = (w5.R + w5.G + w5.B) / 3;

314. int x6 = (w6.R + w6.G + w6.B) / 3;

315. int x7 = (w7.R + w7.G + w7.B) / 3;

316. int x8 = (w8.R + w8.G + w8.B) / 3;

317. int x9 = (w9.R + w9.G + w9.B) / 3;

318. int xb = (int)(a[1] * x1 + a[2] * x2 + a[3] * x3);

319. xb = (int)(xb + a[4] * x4 + a[5]* x5 + a[6]* x6);

320. xb = (int)(xb + a[7] * x7 + a[8]* x8 + a[9]* x9);

321. if (xb < 0) xb = 0;

322. if (xb > 255) xb = 255;

323. Color wb = Color.FromArgb(xb, xb, xb);

324. objBitmap7.SetPixel(x, y, wb);

325. }

326. pictureBox7.Image = objBitmap7;

327. float[] h = new float[256];

328. for (int x = 0; x < objBitmap1.Width; x++)

329. for (int y = 0; y < objBitmap1.Height; y++)

330. {

331. Color w = objBitmap7.GetPixel(x,y);

332. int xg = (int)(w.R + w.G + w.B) / 3; ;

333. h[xg] = h[xg] + 1;

6 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

334. }

335. for (int i = 0; i < 256; i++)

336. {

337. chart6.Series["Series1"].Points.AddXY(i, h[i]);

338. }

339. }

340. }

341. }

Output :

Filter 4 Node

Source Code :

1. private void button2_Click(object sender, EventArgs e) 2. { 3. float[] a = new float[5]; 4. a[0] = (float)0.2; 5. a[1] = (float)0.2; 6. a[2] = (float)0.2; 7. a[3] = (float)0.2; 8. a[4] = (float)0.2; 9. objBitmap2 = new Bitmap(objBitmap1); 10. for (int x = 1; x < objBitmap1.Width - 1; x++) 11. for (int y = 1; y < objBitmap1.Height - 1; y++) 12. { 13. Color w1 = objBitmap1.GetPixel(x - 1, y); 14. Color w2 = objBitmap1.GetPixel(x + 1, y); 15. Color w3 = objBitmap1.GetPixel(x, y - 1); 16. Color w4 = objBitmap1.GetPixel(x, y + 1); 17. Color w = objBitmap1.GetPixel(x, y); 18. int x1 = (w1.R + w1.G + w1.B) / 3; 19. int x2 = (w2.R + w2.G + w2.B) / 3; 20. int x3 = (w3.R + w3.G + w3.B) / 3; 21. int x4 = (w4.R + w4.G + w4.B) / 3; 22. int xg = (w.R + w.G + w.B) / 3; 23. int xb = (int)(a[0] * xg); 24. xb = (int)(xb + a[1] * x1 + a[2]*x2+a[3] *x3+a[4]*x4); 25. if (xb < 0) xb = 0; 26. if (xb > 255) xb = 255; 27. Color wb = Color.FromArgb(xb, xb, xb); 28. objBitmap2.SetPixel(x, y, wb); 29. } 30. pictureBox2.Image = objBitmap2; 31. float[] h = new float[256]; 32. for (int x = 0; x < objBitmap1.Width; x++) 33. for (int y = 0; y < objBitmap1.Height; y++) 34. { 35. Color w = objBitmap2.GetPixel(x, y);

7 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

36. int xg = (int)(w.R + w.G + w.B) / 3; ; 37. h[xg] = h[xg] + 1; 38. } 39. 40. for (int i = 0; i < 256; i++) 41. { 42. chart1.Series["Series1"].Points.AddXY(i, h[i]); 43. } 44. }

Output :

Analisa : Pada praktikum ini menggunakan Filter 4 Node , hasilnya gambar cenderung gelap

dilihat dari histogram yang lebih dominan berada di sebelah kiri grafiknya. Saya bandingkan

histogramnya dengan Grayscale , hasilnya berbeda. Histogram dalam gambar grayscale

menyatakan distribusi dari derajat keabuan (terang/gelap) pada suatu gambar , apakah gambar

tersebut lebih banyak warna gelap atau lebih banyak warna terang. Jika pada 4 Node ini yang

digunakan adalah nilai dari perkalian xg dengan a indeks e 0 lalu dijadikan int. int xb =

(int)(a[0] * xg);

Dan nilai xb itu tadi diinputkan lagi pada rumus ini xb = (int)(xb + a[1] * x1 + a[2]*x2+a[3] *x3+a[4]*x4);

Gambar menunjukkan frekwensi tinggi maka pada gambar tersebut banyak titik yang nilai

gray-scalenya (warna) yang berbeda jauh dengan titik-titik tetangganya.

8 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

Filter 8 Node

Source Code :

1. private void button3_Click(object sender, EventArgs e) 2. { 3. float[] a = new float[10]; 4. a[1] = (float)0.1; 5. a[2] = (float)0.1; 6. a[3] = (float)0.1; 7. a[4] = (float)0.1; 8. a[5] = (float)0.2; 9. a[6] = (float)0.1; 10. a[7] = (float)0.1; 11. a[8] = (float)0.1; 12. a[9] = (float)0.1; 13. objBitmap3 = new Bitmap(objBitmap1); 14. for (int x = 1; x < objBitmap1.Width - 1; x++) 15. for (int y = 1; y < objBitmap1.Height - 1; y++)

16. {

17. Color w1 = objBitmap1.GetPixel(x - 1, y - 1); 18. Color w2 = objBitmap1.GetPixel(x - 1, y); 19. Color w3 = objBitmap1.GetPixel(x - 1, y + 1); 20. Color w4 = objBitmap1.GetPixel(x, y - 1); 21. Color w5 = objBitmap1.GetPixel(x, y); 22. Color w6 = objBitmap1.GetPixel(x, y + 1); 23. Color w7 = objBitmap1.GetPixel(x + 1, y - 1); 24. Color w8 = objBitmap1.GetPixel(x + 1, y); 25. Color w9 = objBitmap1.GetPixel(x + 1, y + 1); 26. int x1 = (w1.R + w1.G + w1.B) / 3; 27. int x2 = (w2.R + w2.G + w2.B) / 3; 28. int x3 = (w3.R + w3.G + w3.B) / 3; 29. int x4 = (w4.R + w4.G + w4.B) / 3; 30. int x5 = (w5.R + w5.G + w5.B) / 3; 31. int x6 = (w6.R + w6.G + w6.B) / 3; 32. int x7 = (w7.R + w7.G + w7.B) / 3; 33. int x8 = (w8.R + w8.G + w8.B) / 3; 34. int x9 = (w9.R + w9.G + w9.B) / 3; 35. int xb = (int)(a[1] * x1 + a[2] * x2 + a[3] * x3); 36. xb = (int)(xb + a[4] * x4 + a[5] * x5 + a[6] * x6); 37. xb = (int)(xb + a[7] * x7 + a[8] * x8 + a[9] * x9); 38. if (xb < 0) xb = 0; 39. if (xb > 255) xb = 255; 40. Color wb = Color.FromArgb(xb, xb, xb); 41. objBitmap3.SetPixel(x, y, wb); 42. } 43. pictureBox3.Image = objBitmap3; 44. float[] h = new float[256]; 45. for (int x = 0; x < objBitmap1.Width ; x++) 46. for (int y = 0; y < objBitmap1.Height ; y++) 47. { 48. Color w = objBitmap3.GetPixel(x, y); 49. int xg = (int)(w.R + w.G + w.B) / 3; ; 50. h[xg] = h[xg] + 1; 51. } 52. for (int i = 0; i < 256; i++) 53. { 54. chart2.Series["Series1"].Points.AddXY(i , h[i]); 55. }

56. }

9 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

Output :

Analisa : Hasil pada gambar Filter 8 Node ini memiliki frekuensi yang tinggi . Dimana bila

suatu gambar menunjukkan frekwensi tinggi maka pada gambar tersebut banyak titik yang nilai

gray-scalenya (warna) yang berbeda jauh dengan titik-titik tetangganya.

TUGAS :

Ubah matrik kernel 4 node dengan matrik berikut

Tugas no 1 H=[0 −0,5 0

−0,5 0 0,50 0,5 0

]

Source Code :

1. private void button4_Click(object sender, EventArgs e) 2. { 3. float[] a = new float[5]; 4. a[0] = (float)-0.5; 5. a[1] = (float)-0.5; 6. a[2] = (float)0; 7. a[3] = (float)0.5; 8. a[4] = (float)0.5; 9. 10. objBitmap4 = new Bitmap(objBitmap1); 11. for (int x = 1; x < objBitmap1.Width - 1; x++) 12. for (int y = 1; y < objBitmap1.Height - 1; y++) 13. {

10 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

14. Color w1 = objBitmap1.GetPixel(x - 1, y); 15. Color w2 = objBitmap1.GetPixel(x + 1, y); 16. Color w3 = objBitmap1.GetPixel(x, y - 1); 17. Color w4 = objBitmap1.GetPixel(x, y + 1); 18. Color w = objBitmap1.GetPixel(x, y); 19. int x1 = (w1.R + w1.G + w1.B) / 3; 20. int x2 = (w2.R + w2.G + w2.B) / 3; 21. int x3 = (w3.R + w3.G + w3.B) / 3; 22. int x4 = (w4.R + w4.G + w4.B) / 3; 23. int xg = (w.R + w.G + w.B) / 3; 24. int xb = (int)(a[0] * xg); 25. xb=(int)(xb + a[1]*x1 + a[2]*x2+ a[3]*x3 + a[4] * x4); 26. if (xb < 0) xb = 0; 27. if (xb > 255) xb = 255; 28. Color wb = Color.FromArgb(xb, xb, xb); 29. objBitmap4.SetPixel(x, y, wb); 30. } 31. pictureBox4.Image = objBitmap4; 32. float[] h = new float[256]; 33. for (int x = 0; x < objBitmap1.Width; x++) 34. for (int y = 0; y < objBitmap1.Height ; y++) 35. { 36. Color w = objBitmap4.GetPixel(x, y); 37. int xg = (int)(w.R + w.G + w.B) / 3; ; 38. h[xg] = h[xg] + 1; 39. } 40. for (int i = 0; i < 256; i++) 41. { 42. chart3.Series["Series1"].Points.AddXY(i, h[i]); 43. } 44. }

Output :

• Analisa : Hasil dari Filter 4 Node dengan nilai tengah 0 ini memiliki nilai frekuensi

tang tinggi dapat dilihat pada hasilnya di histogram. Dimana terlihat di gambar hanya

garis garis tepi dari nilai frekuensi tinggi saja yang terlihat d=sedangkan yang frekuensi

rendah tidak terlihat bahkan jadi hitam. Untuk mempertahankan titik yang berbeda

dengan titik-titik tetangganya (proses deteksi tepi) maka dilakukan High-Pass Filter

(HPF), suatu bentuk filter yang mengambil data pada frekwensi tinggi dan membuang

data pada frekwensi rendah.Nilai HPF nya dari nilai 0 , positif dan negatif. Bila

dijumlahkan, maka jumlah dari semua nilainya sama dengan nol buktinya :

0 + -0,5 + 0 + -0,5 + 0 + 5 + 0 + 5 + 5 = 0

Tugas no 2 H=[0 −0,5 0

−0,5 1 0,50 0,5 0

]

0),( xy

yxH

11 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

1. private void button6_Click(object sender, EventArgs e) 2. { 3. float[] a = new float[5]; 4. a[0] = (float)-0.5; 5. a[1] = (float)-0.5; 6. a[2] = (float)1; 7. a[3] = (float)0.5; 8. a[4] = (float)0.5; 9. 10. objBitmap6 = new Bitmap(objBitmap1); 11. for (int x = 1; x < objBitmap1.Width - 1; x++) 12. for (int y = 1; y < objBitmap1.Height - 1; y++) 13. { 14. Color w1 = objBitmap1.GetPixel(x - 1, y); 15. Color w2 = objBitmap1.GetPixel(x + 1, y); 16. Color w3 = objBitmap1.GetPixel(x, y - 1); 17. Color w4 = objBitmap1.GetPixel(x, y + 1); 18. Color w = objBitmap1.GetPixel(x, y); 19. int x1 = (w1.R + w1.G + w1.B) / 3; 20. int x2 = (w2.R + w2.G + w2.B) / 3; 21. int x3 = (w3.R + w3.G + w3.B) / 3; 22. int x4 = (w4.R + w4.G + w4.B) / 3; 23. int xg = (w.R + w.G + w.B) / 3; 24. int xb = (int)(a[0] * xg); 25. xb = (int)(xb + a[1]* x1+a[2]*x2 +a[3]*x3 +a[4]* x4); 26. if (xb < 0) xb = 0; 27. if (xb > 255) xb = 255; 28. Color wb = Color.FromArgb(xb, xb, xb); 29. objBitmap6.SetPixel(x, y, wb); 30. } 31. pictureBox6.Image = objBitmap6; 32. float[] h = new float[256]; 33. for (int x = 0; x < objBitmap1.Width; x++) 34. for (int y = 0; y < objBitmap1.Height ; y++) 35. { 36. Color w = objBitmap6.GetPixel(x,y); 37. int xg = (int)(w.R + w.G + w.B) / 3; ; 38. h[xg] = h[xg] + 1; 39. } 40. for (int i = 0; i < 256; i++) 41. { 42. chart5.Series["Series1"].Points.AddXY(i, h[i]); 43. } 44. }

Output :

Analisa : hasilnya dari Filter 4 Node dengan nilai tengahnya 1 , memiliki frekuensi yang tinggi

dilihat saja dari histogramnya , memiliki perbedaan yang sangat mencolok terutama pada warna

149 dan 249. Gambar menunjukkan frekwensi tinggi maka pada gambar tersebut banyak titik

yang nilai gray-scalenya (warna) yang berbeda jauh dengan titik-titik tetangganya.

12 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

Ubah matrik kernel 8 node dengan matrik berikut

Tugas no 3 H=[1 −0,5 0

−0,5 0 0,50 0,5 1

]

Source Code :

1. private void button5_Click(object sender, EventArgs e) 2. { 3. float[] a = new float[10]; 4. a[0] = (float)1; 5. a[1] = (float)-0.5; 6. a[2] = (float)0; 7. a[3] = (float)-0.5; 8. a[4] = (float)0; 9. a[5] = (float)0.5; 10. a[6] = (float)0; 11. a[7] = (float)0.5; 12. a[8] = (float)1; 13. objBitmap5 = new Bitmap(objBitmap1); 14. for (int x = 1; x < objBitmap1.Width - 1; x++) 15. for (int y = 1; y < objBitmap1.Height - 1; y++) 16. { 17. Color w1 = objBitmap1.GetPixel(x - 1, y - 1); 18. Color w2 = objBitmap1.GetPixel(x - 1, y); 19. Color w3 = objBitmap1.GetPixel(x - 1, y + 1); 20. Color w4 = objBitmap1.GetPixel(x, y - 1); 21. Color w5 = objBitmap1.GetPixel(x, y); 22. Color w6 = objBitmap1.GetPixel(x, y + 1); 23. Color w7 = objBitmap1.GetPixel(x + 1, y - 1); 24. Color w8 = objBitmap1.GetPixel(x + 1, y); 25. Color w9 = objBitmap1.GetPixel(x + 1, y + 1); 26. int x1 = (w1.R + w1.G + w1.B) / 3; 27. int x2 = (w2.R + w2.G + w2.B) / 3; 28. int x3 = (w3.R + w3.G + w3.B) / 3; 29. int x4 = (w4.R + w4.G + w4.B) / 3; 30. int x5 = (w5.R + w5.G + w5.B) / 3; 31. int x6 = (w6.R + w6.G + w6.B) / 3; 32. int x7 = (w7.R + w7.G + w7.B) / 3; 33. int x8 = (w8.R + w8.G + w8.B) / 3; 34. int x9 = (w9.R + w9.G + w9.B) / 3; 35. int xb = (int)(a[1] * x1 + a[2] * x2 + a[3] * x3); 36. xb = (int)(xb + a[4] * x4 + a[5] * x5 + a[6] * x6); 37. xb = (int)(xb + a[7] * x7 + a[8] * x8 + a[9] * x9); 38. if (xb < 0) xb = 0; 39. if (xb > 255) xb = 255; 40. Color wb = Color.FromArgb(xb, xb, xb); 41. objBitmap5.SetPixel(x, y, wb); 42. } 43. pictureBox5.Image = objBitmap5; 44. float[] h = new float[256]; 45. for (int x = 0; x < objBitmap1.Width; x++) 46. for (int y = 0; y < objBitmap1.Height; y++) 47. { 48. Color w = objBitmap5.GetPixel(x,y); 49. int xg = (int)(w.R + w.G + w.B) / 3; ; 50. h[xg] = h[xg] + 1; 51. } 52. for (int i = 0; i < 256; i++) 53. { 54. chart4.Series["Series1"].Points.AddXY(i, h[i]); 55. } 56. }

13 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

Output :

Analisa : Hasil pada Filter 8 Node dengan nilai tengah 0 ini penajaman pada gambar , dapat

dilihat output gambarnya setelah dilakukan filter ,untuk mempertahankan titik yang dekat

dengan titik-titik tetangganya, dan titik yang berbeda dengan titik-titik tetangganya

(sharperness) maka dilakukan Band Pass Filter, yang berguna mempertahankan frekwensi

rendah dan tinggi yang tidak terlalu rendah dan tinggi.

Tugas no 4 H=[−1 −0,5 0−0,5 1 0,50 0,5 1

]

Source Code :

1. private void button7_Click(object sender, EventArgs e) 2. { 3. float[] a = new float[10]; 4. a[0] = (float)1; 5. a[1] = (float)-0.5; 6. a[2] = (float)0; 7. a[3] = (float)-0.5; 8. a[4] = (float)1; 9. a[5] = (float)0.5; 10. a[6] = (float)0; 11. a[7] = (float)0.5; 12. a[8] = (float)1; 13. objBitmap7 = new Bitmap(objBitmap1); 14. for (int x = 1; x < objBitmap1.Width - 1; x++) 15. for (int y = 1; y < objBitmap1.Height - 1; y++) 16. { 17. Color w1 = objBitmap1.GetPixel(x - 1, y - 1); 18. Color w2 = objBitmap1.GetPixel(x - 1, y); 19. Color w3 = objBitmap1.GetPixel(x - 1, y + 1); 20. Color w4 = objBitmap1.GetPixel(x, y - 1); 21. Color w5 = objBitmap1.GetPixel(x, y); 22. Color w6 = objBitmap1.GetPixel(x, y + 1); 23. Color w7 = objBitmap1.GetPixel(x + 1, y - 1); 24. Color w8 = objBitmap1.GetPixel(x + 1, y); 25. Color w9 = objBitmap1.GetPixel(x + 1, y + 1); 26. int x1 = (w1.R + w1.G + w1.B) / 3; 27. int x2 = (w2.R + w2.G + w2.B) / 3;

14 | M e r i n d a I c h a F – 2 1 0 3 1 3 1 0 3 5 – 2 D 3 I T B

28. int x3 = (w3.R + w3.G + w3.B) / 3; 29. int x4 = (w4.R + w4.G + w4.B) / 3; 30. int x5 = (w5.R + w5.G + w5.B) / 3; 31. int x6 = (w6.R + w6.G + w6.B) / 3; 32. int x7 = (w7.R + w7.G + w7.B) / 3; 33. int x8 = (w8.R + w8.G + w8.B) / 3; 34. int x9 = (w9.R + w9.G + w9.B) / 3; 35. int xb = (int)(a[1] * x1 + a[2] * x2 + a[3] * x3); 36. xb = (int)(xb + a[4] * x4 + a[5] * x5 + a[6] * x6); 37. xb = (int)(xb + a[7] * x7 + a[8] * x8 + a[9] * x9); 38. if (xb < 0) xb = 0; 39. if (xb > 255) xb = 255; 40. Color wb = Color.FromArgb(xb, xb, xb); 41. objBitmap7.SetPixel(x, y, wb); 42. } 43. pictureBox7.Image = objBitmap7; 44. float[] h = new float[256]; 45. for (int x = 0; x < objBitmap1.Width; x++) 46. for (int y = 0; y < objBitmap1.Height; y++) 47. { 48. Color w = objBitmap7.GetPixel(x,y); 49. int xg = (int)(w.R + w.G + w.B) / 3; ; 50. h[xg] = h[xg] + 1; 51. } 52. for (int i = 0; i < 256; i++) 53. { 54. chart6.Series["Series1"].Points.AddXY(i, h[i]); 55. } 56. }

Output :

Gambar 1

Gambar 2

Analisa : Hasil filter 8 Node dengan nilai tengah 1 ini terlihat sangat terang. Jika diamati dengan

seksama pada gambar ada bagian yang hilang pada warna yang cenderung terang. Jadi warna

seperti putih , kuning muda , merah muda pada contoh gambar 2 dari gambar diatas hilang dan

didominasi ke warna putih berbeda dengan gambar yang cenderung gelap atau mendekati 0

maka warna tersebut terlihat masih dipertahankan atau muncul pada hasil filter.