private void setVbx(int noChannels, reconPara reconPar) { vbxKSpace = new List <ViewBoxForm>(); vbxLowImage = new List <ViewBoxForm>(); for (int i = 0; i < imageShort.Length; i++) { vbxKSpace.Add(new ViewBoxForm()); vbxKSpace[i].Width = 512; vbxKSpace[i].Height = 512; vbxKSpace[i].Left = 5; vbxKSpace[i].Top = i * (512 + 5); //kSpace[i].readPixelData(); vbxLowImage.Add(new ViewBoxForm()); vbxLowImage[i].Width = 512; vbxLowImage[i].Height = 512; vbxLowImage[i].Left = 5 + 5 + 512; vbxLowImage[i].Top = i * (512 + 5); vbxLowImage[i].AccelModeScale = 3; //vbxLowImage[i].Dock = DockStyle.Fill; vbxLowImage[i].FovCol = 0d; vbxLowImage[i].FovRow = 0d; vbxLowImage[i].GrayLevelData = null; vbxLowImage[i].Lev = ((short)(0)); vbxLowImage[i].Name = "vbxImage" + i.ToString(); vbxLowImage[i].NoCol = 0; vbxLowImage[i].NoRow = 0; vbxLowImage[i].Pannable = false; vbxLowImage[i].PixelData = null; vbxLowImage[i].SampleRect = new System.Drawing.Rectangle(-161, -205, 322, 410); vbxLowImage[i].ShowPixelValue = false; vbxLowImage[i].ShowStatistics = false; vbxLowImage[i].Size = new System.Drawing.Size(512, 512); vbxLowImage[i].TabIndex = 0; vbxLowImage[i].PixelValueColor = Color.Blue; vbxLowImage[i].TabStop = false; vbxLowImage[i].Win = ((short)(1)); vbxLowImage[i].Zoomable = false; //vbxLowImage[i].SizeChanged += new System.EventHandler(this.vbxImage_SizeChanged); //vbxLowImage[i].VisibleChanged += new System.EventHandler(this.vbxImage_VisibleChanged); //vbxLowImage[i].Click += new System.EventHandler(this.vbxImage_Click); //vbxLowImage[i].Paint += new System.Windows.Forms.PaintEventHandler(this.vbxImage_Paint); //vbxLowImage[i].Resize += new System.EventHandler(vbxImage_Resize); //vbxLowImage[i].MouseUp += new MouseEventHandler(this.vbxImage_MouseUp); //vbxLowImage[i].ImageView.MouseDown += new MouseEventHandler(this.vbxImage_MouseDown); //vbxLowImage[i].OnWinLvlChangedByUI += OnWinLvlChanged; //vbxLowImage[i].OnPanPositionChangedByUI += OnPanPosChanged; //vbxLowImage[i].OnZoomFactorChangedByUI += OnZoomFactorChanged; vbxLowImage[i].NoRow = reconPar.noEchoes * reconPar.noScans; vbxLowImage[i].NoCol = reconPar.noSamples; vbxLowImage[i].ColSpacing = reconPar.colSpacing; vbxLowImage[i].RowSpacing = reconPar.rowSpacing; float rowScale = (float)vbxLowImage[i].Height / vbxLowImage[i].RowSpacing / (vbxLowImage[i].NoRow - 1); float colScale = (float)vbxLowImage[i].Width / vbxLowImage[i].ColSpacing / (vbxLowImage[i].NoCol - 1); vbxLowImage[i].ZoomFactor = (rowScale > colScale) ? rowScale : colScale; } }
private reconPara getReconPara(string fileName, reconParaFrom frm) { using (FileStream fs = new FileStream(fileName, FileMode.Open)) { reconPara rp = new reconPara(); rp.effectiveEcho = 1; rp.noSlices = 1; rp.noScans = 1; rp.noAverages = 1; rp.noEchoes = 1; rp.noSamples = 1; rp.colSpacing = 1; rp.rowSpacing = 1; rp.po = enumPhaseOrder.DownUpDownUp; if (frm == reconParaFrom.fromRawData) { //从原始数据提取重建参数 byte[] buf = new byte[64 * 1024]; fs.Read(buf, 0, buf.Length); string str = System.Text.Encoding.ASCII.GetString(buf); str = str.Remove(str.IndexOf("PARAMEND_V2")); string[] lines = str.Split('\n'); double fovPhase = 0; double fovRead = 0; for (int i = 2; i < lines.Length; i++) { if (lines[i].Contains("noScans")) { rp.noScans = (int)double.Parse(lines[i].Remove(0, lines[i].IndexOf(',') + 1).Trim()); } else if (lines[i].Contains("noEchoes")) { rp.noEchoes = (int)double.Parse(lines[i].Remove(0, lines[i].IndexOf(',') + 1).Trim()); } else if (lines[i].Contains("noSamples")) { rp.noSamples = (int)double.Parse(lines[i].Remove(0, lines[i].IndexOf(',') + 1).Trim()); } else if (lines[i].Contains("noAverages")) { rp.noAverages = (int)double.Parse(lines[i].Remove(0, lines[i].IndexOf(',') + 1).Trim()); } else if (lines[i].Contains("noSlices")) { rp.noSlices = (int)double.Parse(lines[i].Remove(0, lines[i].IndexOf(',') + 1).Trim()); } else if (lines[i].Contains("effectiveEcho")) { rp.effectiveEcho = (int)double.Parse(lines[i].Remove(0, lines[i].IndexOf(',') + 1).Trim()); } else if (lines[i].Contains("fovPhase")) { fovPhase = double.Parse(lines[i].Remove(0, lines[i].IndexOf(',') + 1).Trim()); } else if (lines[i].Contains("fovRead")) { fovRead = double.Parse(lines[i].Remove(0, lines[i].IndexOf(',') + 1).Trim()); } if (fovPhase != 0 && fovRead != 0) { rp.colSpacing = (float)fovRead / rp.noSamples; rp.rowSpacing = (float)fovPhase / rp.noScans / rp.noEchoes; } } return(rp); } } return(new reconPara()); }
private int[][][] openRawData(string[] fileName, reconPara para, int[] customPO, out int maxData, out int minData) { maxData = int.MinValue; minData = int.MaxValue; if (para.noEchoes > 1 && para.po == enumPhaseOrder.UserCustom) { if (customPO == null || customPO.Length != para.noEchoes * para.noScans) { MessageBox.Show("wrong custom phase order array length!"); return(null); } } int noChannels = fileName.Length; int[][][] tmpRawData = new int[noChannels][][]; for (int i = 0; i < noChannels; i++) { tmpRawData[i] = new Int32[para.noSlices][]; for (int j = 0; j < para.noSlices; j++) { tmpRawData[i][j] = new int[2 * para.noScans * para.noEchoes * para.noSamples]; } } for (int ch = 0; ch < fileName.Length; ch++) { using (FileStream fs = new FileStream(fileName[ch], FileMode.Open)) { int noCData = para.noSlices * para.noScans * para.noEchoes * para.noSamples; byte[] buf = new byte[2 * 4 * noCData]; fs.Seek(64 * 1024 + 8 + 256, SeekOrigin.Begin); int tmpLen = fs.Read(buf, 0, buf.Length); for (int scan = 0; scan < para.noScans; scan++) { for (int slice = 0; slice < para.noSlices; slice++) { for (int echo = 0; echo < para.noEchoes; echo++) { for (int sample = 0; sample < 2 * para.noSamples; sample++) { int posRawData = scan * para.noEchoes * 2 * para.noSamples + echo * 2 * para.noSamples + sample; //channelRawData[slice][posRawData] = swap4Bytes(buf, 4 * (posRawData + slice * 2 * para.noEchoes * para.noSamples)); //int posRawData = scan * para.noEchoes * 2 * para.noSamples*para.noSlices + echo * 2 * para.noSamples + sample; //channelRawData[slice][posRawData] = swap4Bytes(buf, 4 * (posRawData + slice * 2 * para.noEchoes * para.noSamples)); tmpRawData[ch][slice][posRawData] = swap4Bytes(buf, 4 * (sample + echo * 2 * para.noSamples + slice * 2 * para.noEchoes * para.noSamples + 2 * scan * para.noSlices * para.noEchoes * para.noSamples)); } } } } //多回波重排 if (para.noEchoes > 1) { int[] phaseOrder = new int[para.noEchoes * para.noScans]; if (para.po == enumPhaseOrder.UpDownUpDown) { for (int i = 0; i < para.noScans; i++) { for (int j = 0; j < para.noEchoes; j++) { phaseOrder[i * para.noEchoes + j] = -j * para.noScans - i + (para.effectiveEcho - 1) * para.noScans + para.noScans / 2 + (para.noScans * para.noEchoes) / 2; } } } else if (para.po == enumPhaseOrder.UpDownDownUp) { for (int i = 0; i < para.noScans; i++) { for (int j = 0; j < para.noEchoes; j++) { phaseOrder[i * para.noEchoes + j] = j * para.noScans - i + (para.effectiveEcho - 1) * para.noScans + para.noScans / 2 + (para.noScans * para.noEchoes) / 2; } } } else if (para.po == enumPhaseOrder.DownUpUpDown) { for (int i = 0; i < para.noScans; i++) { for (int j = 0; j < para.noEchoes; j++) { phaseOrder[i * para.noEchoes + j] = -j * para.noScans + i + (para.effectiveEcho - 1) * para.noScans - para.noScans / 2 + (para.noScans * para.noEchoes) / 2; } } } else if (para.po == enumPhaseOrder.DownUpDownUp) { for (int i = 0; i < para.noScans; i++) { for (int j = 0; j < para.noEchoes; j++) { phaseOrder[i * para.noEchoes + j] = j * para.noScans + i - (para.effectiveEcho - 1) * para.noScans - para.noScans / 2 + (para.noScans * para.noEchoes) / 2; } } } else if (para.po == enumPhaseOrder.UserCustom) { } else { MessageBox.Show("unrecongnized phse order!"); return(null); } for (int i = 0; i < phaseOrder.Length; i++) { if (phaseOrder[i] < 0) { phaseOrder[i] = phaseOrder[i] + para.noScans * para.noEchoes; } else if (phaseOrder[i] >= para.noScans * para.noEchoes) { phaseOrder[i] = phaseOrder[i] - para.noScans * para.noEchoes; } } //k空间重排 int[] tmpRawData1 = new int[2 * para.noScans * para.noEchoes * para.noSamples]; int tmpInt; for (int slice = 0; slice < para.noSlices; slice++) { for (int i = 0; i < tmpRawData1.Length; i++) { tmpInt = phaseOrder[i / (2 * para.noSamples)]; tmpRawData1[tmpInt * 2 * para.noSamples + i % (2 * para.noSamples)] = tmpRawData[ch][slice][i]; } tmpRawData[ch][slice] = tmpRawData1; } } } } //查找最大最小值 for (int i = 0; i < tmpRawData.Length; i++) { for (int j = 0; j < tmpRawData[0].Length; j++) { for (int k = 0; k < tmpRawData[0][0].Length; k++) { if (tmpRawData[i][j][k] > maxData) { maxData = tmpRawData[i][j][k]; } if (tmpRawData[i][j][k] < minData) { minData = tmpRawData[i][j][k]; } } } } return(tmpRawData); }
private void btnOpenImage_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = defaultRawDataPath; ofd.Multiselect = true; ofd.Filter = rawDataFilter; if (ofd.ShowDialog() == DialogResult.OK) { //clearVbx(); Settings.Default.defaultRawDataPath = Path.GetDirectoryName(ofd.FileName); Settings.Default.Save(); defaultRawDataPath = Settings.Default.defaultRawDataPath; //检查文件是否属于同一次扫描 rawDataFileName = ofd.FileNames; if (rawDataFileName.Length > 1) { string s = Regex.Replace(rawDataFileName[0], "_ch[0-9]*_", ""); for (int i = 1; i < rawDataFileName.Length; i++) { if (s != Regex.Replace(rawDataFileName[i], "_ch[0-9]*_", "")) { MessageBox.Show("not in same scan!"); return; } } } reconPar = getReconPara(ofd.FileNames[0], reconParaFrom.fromRawData); imagePar.noCol = reconPar.noSamples; imagePar.noRow = reconPar.noScans * reconPar.noEchoes; imagePar.colSpacing = reconPar.colSpacing; imagePar.rowSpacing = reconPar.rowSpacing; imagePar.imageFileName = Regex.Replace(rawDataFileName[0], "_ch[0-9]*_", "_").Replace(".raw", ""); //FileInfo[] fileInfoArray = new DirectoryInfo(Path.GetDirectoryName(ofd.FileNames[0])).GetFiles(ofd.SafeFileName.Replace("_ch1_","_ch*_")); acqChannels = ofd.FileNames.Length; //初始化k空间数据 if (rawData != null) { rawData = null; } rawData = new int[acqChannels][][]; for (int i = 0; i < acqChannels; i++) { rawData[i] = new int[reconPar.noSlices][]; for (int j = 0; j < reconPar.noSlices; j++) { rawData[i][j] = new int[2 * reconPar.noScans * reconPar.noEchoes * reconPar.noSamples]; } } imageAbsSOS = new short[reconPar.noSlices][]; for (int i = 0; i < reconPar.noSlices; i++) { imageAbsSOS[i] = new short[reconPar.noScans * reconPar.noEchoes * reconPar.noSamples]; } //初始化图像数据 int noCData = reconPar.noSlices * reconPar.noScans * reconPar.noEchoes * reconPar.noSamples; //for (int i = 0; i < acqChannels; i++) //{ //读取通道i的数据 rawData = openRawData(ofd.FileNames, reconPar, null, out rawDataMax, out rawDataMin); //MessageBox.Show("Max:" + RawDataMax.ToString() + ", Min:" + RawDataMin.ToString()); //getRawData(fileInfoArray[i].FullName, para); //} //for (int ch = 0; ch < rawData.Length; ch++) //{ // for (int slice = 0; slice < rawData[0].Length; slice++) // { // using (StreamWriter sw = new StreamWriter(@"D:\wuduan\Engineering\MRI_Spectrometer\EDA\Matlab\mri_data\20170802_anke\rawData_ch" + ch.ToString() + "_slice" + slice.ToString() + ".txt")) // { // for (int i = 0; i < rawData[0][0].Length; i++) // { // sw.WriteLine(rawData[ch][slice][i].ToString()); // } // } // } //} //重建图像 imageShort = imageRecon(rawData); //imageShort = new short[imageDbl.Length][][]; //for (int i = 0; i < imageDbl.Length; i++) //{ // imageShort[i] = new short[imageDbl[0].Length][]; // for (int j = 0; j < imageDbl[0].Length; j++) // { // imageShort[i][j] = new short[imageDbl[0][0].Length]; // } //} //for (int i = 0; i < imageDbl.Length; i++) //{ // for (int j = 0; j < imageDbl[0].Length; j++) // { // for (int k = 0; k < imageDbl[0][0].Length; k++) // { // imageShort[i][j][k] = (short)(imageDbl[i][j][k] * factor); // } // } //} //imageDbl = null; imageAbsSOS = ImageUtils.getSOS(imageShort); //显示图像 //for (int slice = firstImageDisplay; slice < firstImageDisplay+vbxImage.Count; slice++) //{ //if (slice < vbxImage.Count) //{ // imageShow(imageAbsSOS[slice], vbxImage[slice ], reconPar.noScans * reconPar.noEchoes, reconPar.noSamples); // //vbxImage[slice].Image.Save(@"g:\t\bmp_"+slice.ToString()+".bmp"); //} //imageShow(imageAbsSOS[slice], vbxImage[slice], reconPar.noScans * reconPar.noEchoes, reconPar.noSamples); imageShow(); //} } setImageIcon(reconPar.noSlices); }
public FormLowLevelImageViewer(int[][][] rawData, short[][][] imageShort, int focusedImage, reconPara par) { InitializeComponent(); this.rawData = rawData; this.imageShort = imageShort; this.focusedImage = focusedImage; noChannels = rawData.Length; setVbx(noChannels, par); //pnlLowLevelImage.Controls.AddRange(vbxKSpace.ToArray()); for (int i = 0; i < noChannels; i++) { vbxLowImage[i].readPixelData(ImageUtils.ImageFormatTranslate(imageShort[i][focusedImage], enumImgFormat.AbsLinear)); } pnlLowLevelImage.Controls.AddRange(vbxLowImage.ToArray()); if (rbtImageAbs.Checked) { } }