C言語からMATLA​Bdllを呼び出して​、Cからdllへ画像​を受け渡して、dll​から結果画像を受け取​る際に変数はどのよう​に渡されているのでし​ょうか。

12 views (last 30 days)
悠貴 大森
悠貴 大森 on 8 Feb 2022
Commented: Kojiro Saito on 24 Feb 2022
MATLABDLLを呼び出すCファイルの一部です。BMP画像を読み込み、DLLに受け渡し、しきい値処理したものをCファイルのoutに格納したいです。
mbuild 〇〇.c ××.lib -R2018aでコンパイル後、実行すると太線部mlfDllexampleでAccess violationとなります。よろしくお願いいたします。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int run_main(int argc, const char **argv)
{
mxArray *image;
mxArray *out;
unsigned char *inputimage;
unsigned char BitMapFileHeader[14];//BMPのファイルヘッダーを保存する
unsigned int biSize;//BMPのサイズを保存する
int biWidth;//BMPの幅を保存する
int biHeight;//BMPの高さを保存する
unsigned char BitMapInfoHeader[28];//上記3つ以外のBMPの情報ヘッダーを保存する
int i,j,c;//for文用
FILE *fp;//ファイルポインタ
int dims[3];
fp = fopen("~//example.bmp","rb");
fread(&BitMapFileHeader, sizeof(unsigned char),14,fp);//ファイルヘッダーを読み込む
fread(&biSize, sizeof(int),1,fp);//情報ヘッダーにあるサイズを読み込む
fread(&biWidth,sizeof(int),1,fp);//情報ヘッダーにある幅を保存
fread(&biHeight,sizeof(int),1,fp);//情報ヘッダーにある高さを保存
fread(&BitMapInfoHeader,sizeof(unsigned char),28,fp);//残りの情報ヘッダーを保存
dims[0] = biHeight;
dims[1] = biWidth;
dims[2] = 3;
inputimage = (unsigned char*)mxMalloc(sizeof(const char*)*biHeight*biWidth*3);
for(i = 0; i<biHeight;i++){//0から幅まで
for(j = 0;j<biWidth; j++){//0から高さまで
for(c=0;c<3;c++){//RGBのそれぞれ
fread((inputimage + (biHeight - i) + biHeight * j + biHeight * biWidth * (2 - c)), sizeof(unsigned char), 1, fp);//画素の情報を読み込んで保存する
}
}
}
fclose(fp);
image = mxCreateNumericArray(3, (const mwSize*)dims, mxUINT8_CLASS,0);//file name
mxSetUint8s(image, inputimage);
out = mxCreateLogicalMatrix(biHeight, biWidth);
if (!libdetection0208Initialize()){
fprintf(stderr,"Could not initialize the library.\n");
return -2;
}
else
{
mlfDllexample(1, &out, image);//imageで画像を渡し、outで画像を返す。
libdetection0208Terminate();
mclTerminateApplication();
return 0;
}
int main(int argc, const char ** argv)
{
/* Call the mclInitializeApplication routine. Make sure that the application
* was initialized properly by checking the return status. This initialization
* has to be done before calling any MATLAB APIs or MATLAB Compiler SDK
* generated shared library functions. */
printf("nim\n");
if( !mclInitializeApplication(NULL,0) )
{
fprintf(stderr, "Could not initialize the application.\n");
return -1;
}
return mclRunMain((mclMainFcnType)run_main, argc, argv);
}
//mlfDllexampleの中身
function out = dllexample(image)
Te = 180;
out = image(:,:)>Te;
end

Accepted Answer

Kojiro Saito
Kojiro Saito on 9 Feb 2022
dllexample関数でimage配列にアクセスする際にaccess violationが発生しています。
image = mxCreateNumericArray(3, (const mwSize*)dims, mxUINT8_CLASS,0);
の部分のmxCreateNumericArrayの2番目の引数dimsが原因のようです。
冒頭で
//int dims[3];
int *dims[3];
と定義すればエラーが解消します。
  10 Comments
悠貴 大森
悠貴 大森 on 24 Feb 2022
いただいたslnファイルを参考にしたところBMP画像の幅の処理に原因があることがわかり、自分の環境でも実行を確認できました。
ご丁寧な対応ありがとうございました。
Kojiro Saito
Kojiro Saito on 24 Feb 2022
解決して良かったです。

Sign in to comment.

More Answers (0)

Categories

Find more on C 共有ライブラリの統合 in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!