我知道这个问题之前在这里和这里被问过,不幸的是,提供的答案无法解决我的问题。我在 C# 应用程序中使用 Stasm ( http://www.milbo.users.sonic.net/stasm/index.html ) 库。下面是我调用“AsmSearchDll”函数的代码。
[DllImport(@"stasm\stasm_dll.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern void AsmSearchDll
(
[Out] out Int32 pnlandmarks,
[Out] out Int32[] landmarks,
[In, MarshalAs(UnmanagedType.LPStr)] String image_name,
[In, MarshalAs(UnmanagedType.LPStr)] String image_data,
[In] Int32 width,
[In] Int32 height,
[In] Int32 is_color,
[In, MarshalAs(UnmanagedType.LPStr)] String conf_file0,
[In, MarshalAs(UnmanagedType.LPStr)] String conf_file1
);
public void SearchFacialFeatures()
{
string image_name = "image-5.jpg"; // imagePath;
var image = new Image<Bgr, byte>(image_name).Convert<Gray, byte>();
int pnlandmarks = 0;
var landmarks = new int[500];
var imageData = Marshal.PtrToStringAnsi(image.MIplImage.imageData);
int imgWidth = image.Width;
int imgHeight = image.Height;
int is_color = 1;
string confile_file0 = Path.GetFullPath(@"data\mu-68-1d.conf");
string config_file1 = Path.GetFullPath(@"data\mu-76-2d.conf");
string sDataDir = @"\stasm\data";
AsmSearchDll(out pnlandmarks, out landmarks, image_name, imageData, imgWidth, imgHeight, 1, null, null);
MessageBox.Show(image_name);
}
问题是应用程序在到达这条线时停止
AsmSearchDll(out pnlandmarks, out landmarks, image_name, imageData, imgWidth, imgHeight, 0, null, null);
最初,每当调用 AsmSearchDll 函数时,应用程序就会退出,经过多次处理代码后,该函数停止了。现在应用程序出现了,但从未处理 AsmSearchDll 函数。我可以说出来,因为我用 VS 浏览了代码。永远无法到达函数下方的消息框。
我有一种强烈的感觉,该函数正在引发内部错误。对我来说不幸的是,这是我与 Interop/DllImport 的第一次交易。
我的问题是,我做错了什么,我该如何解决这个问题?我已经坚持了一天。
编辑:为非托管函数添加了代码
非托管函数的签名
void AsmSearchDll (
int *pnlandmarks, // out: number of landmarks, 0 if can't get landmarks
int landmarks[], // out: the landmarks, caller must allocate
const char image_name[], // in: used in internal error messages, if necessary
const char image_data[], // in: image data, 3 bytes per pixel if is_color
const int width, // in: the width of the image
const int height, // in: the height of the image
const int is_color, // in: 1 if RGB image, 0 if grayscale
const char conf_file0[], // in: 1st config filename, NULL for default
const char conf_file1[]) // in: 2nd config filename, NULL for default, "" if none
调用函数的 C++ 示例
const char *image_name = "../data/test-image.jpg";
IplImage *img = cvLoadImage(image_name, CV_LOAD_IMAGE_COLOR);
if(img == NULL) {
printf("Error: Cannot open %s\n", image_name);
return -1;
}
// sanity checks (AsmSearchDll assumes imageData is vector of b,r,g bytes)
if(img->nChannels != 3 || img->depth != IPL_DEPTH_8U ||
img->origin != 0 || img->widthStep != 3 * img->width) {
printf("Error: %s is an unrecognized image type\n", image_name);
return -1;
}
// locate the facial landmarks with stasm
int nlandmarks;
int landmarks[500]; // space for x,y coords of up to 250 landmarks
AsmSearchDll(&nlandmarks, landmarks,
image_name, img->imageData, img->width, img->height,
1 /* is_color */, NULL /* conf_file0 */, NULL /* conf_file1 */);
谢谢你的帮助。