Silverlight中摄像头的运用—part2
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Silverlight中摄像头的运用—part2
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                
                            
                            
                            Silverlight 4 中攝像頭的運(yùn)用—part1  將跟蹤顏色視作輸入? 
ball.Width = 20; ball.Height = 20; ball.Fill = new SolidColorBrush(Colors.Orange); ballctf = new CompositeTransform(); ball.RenderTransform = ballctf;
????????????labRes.Content =?"捕獲顏色="?+ found.ToString();
????????????labPoint.Content =?"坐標(biāo)="?+ _lastPoint.X.ToString() +?","?+ _lastPoint.Y.ToString();
????????????ballctf.TranslateX = _lastPoint.X;
????????????ballctf.TranslateY = _lastPoint.Y;
????????????Debug.WriteLine(found);
===================================
????{
????????CaptureSource?_captureSource;
????????VideoCaptureDevice?_video;
????????VideoBrush?_videoBrush;
????????Rectangle?_rect;
????????Image?_wb_image;
????????WriteableBitmap?_wb =?null;
????????bool?_isEnableCamera =?false;
????????WriteableBitmap?_newFrameBitmap;
????????WriteableBitmap?_oldFrameBitmap;
????????Image?_newFrame;
????????Image?_oldFrame;
????????public?MotionTracking()
????????{
????????????InitializeComponent();
????????????_captureSource =?new?CaptureSource();
????????????_video =?CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
????????????if?(_video !=?null)
????????????{
????????????????_video.DesiredFormat = _video.SupportedFormats[1];
????????????????_captureSource.VideoCaptureDevice = _video;
????????????????_videoBrush =?new?VideoBrush();
????????????????_videoBrush.SetSource(_captureSource);
????????????}
????????????CompositionTarget.Rendering +=?new?EventHandler(OnRender);
????????????btnStart.Click +=?new?RoutedEventHandler(btnStart_Click);
????????????this.imageEffectsListBox.Items.Add(new?NormalEffect());
????????????this.imageEffectsListBox.Items.Add(new?DarkenEffect());
????????????this.imageEffectsListBox.Items.Add(new?MultiplyEffect());
????????????this.imageEffectsListBox.Items.Add(new?ColorBurnEffect());
????????????this.imageEffectsListBox.Items.Add(new?LinearBurnEffect());
????????????this.imageEffectsListBox.Items.Add(new?LightenEffect());
????????????this.imageEffectsListBox.Items.Add(new?ScreenEffect());
????????????this.imageEffectsListBox.Items.Add(new?ColorDodgeEffect());
????????????this.imageEffectsListBox.Items.Add(new?LinearDodgeEffect());
????????????this.imageEffectsListBox.Items.Add(new?OverlayEffect());
????????????this.imageEffectsListBox.Items.Add(new?SoftLightEffect());
????????????this.imageEffectsListBox.Items.Add(new?HardLightEffect());
????????????this.imageEffectsListBox.Items.Add(new?VividLightEffect());
????????????this.imageEffectsListBox.Items.Add(new?LinearLightEffect());
????????????this.imageEffectsListBox.Items.Add(new?PinLightEffect());
????????????this.imageEffectsListBox.Items.Add(new?DifferenceEffect());
????????????this.imageEffectsListBox.Items.Add(new?ExclusionEffect());
????????????this.imageEffectsListBox.Items.Add(new?GlowEffect());
????????????this.imageEffectsListBox.Items.Add(new?ReflectEffect());
????????????this.imageEffectsListBox.Items.Add(new?HardMixEffect());
????????????this.imageEffectsListBox.Items.Add(new?NegationEffect());
????????????this.imageEffectsListBox.Items.Add(new?PhoenixEffect());
????????????this.imageEffectsListBox.Items.Add(new?AverageEffect());
????????????this.imageEffectsListBox.SelectedIndex = 0;
????????}
????????void?btnStart_Click(object?sender,?RoutedEventArgs?e)
????????{
????????????if?(CaptureDeviceConfiguration.AllowedDeviceAccess ||
????????????????CaptureDeviceConfiguration.RequestDeviceAccess())
????????????{
????????????????_rect =?new?Rectangle();
????????????????_rect.Width = 320;
????????????????_rect.Height = 240;
????????????????_rect.Fill = _videoBrush;
????????????????_rect.Visibility =?Visibility.Collapsed;
????????????????video_Canvas.Children.Add(_rect);
????????????????_newFrameBitmap =?new?WriteableBitmap(_rect,?null);
????????????????_oldFrameBitmap =?new?WriteableBitmap(_rect,?null);
????????????????_newFrame =?new?Image();
????????????????_newFrame.Width = 320;
????????????????_newFrame.Height = 240;
????????????????_newFrame.Source = _newFrameBitmap;
????????????????_newFrame.Visibility =?Visibility.Collapsed;
????????????????_oldFrame =?new?Image();
????????????????_oldFrame.Width = 320;
????????????????_oldFrame.Height = 240;
????????????????_oldFrame.Source = _oldFrameBitmap;
????????????????video_Canvas.Children.Add(_oldFrame);
????????????????video_Canvas.Children.Add(_newFrame);
????????????????
????????????????_captureSource.Start();
????????????????_isEnableCamera =?true;
????????????????Thread?thread =?new?Thread(new?ThreadStart(ThreadProc));
????????????????thread.Start();
????????????}
????????}
????????void?OnRender(object?sender,?EventArgs?e)
????????{
????????????if?(_isEnableCamera)
????????????{
????????????????MatrixTransform?transform =?new?MatrixTransform();
????????????????transform.Matrix =?new?Matrix(-1, 0, 0, 1, 320, 0);
????????????????
????????????????_newFrameBitmap.Render(_rect, transform);
????????????????_newFrameBitmap.Invalidate();
????????????}
????????}
????????void?ThreadProc()
????????{
????????????while?(true)
????????????{
????????????????Thread.Sleep(20);
????????????????//Do the action in the UI thread
????????????????Dispatcher.BeginInvoke(ThreadUpdate);
????????????}
????????}
????????void?ThreadUpdate()
????????{
????????????if?(_isEnableCamera)
????????????{
????????????????_oldFrameBitmap.Render(_newFrame,?null);
????????????????_oldFrameBitmap.Invalidate();
????????????}
????????}
????????private?void?imageEffectsListBox_SelectionChanged(object?sender,?SelectionChangedEventArgs?e)
????????{
????????????BlendModeEffect?effect = e.AddedItems[0]?as?BlendModeEffect;
????????????if?(effect !=?null)
????????????{
????????????????if?(_oldFrameBitmap !=?null)
????????????????{
????????????????????ImageBrush?_newImageBrush =?new?ImageBrush();
????????????????????_newImageBrush.ImageSource = _newFrameBitmap;
????????????????????ImageBrush?_oldImageBrush =?new?ImageBrush();
????????????????????_oldImageBrush.ImageSource = _oldFrameBitmap;
????????????????????//effect.AInput = _oldImageBrush;
????????????????????effect.BInput = _newImageBrush;
????????????????????this._oldFrame.Effect = effect;
????????????????}
????????????}
????????}
????}
=================================== 當(dāng)運(yùn)行剛啟動(dòng),會(huì)出現(xiàn)一張純黑色的矩形。但接著就會(huì)看到鬼一樣移動(dòng)的輪廓。這個(gè)輪廓就是兩幀畫面的不同之處。
                        
                        
                        好了,我們能夠跟蹤到這個(gè)顏色了,那這么做的意義是什么呢?實(shí)際上,我們可以根據(jù)它的位置來移動(dòng)?xùn)|西。接下來的例子中,創(chuàng)建的一個(gè)球會(huì)跟隨這個(gè)顏色一起移動(dòng)。你可以用來作出很詭異的對(duì)象跟隨畫面移動(dòng)的效果。?
