C#数字图像处理学习(一)

开始学习数字图像处理方面的知识了,用了三天的时间写了一个简单的处理,包括图像的向下扫描,交叉飞入,百叶窗,马赛克等效果,找了一些图片测试了一下,还勉强可以用,不过不怎么理想。而且我也是初学者,想多练练手,里面还用了单件模式和抽象工厂模式,等我写完了发现的确不需要这样,弄的类太多了,导致一个类里只有一个方法,很傻,不过初衷也是同时为了练习一下设计模式的使用,也算是有点锻炼吧。

这个是我抽象工厂的设计,等我写完了,才发现写的比较笨,并且我痛恨switch case,这让代码看起来很不优雅,只是当时没考虑太多,就马虎的用了上来。

 

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5  using System.Drawing;
6  using ImageProcessScanTest.ImageProcess;
7
8  namespace ImageProcessScanTest
9 {
10 public abstract class ImageProcessFactory
11 {
12 public static ImageProcessFactory GetInstance(SpecialEffect se)
13 {
14 ImageProcessFactory instance = null;
15 switch (se)
16 {
17 case SpecialEffect.ScanDown:
18 instance = ProcessCreator.ScanDownInstance;
19 break;
20 case SpecialEffect.ScanRigth:
21 instance = ProcessCreator.ScanRightInstance;
22 break;
23 case SpecialEffect.CrossFlyIn:
24 instance = ProcessCreator.ScanCrossFlyIn;
25 break;
26 case SpecialEffect.MiddleExpasion:
27 instance = ProcessCreator.MiddleExpand;
28 break;
29 case SpecialEffect.FadeIn:
30 instance = ProcessCreator.FadeIn;
31 break;
32 case SpecialEffect.HorizontalBlinds:
33 instance = ProcessCreator.ScanHorizontalBlinds;
34 break;
35 case SpecialEffect.Mosaic:
36 instance = ProcessCreator.ScanMosaic;
37 break;
38 default:
39 break;
40 }
41 return instance;
42 }
43
44 public abstract void ImageOperate(Bitmap bp, System.Windows.Forms.PictureBox contain);
45
46 public Bitmap BitMap
47 {
48 get { return mBitmap; }
49 set { mBitmap = value;}
50 }
51
52 protected Bitmap mBitmap;
53 protected int bmpWidth;
54 protected int bmpHeigth;
55
56 }
57
58 public sealed class ProcessCreator
59 {
60 static ImageProcessFactory scanDownInstance = null;
61 static ImageProcessFactory scanRightInstance = null;
62 static ImageProcessFactory scanCrossFlyIn = null;
63 static ImageProcessFactory scanMiddleExpand = null;
64 static ImageProcessFactory scanFadeIn = null;
65 static ImageProcessFactory scanHorizontalBlinds = null;
66 static ImageProcessFactory scanMosaic = null;
67
68 public static ImageProcessFactory ScanMosaic
69 {
70 get
71 {
72 if(scanMosaic == null)
73 {
74 scanMosaic = new ImageMosaic();
75 }
76 return scanMosaic;
77 }
78 }
79 public static ImageProcessFactory ScanHorizontalBlinds
80 {
81 get
82 {
83 if (scanHorizontalBlinds == null)
84 {
85 scanHorizontalBlinds = new ImageHorizontalBlinds();
86 }
87 return scanHorizontalBlinds;
88 }
89 }
90
91 public static ImageProcessFactory ScanDownInstance
92 {
93 get
94 {
95 if (scanDownInstance == null)
96 {
97 scanDownInstance = new ImageScanDown();
98 }
99 return scanDownInstance;
100 }
101 }
102 public static ImageProcessFactory ScanRightInstance
103 {
104 get
105 {
106 if (scanRightInstance == null)
107 {
108 scanRightInstance = new ImageScanRight();
109 }
110 return scanRightInstance;
111 }
112 }
113 public static ImageProcessFactory ScanCrossFlyIn
114 {
115 get
116 {
117 if (scanCrossFlyIn == null)
118 {
119 scanCrossFlyIn = new ImageCrossFlyIn();
120 }
121 return scanCrossFlyIn;
122 }
123 }
124 public static ImageProcessFactory MiddleExpand
125 {
126 get
127 {
128 if (scanMiddleExpand == null)
129 {
130 scanMiddleExpand = new ImageMiddleExpand();
131 }
132 return scanMiddleExpand;
133 }
134 }
135 public static ImageProcessFactory FadeIn
136 {
137 get
138 {
139 if (scanFadeIn == null)
140 {
141 scanFadeIn = new ImageFadeIn();
142 }
143 return scanFadeIn;
144 }
145 }
146 }
147
148 public enum SpecialEffect
149 {
150 ScanDown,
151 ScanRigth,
152 CrossFlyIn,
153 MiddleExpasion,
154 MiddleShrink,
155 Bar,
156 FadeIn,
157 HorizontalBlinds,
158 VerticalBlinds,
159 Mosaic
160 }
161 }

 

