private Bitmap GrayPicture(string filename, int index, PictureOpticflowDataResultStruct[] PictureOpticflowDataResultArray) { Bitmap bmp = (Bitmap)Image.FromFile(filename); int PictureWidth = bmp.Width; int PictureHeight = bmp.Height; int strideValue; double max = 0, min = 255; float AverageOrigingray = 0; double SumCurentgray = 0; int lenth = PictureWidth * PictureHeight; if (lenth == PictureOpticflowDataResultArray[index].OpticflowResultFOfPixelArray.Length) { Apicturegrayinformation[] ApicturegrayinformationArray = new Apicturegrayinformation[lenth]; Rectangle rect = new Rectangle(0, 0, PictureWidth, PictureHeight); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); IntPtr ptr = bmpData.Scan0; if (grayed == true) // 灰度化显示 { if (PictureWidth % 4 == 0) { strideValue = PictureWidth * 3; byte[] rgbValues = new byte[lenth * 3]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, lenth * 3); //灰度化 Dogray(ref ApicturegrayinformationArray, rgbValues, strideValue, PictureWidth, PictureHeight, false, ref max, ref min, ref AverageOrigingray); //灰度拉伸 graystretch(ref ApicturegrayinformationArray, max, min, ref SumCurentgray); } else { strideValue = bmpData.Stride; int bytes = strideValue * PictureHeight; byte[] rgbValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); //灰度化 Dogray(ref ApicturegrayinformationArray, rgbValues, strideValue, PictureWidth, PictureHeight, false, ref max, ref min, ref AverageOrigingray); //灰度拉伸 graystretch(ref ApicturegrayinformationArray, max, min, ref SumCurentgray); //光滑去噪 //showgussSmooth(ref ApicturegrayinformationArray); } bmp.UnlockBits(bmpData); bmp.Dispose(); GC.Collect(); curBitmap = CreateBmpByGrays(ApicturegrayinformationArray, index, PictureWidth, PictureHeight, PictureOpticflowDataResultArray); return(curBitmap); } // 灰度化显示 else // 彩色显示 { float tmp = 0; if (PictureWidth % 4 == 0) { strideValue = PictureWidth * 3; byte[] rgbValues = new byte[lenth * 3]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, lenth * 3); for (int i = 0; i < lenth; i++) { for (int j = 0; j < 3; j++) { if (biaodingDsp == true) { tmp = rgbValues[i * 3 + j]; } else { tmp = 0; } if (graydeltaAdd == true) // 灰度叠加 { if (forwardAdd == true) // 正向叠加 { tmp += PictureOpticflowDataResultArray[index].OpticflowResultFOfPixelArray[i].Greyvalue * FactorOfGreygrad; } else { tmp -= PictureOpticflowDataResultArray[index].OpticflowResultFOfPixelArray[i].Greyvalue * FactorOfGreygrad; } } if (tmp > 255) { rgbValues[i * 3 + j] = 255; } if (tmp < 0) { rgbValues[i * 3 + j] = 0; } if ((tmp >= 0) & (tmp <= 255)) { rgbValues[i * 3 + j] = (byte)tmp; } } } System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, lenth * 3); } else { strideValue = bmpData.Stride; int bytes = strideValue * PictureHeight; byte[] rgbValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); for (int i = 0; i < rgbValues.Length; i++) { if (biaodingDsp == true) { tmp = rgbValues[i]; } else { tmp = 0; } if (graydeltaAdd == true) // 灰度叠加 { if (forwardAdd == true) // 正向叠加 { tmp += PictureOpticflowDataResultArray[index].OpticflowResultFOfPixelArray[i].Greyvalue * FactorOfGreygrad; } else { tmp -= PictureOpticflowDataResultArray[index].OpticflowResultFOfPixelArray[i].Greyvalue * FactorOfGreygrad; } } if (tmp > 255) { rgbValues[i] = 255; } if (tmp < 0) { rgbValues[i] = 0; } if ((tmp >= 0) & (tmp <= 255)) { rgbValues[i] = (byte)tmp; } } System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); } bmp.UnlockBits(bmpData); return(bmp); } } else { MessageBox.Show("计算灰度差的照片大小与当前照片大小不一致!!!"); return(bmp); } }
private void testToolStripMenuItem_Click(object sender, EventArgs e) { //读入Excel数据 #region 测试读入Excel数据 //OpenFileDialog openFileDialog1 = new OpenFileDialog(); //DataTable ExcelTable; //openFileDialog1.ShowDialog(); //string MyFileName = openFileDialog1.FileName; //if (MyFileName.Trim() == "") //{ // return; //} //string MyFilePath = Path.GetFullPath(MyFileName); //string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + MyFilePath + ";Extended Properties=Excel 8.0;"; //OleDbConnection myConn = new OleDbConnection(strCon); //string strCom = " SELECT * FROM [Sheet1$] "; //myConn.Open(); //OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); //DataSet myDataSet = new DataSet(); //myCommand.Fill(myDataSet, "[Sheet1$]"); //myConn.Close(); //ExcelTable = myDataSet.Tables[0]; //int iRows = ExcelTable.Rows.Count; //int iColums = ExcelTable.Columns.Count; ////string[,] StoreData = new string[iRows, iColums]; //string[] StoreData = new string[iRows * iColums]; //float[] Data = new float[iRows * iColums]; //int k = 0; //for (int i = 0; i < ExcelTable.Rows.Count; i++) // for (int j = 0; j < ExcelTable.Columns.Count; j++) // { // //将Excel表中的数据存储到数组 // StoreData[k] = ExcelTable.Rows[i][j].ToString(); // k++; // } //for (int i = 0; i < StoreData.Length;i++ ) //{ // //Data[i] =(float) StoreData[i]; //} #endregion Bitmap ReferencePictureBitmap; // 图像对象 Bitmap CurPictureBitmap; // 图像对象 CreatArray CreatArray = new CreatArray(); //创建数组对象 //得到bitmap ReferencePictureBitmap = (Bitmap)Image.FromFile(currentOMC.readFileNames[0]);//得到母板点的Bitmap CurPictureBitmap = (Bitmap)Image.FromFile(currentOMC.readFileNames[1]); Apicturegrayinformation[] Im1 = new Apicturegrayinformation[ReferencePictureBitmap.Height * ReferencePictureBitmap.Width]; Apicturegrayinformation[] Im2 = new Apicturegrayinformation[CurPictureBitmap.Width * CurPictureBitmap.Height]; Apicturegrayinformation[] Im2correction = new Apicturegrayinformation[CurPictureBitmap.Width * CurPictureBitmap.Height]; // 装载灰度数组 //CreatArray.creatPictureGreyArray(ReferencePictureBitmap, ref Im1); //CreatArray.creatPictureGreyArray(CurPictureBitmap, ref Im2); functionFun.LoadgrayValuesArrayForMatch(ReferencePictureBitmap, ref Im1); //装载母板灰度数组 functionFun.LoadgrayValuesArrayForMatch(CurPictureBitmap, ref Im2); //装载后一帧图片灰度数组 float[] unew = new float[Im1.Length]; float[] vnew = new float[Im1.Length]; // 光强修正 functionFun.correction_illumination(CurPictureBitmap, Im1, Im2, ref Im2correction); //预处理 functionFun.horn_schunk_estimator(CurPictureBitmap, Im1, Im2, ref unew, ref vnew); }