關(guān)鍵代碼:
=================================== //part2 Ellipse ball = new Ellipse(); CompositeTransform ballctf;?
ball.Width = 20; ball.Height = 20; ball.Fill = new SolidColorBrush(Colors.Orange); ballctf = new CompositeTransform(); ball.RenderTransform = ballctf;
????????????labRes.Content =?"捕獲顏色="?+ found.ToString();
????????????labPoint.Content =?"坐標(biāo)="?+ _lastPoint.X.ToString() +?","?+ _lastPoint.Y.ToString();
????????????ballctf.TranslateX = _lastPoint.X;
????????????ballctf.TranslateY = _lastPoint.Y;
????????????Debug.WriteLine(found);
===================================
?
分析移動(dòng)區(qū)域?? ?在這一節(jié),我們雖然還不去涉及如何跟蹤物體的具體軌跡,但會(huì)知道如何判斷是否有移動(dòng)。 ?一個(gè)基本概念是:如果有移動(dòng),每幀的畫面會(huì)明顯不同。所以,如果發(fā)現(xiàn)兩幀畫面中位圖的像素有不同的地方,就能知道發(fā)生了移動(dòng)。?
有兩個(gè)潛在元素。第一,我們需要兩張位圖。第二,我們還需要一個(gè)比較函數(shù)。如果,你正在想著是否需要遍歷所有像素來進(jìn)行比較,那么我告訴你,這里有一個(gè)很實(shí)用的技巧:使用混合模式。繪制時(shí)如果不指定混合模式,新的像素值就會(huì)完全覆蓋以取代存在的像素值。這也是我們至今為止一直在做的事情。如果使用混合模式,新的像素會(huì)影響已存在的像素,兩張圖片會(huì)以一種特別的方式混合在一起。而此刻,我們要用的混合模式叫做difference(差異),它對(duì)兩張圖片的紅、綠、藍(lán)三個(gè)通道的每個(gè)像素進(jìn)行一次比較,然后給出它們之間的相減所得的差值。如果兩個(gè)像素完全一致,那么結(jié)果就是0,也就是黑色,否則就是別的其它什么值(顏色)。這樣,我們就把跟蹤移動(dòng)的問題簡(jiǎn)化了,只要尋找非黑色區(qū)域即可。? =================================== public?partial?class?MotionTracking?:?UserControl????{
????????CaptureSource?_captureSource;
????????VideoCaptureDevice?_video;
????????VideoBrush?_videoBrush;
????????Rectangle?_rect;
????????Image?_wb_image;
????????WriteableBitmap?_wb =?null;
????????bool?_isEnableCamera =?false;
????????WriteableBitmap?_newFrameBitmap;
????????WriteableBitmap?_oldFrameBitmap;
????????Image?_newFrame;
????????Image?_oldFrame;
????????public?MotionTracking()
????????{
????????????InitializeComponent();
????????????_captureSource =?new?CaptureSource();
????????????_video =?CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
????????????if?(_video !=?null)
????????????{
????????????????_video.DesiredFormat = _video.SupportedFormats[1];
????????????????_captureSource.VideoCaptureDevice = _video;
????????????????_videoBrush =?new?VideoBrush();
????????????????_videoBrush.SetSource(_captureSource);
????????????}
????????????CompositionTarget.Rendering +=?new?EventHandler(OnRender);
????????????btnStart.Click +=?new?RoutedEventHandler(btnStart_Click);
????????????this.imageEffectsListBox.Items.Add(new?NormalEffect());
????????????this.imageEffectsListBox.Items.Add(new?DarkenEffect());
????????????this.imageEffectsListBox.Items.Add(new?MultiplyEffect());
????????????this.imageEffectsListBox.Items.Add(new?ColorBurnEffect());
????????????this.imageEffectsListBox.Items.Add(new?LinearBurnEffect());
????????????this.imageEffectsListBox.Items.Add(new?LightenEffect());
????????????this.imageEffectsListBox.Items.Add(new?ScreenEffect());
????????????this.imageEffectsListBox.Items.Add(new?ColorDodgeEffect());
????????????this.imageEffectsListBox.Items.Add(new?LinearDodgeEffect());
????????????this.imageEffectsListBox.Items.Add(new?OverlayEffect());
????????????this.imageEffectsListBox.Items.Add(new?SoftLightEffect());
????????????this.imageEffectsListBox.Items.Add(new?HardLightEffect());
????????????this.imageEffectsListBox.Items.Add(new?VividLightEffect());
????????????this.imageEffectsListBox.Items.Add(new?LinearLightEffect());
????????????this.imageEffectsListBox.Items.Add(new?PinLightEffect());
????????????this.imageEffectsListBox.Items.Add(new?DifferenceEffect());
????????????this.imageEffectsListBox.Items.Add(new?ExclusionEffect());
????????????this.imageEffectsListBox.Items.Add(new?GlowEffect());
????????????this.imageEffectsListBox.Items.Add(new?ReflectEffect());
????????????this.imageEffectsListBox.Items.Add(new?HardMixEffect());
????????????this.imageEffectsListBox.Items.Add(new?NegationEffect());
????????????this.imageEffectsListBox.Items.Add(new?PhoenixEffect());
????????????this.imageEffectsListBox.Items.Add(new?AverageEffect());
????????????this.imageEffectsListBox.SelectedIndex = 0;
????????}
????????void?btnStart_Click(object?sender,?RoutedEventArgs?e)
????????{
????????????if?(CaptureDeviceConfiguration.AllowedDeviceAccess ||
????????????????CaptureDeviceConfiguration.RequestDeviceAccess())
????????????{
????????????????_rect =?new?Rectangle();
????????????????_rect.Width = 320;
????????????????_rect.Height = 240;
????????????????_rect.Fill = _videoBrush;
????????????????_rect.Visibility =?Visibility.Collapsed;
????????????????video_Canvas.Children.Add(_rect);
????????????????_newFrameBitmap =?new?WriteableBitmap(_rect,?null);
????????????????_oldFrameBitmap =?new?WriteableBitmap(_rect,?null);
????????????????_newFrame =?new?Image();
????????????????_newFrame.Width = 320;
????????????????_newFrame.Height = 240;
????????????????_newFrame.Source = _newFrameBitmap;
????????????????_newFrame.Visibility =?Visibility.Collapsed;
????????????????_oldFrame =?new?Image();
????????????????_oldFrame.Width = 320;
????????????????_oldFrame.Height = 240;
????????????????_oldFrame.Source = _oldFrameBitmap;
????????????????video_Canvas.Children.Add(_oldFrame);
????????????????video_Canvas.Children.Add(_newFrame);
????????????????
????????????????_captureSource.Start();
????????????????_isEnableCamera =?true;
????????????????Thread?thread =?new?Thread(new?ThreadStart(ThreadProc));
????????????????thread.Start();
????????????}
????????}
????????void?OnRender(object?sender,?EventArgs?e)
????????{
????????????if?(_isEnableCamera)
????????????{
????????????????MatrixTransform?transform =?new?MatrixTransform();
????????????????transform.Matrix =?new?Matrix(-1, 0, 0, 1, 320, 0);
????????????????
????????????????_newFrameBitmap.Render(_rect, transform);
????????????????_newFrameBitmap.Invalidate();
????????????}
????????}
????????void?ThreadProc()
????????{
????????????while?(true)
????????????{
????????????????Thread.Sleep(20);
????????????????//Do the action in the UI thread
????????????????Dispatcher.BeginInvoke(ThreadUpdate);
????????????}
????????}
????????void?ThreadUpdate()
????????{
????????????if?(_isEnableCamera)
????????????{
????????????????_oldFrameBitmap.Render(_newFrame,?null);
????????????????_oldFrameBitmap.Invalidate();
????????????}
????????}
????????private?void?imageEffectsListBox_SelectionChanged(object?sender,?SelectionChangedEventArgs?e)
????????{
????????????BlendModeEffect?effect = e.AddedItems[0]?as?BlendModeEffect;
????????????if?(effect !=?null)
????????????{
????????????????if?(_oldFrameBitmap !=?null)
????????????????{
????????????????????ImageBrush?_newImageBrush =?new?ImageBrush();
????????????????????_newImageBrush.ImageSource = _newFrameBitmap;
????????????????????ImageBrush?_oldImageBrush =?new?ImageBrush();
????????????????????_oldImageBrush.ImageSource = _oldFrameBitmap;
????????????????????//effect.AInput = _oldImageBrush;
????????????????????effect.BInput = _newImageBrush;
????????????????????this._oldFrame.Effect = effect;
????????????????}
????????????}
????????}
????}
=================================== 當(dāng)運(yùn)行剛啟動(dòng),會(huì)出現(xiàn)一張純黑色的矩形。但接著就會(huì)看到鬼一樣移動(dòng)的輪廓。這個(gè)輪廓就是兩幀畫面的不同之處。
接下來再使用threshold濾鏡來對(duì)圖片進(jìn)行一下處理,對(duì)移動(dòng)區(qū)域做更加精細(xì)的捕捉。
參考這里:http://kodierer.blogspot.com/2009/07/livin-on-edge-silverlight-parametric_4324.html總結(jié)
以上是生活随笔為你收集整理的Silverlight中摄像头的运用—part2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: mysql sqlexception_c
- 下一篇: XP下 无法定位程序输入点WSAPoll