下面是各个效果的具体实现,我写在了一起,看这很别扭,第一次是不是都这样呢?开始写的时候还觉得挺有意思的,后来才发现真的是在堆砌代码,有种不想写下去的冲动,不我还是坚持写完了,也有偷懒,少写了几种效果,不过都差不太多。由于我对位图理解的过于肤浅,所以我不知道怎样把一个位图切割成很多块的小的位图,因而用了大量的Clone方法。

 

 

代码
1 using System;
2 using System.Collections.Generic;
3 using System.Collections;
4 using System.Linq;
5 using System.IO;
6 using System.Text;
7 using System.Drawing;
8 using System.Drawing.Imaging;
9 using System.Threading;
10 using System.Runtime.InteropServices;
11
12 namespace ImageProcessScanTest.ImageProcess
13 {
14 public class ImageScanDown : ImageProcessFactory
15 {
16 public override void ImageOperate(Bitmap bp, System.Windows.Forms.PictureBox contain)
17 {
18 try
19 {
20 BitmapData bmpData = bp.LockBits(new Rectangle(0, 0, bp.Width, bp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
21 IntPtr ptr = bmpData.Scan0;
22 Graphics g = contain.CreateGraphics();
23 for (int i = 1; i <= bp.Height; i++ )
24 {
25 Bitmap temp = new Bitmap(bp.Width, i, bmpData.Stride, PixelFormat.Format24bppRgb, ptr);
26 g.DrawImageUnscaled(temp, 0, 0, temp.Width, temp.Height);
27 System.Threading.Thread.Sleep(3);
28 }
29 bp.UnlockBits(bmpData);
30 }
31 catch (System.Exception ex)
32 {
33 Console.WriteLine(ex.Message);
34 }
35 }
36 }
37
38 public class ImageScanRight : ImageProcessFactory
39 {
40 public override void ImageOperate(Bitmap bp, System.Windows.Forms.PictureBox contain)
41 {
42 try
43 {
44 BitmapData bmpData = bp.LockBits(new Rectangle(0, 0, bp.Width, bp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
45 IntPtr ptr = bmpData.Scan0;
46 Graphics g = contain.CreateGraphics();
47 for (int i = 1; i <= bp.Width; i++)
48 {
49 Bitmap temp = new Bitmap(i, bp.Height, bmpData.Stride, PixelFormat.Format24bppRgb, ptr);
50 g.DrawImageUnscaled(temp, 0, 0, temp.Width, temp.Height);
51 System.Threading.Thread.Sleep(3);
52 }
53 bp.UnlockBits(bmpData);
54 }
55 catch (System.Exception ex)
56 {
57 Console.WriteLine(ex.Message);
58 }
59 }
60 }
61
62 public class ImageCrossFlyIn : ImageProcessFactory
63 {
64 public override void ImageOperate(Bitmap bp, System.Windows.Forms.PictureBox contain)
65 {
66 try
67 {
68 BitmapData bmpData = bp.LockBits(new Rectangle(0, 0, bp.Width, bp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
69 IntPtr ptr = bmpData.Scan0;
70 Graphics g = contain.CreateGraphics();
71 int height = bp.Height / 2;
72 int width = bp.Width;
73 for (int i = 1; i <= width; i++)
74 {
75 Bitmap tempLeft = new Bitmap(i, height, bmpData.Stride, PixelFormat.Format24bppRgb, ptr);
76 g.DrawImageUnscaled(tempLeft, 0, 0, tempLeft.Width, height);
77 Bitmap tempRight = bp.Clone(new Rectangle((width - i), height, i, height), PixelFormat.Format24bppRgb);
78 g.DrawImageUnscaled(tempRight, width - i, height, tempRight.Width, height);
79 System.Threading.Thread.Sleep(3);
80 }
81 bp.UnlockBits(bmpData);
82 }
83 catch (System.Exception ex)
84 {
85 Console.WriteLine(ex.Message);
86 }
87 }
88 }
89
90 public class ImageMiddleExpand : ImageProcessFactory
91 {
92 public override void ImageOperate(Bitmap bp, System.Windows.Forms.PictureBox contain)
93 {
94 try
95 {
96 BitmapData bmpData = bp.LockBits(new Rectangle(0, 0, bp.Width, bp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
97 IntPtr ptr = bmpData.Scan0;
98 Graphics g = contain.CreateGraphics();
99 int width = bp.Width;
100 if (bp.Height % 2 == 0)
101 {
102 int height = bp.Height / 2;
103 for (int i = 1; i <= height; i++)
104 {
105 Bitmap tempUp = bp.Clone(new Rectangle(0, height - i + 1, width, i), PixelFormat.Format24bppRgb);
106 Bitmap tempDown = bp.Clone(new Rectangle(0, height + i - 1, width, i), PixelFormat.Format24bppRgb);
107 g.DrawImageUnscaled(tempUp, 0, height - i + 1, width, tempUp.Height);
108 g.DrawImageUnscaled(tempDown, 0, height + i - 1, width, tempDown.Height);
109 System.Threading.Thread.Sleep(3);
110 }
111 }
112 else
113 {
114 int heightUp = bp.Height / 2;
115 int heightDown = heightUp + 1;
116 for (int i = 1; i <= bp.Height / 2 + 1; i++)
117 {
118 Bitmap tempUp = bp.Clone(new Rectangle(0, heightUp - i + 1, width, i), PixelFormat.Format24bppRgb);
119 Bitmap tempDown = bp.Clone(new Rectangle(0, heightDown + i - 1, width, i), PixelFormat.Format24bppRgb);
120 g.DrawImageUnscaled(tempUp, 0, heightUp - i + 1, width, tempUp.Height);
121 g.DrawImageUnscaled(tempDown, 0, heightDown + i - 1, width, tempDown.Height);
122 System.Threading.Thread.Sleep(3);
123 }
124 }
125 bp.UnlockBits(bmpData);
126 }
127 catch (System.Exception ex)
128 {
129 Console.WriteLine(ex.Message);
130 }
131 }
132 }
133
134 public class ImageFadeIn : ImageProcessFactory
135 {
136 public override void ImageOperate(Bitmap bp, System.Windows.Forms.PictureBox contain)
137 {
138 try
139 {
140 int bpWidth = bp.Width;
141 int bpHeight = bp.Height;
142 Graphics g = contain.CreateGraphics();
143 byte[]store = new byte[bpWidth * bpHeight * 3];
144 BitmapData bpData = bp.LockBits(new Rectangle(0, 0, bpWidth, bpHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
145 unsafe
146 {
147 byte* p = (byte*)bpData.Scan0;
148 int start = 0;
149 for (int i = 0; i < bpHeight; i++)
150 {
151 for (int j = 0; j < bpWidth; j++)
152 {
153 store[start] = p[0];
154 store[start + 1] = p[1];
155 store[start + 2] = p[2];
156 start += 3;
157 p += 3;
158 }
159 p += bpData.Stride - bpData.Width * 3;
160 }
161 }
162 bp.UnlockBits(bpData);
163 for (int n = 30; n > 0; n-- )
164 {
165 BitmapData bmpData = bp.LockBits(new Rectangle(0, 0, bpWidth, bpHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
166 IntPtr ptr = bmpData.Scan0;
167 unsafe
168 {
169 byte* p = (byte*)ptr;
170 int start = 0;
171 for (int i = 0; i < bpHeight; i++)
172 {
173 for (int j = 0; j < bpWidth; j++)
174 {
175 p[0] = Convert.ToByte(store[start] / n);
176 p[1] = Convert.ToByte(store[start + 1] / n);
177 p[2] = Convert.ToByte(store[start + 2] / n);
178 p += 3;
179 start += 3;
180 }
181 p += bmpData.Stride - bmpData.Width * 3;
182 }
183 }
184 bp.UnlockBits(bmpData);
185 g.DrawImageUnscaled(bp, 0, 0, bpWidth, bpHeight);
186 System.Threading.Thread.Sleep(20);
187 }
188
189 }
190 catch (System.Exception ex)
191 {
192 Console.WriteLine(ex.Message);
193 }
194 }
195 }
196
197 public class ImageHorizontalBlinds : ImageProcessFactory
198 {
199 public override void ImageOperate(Bitmap bp, System.Windows.Forms.PictureBox contain)
200 {
201 try
202 {
203 int bpHeight = bp.Height;
204 int bpWidth = bp.Width;
205 int usedHeight = bpHeight / 10 - 1;
206 BitmapData bmpData = bp.LockBits(new Rectangle(0, 0, bp.Width, bp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
207 int bpStride = bmpData.Stride;
208 Graphics g = contain.CreateGraphics();
209 for (int i = 1; i < bpHeight / 10; i++ )
210 {
211 Bitmap bp0 = bp.Clone(new Rectangle(0, 0, bpWidth, i), PixelFormat.Format24bppRgb);
212 Bitmap bp1 = bp.Clone(new Rectangle(0, usedHeight, bpWidth, i), PixelFormat.Format24bppRgb);
213 Bitmap bp2 = bp.Clone(new Rectangle(0, usedHeight * 2, bpWidth, i), PixelFormat.Format24bppRgb);
214 Bitmap bp3 = bp.Clone(new Rectangle(0, usedHeight * 3, bpWidth, i), PixelFormat.Format24bppRgb);
215 Bitmap bp4 = bp.Clone(new Rectangle(0, usedHeight * 4, bpWidth, i), PixelFormat.Format24bppRgb);
216 Bitmap bp5 = bp.Clone(new Rectangle(0, usedHeight * 5, bpWidth, i), PixelFormat.Format24bppRgb);
217 Bitmap bp6 = bp.Clone(new Rectangle(0, usedHeight * 6, bpWidth, i), PixelFormat.Format24bppRgb);
218 Bitmap bp7 = bp.Clone(new Rectangle(0, usedHeight * 7, bpWidth, i), PixelFormat.Format24bppRgb);
219 Bitmap bp8 = bp.Clone(new Rectangle(0, usedHeight * 8, bpWidth, i), PixelFormat.Format24bppRgb);
220 Bitmap bp9 = bp.Clone(new Rectangle(0, usedHeight * 9, bpWidth, i), PixelFormat.Format24bppRgb);
221 g.DrawImageUnscaled(bp0, 0, 0);
222 g.DrawImageUnscaled(bp1, 0, usedHeight);
223 g.DrawImageUnscaled(bp2, 0, usedHeight * 2);
224 g.DrawImageUnscaled(bp3, 0, usedHeight * 3);
225 g.DrawImageUnscaled(bp4, 0, usedHeight * 4);
226 g.DrawImageUnscaled(bp5, 0, usedHeight * 5);
227 g.DrawImageUnscaled(bp6, 0, usedHeight * 6);
228 g.DrawImageUnscaled(bp7, 0, usedHeight * 7);
229 g.DrawImageUnscaled(bp8, 0, usedHeight * 8);
230 g.DrawImageUnscaled(bp9, 0, usedHeight * 9);
231 System.Threading.Thread.Sleep(5);
232 }
233 bp.UnlockBits(bmpData);
234 }
235 catch (System.Exception ex)
236 {
237 Console.WriteLine(ex.Message);
238 }
239 }
240 }
241
242 public class ImageMosaic : ImageProcessFactory
243 {
244 Random ra = new Random();
245 public override void ImageOperate(Bitmap bp, System.Windows.Forms.PictureBox contain)
246 {
247 try
248 {
249 Graphics g = contain.CreateGraphics();
250 List<Bitmap> bpList = new List<Bitmap>();
251 List<Point> pointList = new List<Point>();
252 int usedWidth = bp.Width / 10;
253 int usedHeight = bp.Height / 10;
254 for (int i = 0; i < 10; i++ )
255 {
256 for (int j = 0; j < 10; j++ )
257 {
258 bpList.Add(bp.Clone(new Rectangle(i * usedWidth, j * usedHeight, usedWidth, usedHeight), PixelFormat.Format24bppRgb));
259 pointList.Add(new Point(i * usedWidth, j * usedHeight));
260 }
261 }
262 int[]source = new int[100];
263 int[] randomOrder = new int[100];
264 for (int i = 0; i < 100; i++ )
265 {
266 source[i] = i;
267 }
268 GetRandom(source, randomOrder, ra);
269 foreach (int index in randomOrder)
270 {
271 g.DrawImageUnscaled(bpList[index], pointList[index]);
272 System.Threading.Thread.Sleep(20);
273 }
274 }
275 catch (System.Exception ex)
276 {
277 Console.WriteLine(ex.Message);
278 }
279 }
280
281 protected void GetRandom(int[]source, int[]dest, Random ra)
282 {
283 try
284 {
285 if (dest[dest.Length - 1] != 0)
286 {
287 return;
288 }
289 int tempValue = ra.Next(source.Length - 1);
290 for(int i = 0; i < dest.Length; i++)
291 {
292 if (dest[i] == 0)
293 {
294 dest[i] = source[tempValue];
295 break;
296 }
297 }
298 int[] _source = new int[source.Length - 1];
299 for (int i = 0; i < source.Length; i++ )
300 {
301 if (i < tempValue)
302 {
303 _source[i] = source[i];
304 }
305 else if (i == tempValue)
306 {
307
308 }
309 else
310 {
311 _source[i - 1] = source[i];
312 }
313 }
314 GetRandom(_source, dest, ra);
315 }
316 catch (System.Exception ex)
317 {
318 Console.WriteLine(ex.Message);
319 }
320
321 }
322 }
323 }

 

 

主窗体

 

代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageProcessScanTest.ImageProcess;

namespace ImageProcessScanTest
{
public partial class MainForm : Form
{
private Bitmap mBitmap = null;
private bool first = true;
private Dictionary<RadioButton, SpecialEffect> dict = new Dictionary<RadioButton, SpecialEffect>();
public MainForm()
{
InitializeComponent();

//my init
dict.Add(this.radioButton_Bar, SpecialEffect.Bar);
dict.Add(
this.radioButton_CrossFlyIn, SpecialEffect.CrossFlyIn);
dict.Add(
this.radioButton_DownScan, SpecialEffect.ScanDown);
dict.Add(
this.radioButton_FadeIn, SpecialEffect.FadeIn);
dict.Add(
this.radioButton_horizontalBlinds, SpecialEffect.HorizontalBlinds);
dict.Add(
this.radioButton_MiddleExpand, SpecialEffect.MiddleExpasion);
dict.Add(
this.radioButton_MiddleShrink, SpecialEffect.MiddleShrink);
dict.Add(
this.radioButton_Mosaic, SpecialEffect.Mosaic);
dict.Add(
this.radioButton_RightScan, SpecialEffect.ScanRigth);
dict.Add(
this.radioButton_verticalBlinds, SpecialEffect.VerticalBlinds);
button_ChooseImage.Enabled
= true;
button_Scan.Enabled
= false;
}

private void button_ChooseImage_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog
= new OpenFileDialog();
if (fileDialog.ShowDialog() == DialogResult.OK)
{
if (fileDialog.FileName != null)
{
mBitmap
= new Bitmap(fileDialog.FileName);
ButtonStateChange(button_ChooseImage);
if (first)
{
ButtonStateChange(button_Scan);
first
= false;
}

Graphics g
= pictureBox_PreView.CreateGraphics();
g.DrawImage(mBitmap,
new Rectangle(0, 0, this.pictureBox_PreView.Width, this.pictureBox_PreView.Height));
}
}
}

private void button_Scan_Click(object sender, EventArgs e)
{
ButtonStateChange(button_Scan);
foreach (KeyValuePair<RadioButton, SpecialEffect>value in dict)
{
if (value.Key.Checked)
{
ImageProcessFactory.GetInstance(value.Value).ImageOperate((mBitmap.Clone()
as Bitmap), pictureBox_Show);
}
}
ButtonStateChange(button_ChooseImage);
ButtonStateChange(button_Scan);
}

private void ButtonStateChange(Button button)
{
if (button.Enabled)
{
button.Enabled
= false;
}
else
{
button.Enabled
= true;
}
}

private void OnPictureBoxClicked(object sender, MouseEventArgs e)
{
if (sender == pictureBox_Show)
{
pictureBox_Show.Image
= null;
}
}
}
}

 

 

 

源代码

这个是我全部代码的链接 ,有兴趣的可以下载看看,并恳请大家多提意见。

posted @ 2010-11-12 16:00  幽灵主  阅读(587)  评论(0编辑  收藏  举报