**我的开发环境:**windows下opencv3.2,QT4.8.6。
遇到的问题是:smooth = cvCreateImage(cvGetSize(iplImg),IPL_DEPTH_8U,1);
cvSmooth(iplImg,smooth,CV_GAUSSIAN,3,0,0);
**所报错误为:**OpenCV Error: Assertion failed (dst.size() == src.size() && (smooth_type == CV_BLUR_NO_SCALE || dst.type() == src.type()))。程序执行后卡死退出,但是同样的代码在Linux中却可以执行。
应该不是申请内存那块。同样的方式做“灰度化”就没有问题。初步怀疑是编译器问题。同时也请看到这篇文章的大牛们回复具体讨论。
为了达到Gauss滤波的目的先用GaussianBlur函数代替cvSmooth平滑滤波,没有问题。
———-
opencv中的转换挺重要,2.X和3.X有的会有些差别,而且方法不局限一种。因为我的环境是QT,在这里对QImage Mat IplImage转换记录下:
QImage—>IplImage:
QImage* img;
IplImage *iplImg;
int nChannel = 0;
nChannel = img->format();
qDebug()<<"img format is:"<<img->format();
if( nChannel==0 )
{
return;
}
else
{
iplImg=cvCreateImageHeader(cvSize(img->width(),img->height()),8,nChannel);
iplImg->imageData=(char*)img->bits();
if(nChannel==3)
{
cvConvertImage(iplImg,iplImg,CV_CVTIMG_SWAP_RB);
}
IplImage—>QImage: 摘自
QImage* CjwIplToQImg(const IplImage *cvImg)
{
if(!cvImg)
return NULL;
IplImage* temp=NULL;
temp=cvCloneImage(cvImg);
cvCvtColor(cvImg,temp,CV_BGR2RGB);
uchar* imgData = (uchar*)(temp->imageData);
QImage *qImg=new QImage(imgData,temp->width,temp->height,QImage::Format_RGB888);
return qImg;
cvReleaseImage(&temp);
delete imgData;
}
IplImage—>Mat:
IplImage iplImg;
Mat matimg;
matimg = cvarrToMat(iplImg);
Mat—>IplImage:
iplImg= IplImage(matimg);
Mat—>QImage:摘自
QImage cvMat2QImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if(mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setNumColors(256);
for(int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat.data;
for(int row = 0; row < mat.rows; row ++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if(mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
else if(mat.type() == CV_8UC4)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
return QImage();
}
}
QImage—>Mat:摘自
Mat QImage2cvMat(QImage image)
{
cv::Mat mat;
switch(image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.bits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.bits(), image.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.bits(), image.bytesPerLine());
break;
}
return mat;
}