求大佬帮看看是什么问题
编写了一个Image类,在主函数中构建对象,利用对象加载图片并显示,运行时出错:OPencv GUI error Handler:incorrect size of input array(non-postion cols or rows)#ifndef Image_H
#define Image_H
class Image
{
public:
void Alloc(int h,int w);
Image(); //构造函数,创建行列都为零的Image对象
Image(int h, int w); //构造函数重载,创建h行,w列的Image对象
Image(int h, int w, unsigned char val); //构造函数重载
Image(char* ImageName); //构造函数重载,利用文件名从硬盘加载图像文件成为Image对象;
Image(unsigned char m[][100], int n); //构造函数重载,从静态数组创建Image对象;
Image(unsigned char **m, int h, int w); //构造函数重载,从动态数组创建Image对象;
Image(const Image &im); //拷贝构造函数;
~Image();
void Read(char* ImageName); //从硬盘文件中读入图像数据;
void Write(char* filename); //将图像数据保存为图像文件;
void Show(char* winname); //显示图像;
protected:
int height;
int width;
unsigned char **data;
};
#endif
#include"Image.h"
#include "cv.h"
#include"highgui.h"
void Image::Alloc(int h,int w)
{data=new unsigned char *[h];
for(int i=0;i<h;i++)
data[i]=new unsigned char [w];
}
Image::Image()
{
width=0;
height=0;
}
Image::Image(int h,int w)
{
int height=h;
int width=w;
Alloc(h,w);
for(int i=0;i<h;i++){
for(int j=0;j<w;j++)
data[i][j]=0;
}
}
Image::Image(int h,int w,unsigned char val)
{Alloc(h,w);
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
data[i][j]=val;
}
}
Image::Image(char* ImageName)
{
IplImage* img = cvLoadImage(ImageName, CV_LOAD_IMAGE_GRAYSCALE);
unsigned char *img_data = (unsigned char *)(img->imageData);
int widthstep = img->widthStep;
int height=img->height;
int width=img->width;
for(int i=0;i<height;i++){
for(int j=0;j<width;j++)
data[i][j]=img_data[i*widthstep+j];
}
}
Image::Image(unsigned char m[][100], int rows)
{
data=new unsigned char *[height];
for(int i=0;i<rows;i++)
{
data[i]=new unsigned char [width];
for(int j=0;j<width;j++)
data[i][j]=m[i][j];
}
}
Image::Image(unsigned char **m, int h, int w)
{
data=new unsigned char *[height];
for(int i=0;i<height;i++)
{
data[i]=new unsigned char [width];
for(int j=0;j<width;j++)
data[i][j]=m[i][j];
}
}
Image::Image(const Image &im)
{
height=im.height;
width=im.width;
data=im.data;
data=new unsigned char *[height];
for(int i=0;i<height;i++)
{
data[i]=new unsigned char [width];
for(int j=0;j<width;j++)
data[i][j]=im.data[i][j];
}
}
Image::~Image()
{
for(int i=0;i<height;i++)
{
delete [] data[i];
}
delete [] data;
}
void Image::Read(char *ImageName)
{
IplImage * img = cvLoadImage(ImageName, CV_LOAD_IMAGE_GRAYSCALE);
unsigned char *img_data = (unsigned char *)(img->imageData);
int widthstep = img->widthStep;
int height=img->height;
int width=img->width;
Alloc(height,width);
for(int i=0;i<height;i++){
for(int j=0;j<width;j++)
data[i][j]=img_data[i*widthstep+j];
}
cvReleaseImage(&img);
}
void Image::Write(char *filename)
{
IplImage* img = cvCreateImage(cvSize(height, width), IPL_DEPTH_8U, 1);
unsigned char *img_data = (unsigned char *)(img->imageData);
int widthstep = img->widthStep;
int height=img->height;
int width=img->width;
for( int i=0;i<height;i++)
{for(int j=0;j<width;j++)
{
img_data[i*widthstep+j]=data[i][j];
}
}
cvSaveImage(filename, img);
cvReleaseImage(&img);
}
void Image::Show(char *winname)
{
IplImage *img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
unsigned char *img_data = (unsigned char *)(img->imageData);
int widthstep = img->widthStep;
for( int i=0;i<height;i++)
{for(int j=0;j<width;j++)
{
img_data[i*widthstep+j]=data[i][j];
}
}
cvNamedWindow(winname, CV_WINDOW_AUTOSIZE);
cvShowImage(winname, img);
cvWaitKey(0);
cvReleaseImage(&img);
}
主函数
#include<iostream>
#include"Image.h"
#include"cv.h"
#include"highgui.h"
int main(int argc, char *argv[])
{
Image img1;
img1.Read("scene2_bg.jpg");
img1.Show("Image img1");
return 0;
}