废话少说,只上程序。
C++生成dll程序:
/*
实现功能:将一个图片中,所在行的像素值,按照一定的间隔(列数),将对应点的像素值提取出来。
*/
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;
using namespace cv;
struct Node
{
int R;
int G;
int B;
};
extern "C" _declspec(dllexport) bool operator_Image(char *path , int rownum,int intervalNum,struct Node *Result,int *Num)
{
Mat src = imread(path);
int rows,cols;
int x = 0,y = 0;
//cout << src.row(100) << endl;
rows = src.rows;
cols = src.cols;
if (src.empty())
{
cout << "图片不存在!" << endl;
}
if (rows < intervalNum)
{
cout << "你输入的间隔有误!!" << endl;
}
//创建动态数组,用来保存像素的值
//Result = new int[cols/intervalNum*3*sizeof(int)];
//int savedata[10000] = {0};
*Num = cols/intervalNum*3;
int i = 0;
while(x<cols)
{
y = rownum;
//Result[i++] = src.at<Vec3b>(y,x)[0];
//Result[i++] = src.at<Vec3b>(y,x)[1];
//Result[i++] = src.at<Vec3b>(y,x)[2];
Result[i].G = src.at<Vec3b>(y,x)[0];
Result[i].B = src.at<Vec3b>(y,x)[1];
Result[i].R = src.at<Vec3b>(y,x)[2];
/*if (x%100 == 0)
{
printf("%d ",src.at<Vec3b>(y,x)[0]);
printf("%d ",src.at<Vec3b>(y,x)[1]);
printf("%d ",src.at<Vec3b>(y,x)[2]);
}*/
x = x+intervalNum;
i++;
}
return true;
}
C#中调用他的程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public struct Node
{
int R;
int G;
int B;
};
namespace TestLSPVC
{
public partial class Form1 : Form
{
[DllImport("readcolpixelDll.dll", EntryPoint = "operator_Image", CharSet = CharSet.Ansi)]
public static extern bool operator_Image(string path , int rownum,int intervalNum, ref Node Result,ref int Num);
public Form1()<img alt="大笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif" /> {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = "c:\\lena.jpg";
Node[] x = new Node[1000];
int Count=0;
operator_Image(path,100,100,ref x[0],ref Count);
}
}
}
以上程序,经过测试,没有问题。
1、dll工程>>属性>>配置属性>>生成>>选择目录文件夹为要调用该dll文件的工程的bin文件夹
2、在要调用该dll文件的工程中添加引用该dll(dll文件在该工程的bin文件夹中)
3、在dll工程中设置断点
4、按F5即可进入调试