源代码如下:跪求帮助!!!
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
int ImageIn[500][500];
int Hui[255]; //图像灰度级数
int Tol,Ave,Std;
int Vt1,Vt2;
int i,j;
int Min,Max;
Bitmap^box1=gcnew Bitmap(pictureBox1->Image);
Color^c=gcnew Color();
int dwWidth=this->pictureBox1->Image->Width;
int dwHeight=this->pictureBox1->Image->Height;
int R,G,B;
for(i=0;i<dwWidth-1;i++)
{
for(j=0;j<dwHeight-1;j++)
{
c=box1->GetPixel(j,i);
R=c->R;
G=c->G;
B=c->B;
ImageIn[i][j]=(int)((R+G+B)/3);
}
}
for(i=0;i<dwWidth-1;i++)
{
for(j=0;j<dwHeight-1;j++)
{
Vt1=Vt1+ImageIn[i][j];
Vt2=Vt2+Math::Pow(ImageIn[i][j],2);
Hui[ImageIn[i][j]]=Hui[ImageIn[i][j]]+1;
if(ImageIn[i][j]<Min)
{Min=ImageIn[i][j];} //最小值
if(ImageIn[i][j]>Max)
{Max=ImageIn[i][j];} //最大值
}
}
Tol=dwWidth*dwHeight;
Ave=Vt1/Tol;
Std=Math::Sqrt(Vt2/Tol-Ave*Ave);
label1->Text="图像的均值是:"+Max.ToString();
}
------解决方案--------------------------------------------------------
循环控制变量dwWidth、dwHeight不应该用pictureBox1->Image的尺寸来赋值,而应该用box1的尺寸来赋值,即:
int dwWidth = box1->Width; int dwHeight = box1->Height;
因为pictureBox1->Image的尺寸跟box1的尺寸并非完全相等的,虽然box1是由pictureBox1->Image做参数构造而成(你可以自己试一下)