日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Silverlight中摄像头的运用—part2

發布時間:2023/12/9 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Silverlight中摄像头的运用—part2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Silverlight 4 中攝像頭的運用—part1 將跟蹤顏色視作輸入?

好了,我們能夠跟蹤到這個顏色了,那這么做的意義是什么呢?實際上,我們可以根據它的位置來移動東西。接下來的例子中,創建的一個球會跟隨這個顏色一起移動。你可以用來作出很詭異的對象跟隨畫面移動的效果。?

關鍵代碼:

=================================== //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 =?"坐標="?+ _lastPoint.X.ToString() +?","?+ _lastPoint.Y.ToString();

????????????ballctf.TranslateX = _lastPoint.X;
????????????ballctf.TranslateY = _lastPoint.Y;

????????????Debug.WriteLine(found);

===================================

?

分析移動區域

?? ?在這一節,我們雖然還不去涉及如何跟蹤物體的具體軌跡,但會知道如何判斷是否有移動。 ?一個基本概念是:如果有移動,每幀的畫面會明顯不同。所以,如果發現兩幀畫面中位圖的像素有不同的地方,就能知道發生了移動。?

有兩個潛在元素。第一,我們需要兩張位圖。第二,我們還需要一個比較函數。如果,你正在想著是否需要遍歷所有像素來進行比較,那么我告訴你,這里有一個很實用的技巧:使用混合模式。繪制時如果不指定混合模式,新的像素值就會完全覆蓋以取代存在的像素值。這也是我們至今為止一直在做的事情。如果使用混合模式,新的像素會影響已存在的像素,兩張圖片會以一種特別的方式混合在一起。而此刻,我們要用的混合模式叫做difference(差異),它對兩張圖片的紅、綠、藍三個通道的每個像素進行一次比較,然后給出它們之間的相減所得的差值。如果兩個像素完全一致,那么結果就是0,也就是黑色,否則就是別的其它什么值(顏色)。這樣,我們就把跟蹤移動的問題簡化了,只要尋找非黑色區域即可。? =================================== 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;
????????????????}
????????????}
????????}
????}
=================================== 當運行剛啟動,會出現一張純黑色的矩形。但接著就會看到鬼一樣移動的輪廓。這個輪廓就是兩幀畫面的不同之處。

接下來再使用threshold濾鏡來對圖片進行一下處理,對移動區域做更加精細的捕捉。

參考這里:http://kodierer.blogspot.com/2009/07/livin-on-edge-silverlight-parametric_4324.html

總結

以上是生活随笔為你收集整理的Silverlight中摄像头的运用—part2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。