用途

通过使用C++ + Opencv 编写算法,然后用WPF(C#)编写程序界面,实现交互

可以参考MSDN文档:https://docs.microsoft.com/en-us/cpp/windows/pin-ptr-cpp-cli?view=vs-2017

项目结构

代码

CLR部分:

CLRLibrary.h文件

#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:

<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:

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或者微信。

关于更多的C++/CLI代码可以参考我上面给的MSDN文档链接,基本用得到的语法都能从上面找到。

最新文章

  1. Select查询执行顺序
  2. React学习——ListView(Reflux)
  3. python命令行解释器 argparse
  4. 解决ckeditor中文路径无法下载,无法显示图片问题
  5. Python 3.x print 小结
  6. 在config文件输入特殊字符
  7. python学习之“切片操作从入门到精通”
  8. Poj 2262 / OpenJudge 2262 Goldbach&#39;s Conjecture
  9. SQL Server Management Studio 使用作业实现数据库备份
  10. [LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.
  11. bzoj1676[Usaco2005 Feb]Feed Accounting 饲料计算
  12. Git 日常操作
  13. MDK 中 [WEAK] 的作用
  14. H5 13-子元素选择器
  15. [转帖] bat方式遍历目录内的文件
  16. Basic berkeley socket functions
  17. vue中滚动事件绑定的函数无法调用问题
  18. Android 实时视频编码—H.264硬编码
  19. linux brctl command not found
  20. django 动态生成CSV文件

热门文章

  1. Vue3 SFC 和 TSX 方式调用子组件中的函数
  2. java连接数据库加载驱动到java项目
  3. C语言编译环境中的 调试功能及常见错误提示
  4. 17_Vue列表过滤_js模糊查询
  5. 一个超经典 WinForm 卡死问题的再反思
  6. CodeQL(1)
  7. python面试题常用语句
  8. 启动homestead虚拟机 vagrant up执行后,提示Timed out while waiting for the machine to boot
  9. Chrome 103支持使用本地字体,纯前端导出PDF优化
  10. go slice不同初始化方式性能&amp;数组比较