|
private void buttonGrayImg_Click(object sender, EventArgs e)
{
string path = textBoxPath.Text;
if (path == "")
{
MessageBox.Show("lease select an image at first.", "Information");
return;
}
string ext = path.Substring(path.Length - 3, 3);
ext = ext.ToLower();
ext = ext.Trim();
if (ext.CompareTo("bmp") != 0 && ext.CompareTo("jpg") != 0)
{
MessageBox.Show("You must select an .bmp or .jpg file at first.", "Information");
return;
}
IntPtr oldImg = CvInvoke.cvLoadImage(path, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR);
if (oldImg == eof)
{
MessageBox.Show("can not load the image:" + path, "Information");
return;
}
Rectangle cr = CvInvoke.cvGetImageROI(oldImg);
int width = cr.Width;
int height = cr.Height;
IntPtr grayImg = CvInvoke.cvCreateImage(cr.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);
if (grayImg == eof)
{
MessageBox.Show("can not create an image in memory.", "Information");
return;
}
CvInvoke.cvCvtColor(oldImg, grayImg, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY);
CvInvoke.cvNamedWindow("灰度图");
CvInvoke.cvShowImage("灰度图", grayImg);
CvInvoke.cvWaitKey(0);
CvInvoke.cvReleaseImage(ref oldImg);
CvInvoke.cvReleaseImage(ref grayImg);
CvInvoke.cvDestroyWindow("灰度图");
} |
|