MATLAB调用c++函数时的类型匹配问题错误

Liu Zhian 2017-11-15 12:48:29
我的目的是根据一幅图,用MATLAB获取灰度图,然后根据灰度图求Ostu算法的门阈值,然后再根据这个门阈值获取到二值图。
以下为function_Ostu.cpp文件,也就是MATLAB调用的c++函数。


#include "mex.h"
#include <iostream>
using namespace std;
double getThresh(int ** a, long row, long col)//Otsu法(最大类间方差法)
{
long N = row*col;
double global_gram[256] = { 0.0 };
double fangcha[256] = { 0.0 };

for (long i = 0; i < row; i++)
{
for (long j = 0; j < col; j++)
{
global_gram[a[i][j]]++; //统计灰度直方图(此时为统计像素数量)
}
}
int w0, w_back, w_front; // w0为全局平均灰度,背景灰度,前景灰度
double p_back, p_front; // 背景概率,前景概率
double data_back, data_front;
for (int i = 0; i < 256; i++) //全局平均灰度
{
global_gram[i] /= N;
w0 += i*global_gram[i];
}
for (int i = 0; i < 256; i++) // 对每个灰度值,计算类间方差
{
p_back += global_gram[i];
p_front = 1 - p_back;
data_back += global_gram[i] * i;
data_front = w0 - data_back;
w_back = data_back / p_back;
w_front = data_front / p_front;
fangcha[i] = p_back*(w0 - w_back)*(w0 - w_back) + p_front*(w0 - w_front)*(w0 - w_front);
}
double temp = 0;
double result = 0;
for (int i = 0; i < 256; i++)
{
if (fangcha[i]>temp)
{
temp = fangcha[i];
result = i;
}
}
return result;
}

void mexFunction(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
if (nlhs != 1 || nrhs != 3) // 判断参数数量是否正确
{
cout << "参数个数不匹配!\n";
exit(0);
}
int **a;
a = (int **)(mxGetPr(prhs[0]));
long r = *(mxGetPr(prhs[1]));
long c = *(mxGetPr(prhs[2]));
double *result;
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
*result = getThresh(a, r, c);
}


MATLAB运行时报错如下:
>> rgb=imread('图C.jpg');
>> gray=rgb2gray(rgb);
>> [row,col]=size(gray);
>> thresh=getThresh(gray,row,col);
未定义与 'uint8' 类型的输入参数相对应的函数 'getThresh'

求指点
...全文
347 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr_Zhouzl 2018-05-11
  • 打赏
  • 举报
回复
thresh初始定义为double类型矩阵试一下

3,424

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