这是点击按钮的响应函数
void CCrazyDlg::OnBnClickedOk()
{
CString FilePath;
CFileDialog FileDlg(TRUE);
if (IDOK == FileDlg.DoModal())
{
//获取FileOpen对话框返回的路径名
FilePath = FileDlg.GetPathName();
//GetPathName返回的是CString类型,要经过转换为string类型才能使用imread打开图片
//std::string pathName(FilePath.GetBuffer());
std::string tempName = (LPCSTR)CStringA(FilePath);
const char*pathName = tempName.c_str();
Mat bin;
//读取图片
Mat im = imread(pathName); // 读图,并转化为灰度图
threshold(im, bin, 120, 255, CV_THRESH_BINARY | THRESH_OTSU);
//显示图片
ShowMat(bin, GetDlgItem(IDC_SHOW)->GetSafeHwnd());
}
}
这是ShowMat 函数
int CCrazyDlg::ShowMat(Mat img, HWND hWndDisplay)
{
if (img.channels()
{
return -1;
}
//构造将要显示的Mat版本图片
RECT rect;
::GetClientRect(hWndDisplay, &rect);
Mat imgShow(abs(rect.top - rect.bottom), abs(rect.right - rect.left), CV_8UC3);
resize(img, imgShow, imgShow.size());
//在控件上显示要用到的CImage类图片
ATL::CImage CI;
int w = imgShow.cols;//宽
int h = imgShow.rows;//高
int channels = imgShow.channels();//通道数
CI.Create(w, h, 8 * channels); //CI像素的复制
uchar *pS; uchar *pImg = (uchar *)CI.GetBits();//得到CImage数据区地址
int step = CI.GetPitch();
for (int i = 0; i
{
pS = imgShow.ptr(i);
for (int j = 0; j<w; j++)
{
for (int k = 0; k<3; k++)
*(pImg + i*step + j * 3 + k) = pS[j * 3 + k]; //注意到这里的step不用乘以3
}
}
//在控件显示图片
HDC dc;
dc = ::GetDC(hWndDisplay);
CI.Draw(dc, 0, 0);
::ReleaseDC(hWndDisplay, dc);
CI.Destroy();
return 0;
}
我调试到threshold()那里就停止运行,错误信息是0x7734C42D 处(位于 Crazy.exe 中)有未经处理的异常: Microsoft C++ 异常: cv::Exception,位于内存位置 0x0039DFB4 处。求解释,我困扰了好久了