用途
經過使用C++ + Opencv 編寫算法,而後用WPF(C#)編寫程序界面,實現交互算法
能夠參考MSDN文檔:https://docs.microsoft.com/en-us/cpp/windows/pin-ptr-cpp-cli?view=vs-2017express
項目結構
代碼
CLR部分:
CLRLibrary.h文件 windows
#pragma once using namespace System; using namespace System::Collections::Generic; #include<opencv2\opencv.hpp> #include<opencv2\highgui\highgui.hpp> namespace CLRLibrary { public ref class Class1 { public: List<Byte>^ ErodeImage(System::String^ filename); int Rows; int Cols; }; }
CLRLibrary.cpp文件微信
#include "stdafx.h" using namespace System; using namespace System::Runtime::InteropServices; #include "CLRLibrary.h" #include "H:\CPP\WPF_CPPCLI_Demo\ConsoleLibrary\ErodeClass.h" #include<opencv2\opencv.hpp> #include<opencv2\highgui\highgui.hpp> using namespace cv; Mat ErodeAction(const char* filename) { Mat img = imread(filename); Mat out; //獲取自定義核 Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); //第一個參數MORPH_RECT表示矩形的卷積核,固然還能夠選擇橢圓形的、交叉型的 //膨脹操做 dilate(img, out, element); return out; } //Erode The Image Func List<Byte>^ CLRLibrary::Class1::ErodeImage(System::String ^ filename) { //將String^轉換成const char* IntPtr ip = Marshal::StringToHGlobalAnsi(filename); const char* str = static_cast<const char*>(ip.ToPointer()); Mat result=ErodeAction(str); List<Byte>^ ret=gcnew List<Byte>(); for (int i = 0; i < result.rows; i++) { for (int j = 0; j < result.cols; j++) { //RBG ret->Add(result.at<Vec3b>(i, j)[0]); ret->Add(result.at<Vec3b>(i, j)[1]); ret->Add(result.at<Vec3b>(i, j)[2]); } } Rows = result.rows; Cols = result.cols; return ret; }
WPF部分:
MainWindow.xaml:ui
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" Loaded="MainWindow_OnLoaded"> <Grid> <Image Name="Box"></Image> </Grid> </Window>
MainWindow.xaml.cs:spa
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using CLRLibrary; using Color = System.Drawing.Color; namespace WpfApp1 { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } /// <summary> /// 將BitMap轉換成ImageSource /// </summary> /// <param name="bitmap"></param> /// <returns></returns> public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap) { //Bitmap bitmap = icon.ToBitmap(); IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); return wpfBitmap; } private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { var drawClass=new Class1(); List<byte> RGBData=drawClass.ErodeImage("D:/Background/1.jpg"); Bitmap map = new Bitmap(drawClass.Cols, drawClass.Rows); int temp = 0; for (int i = 0; i < drawClass.Rows; i++) { for (int j = 0; j < drawClass.Cols; j++) { var color = Color.FromArgb(RGBData[temp], RGBData[temp + 1], RGBData[temp + 2]); temp += 3; map.SetPixel(j, i, color); } } Box.Source = ChangeBitmapToImageSource(map); } } }
運行效果圖
項目全部代碼全在上面,若是有須要源碼的或者須要交流的能夠加我QQ或者微信。code
關於更多的C++/CLI代碼能夠參考我上面給的MSDN文檔連接,基本用獲得的語法都能從上面找到。orm