public void lineFilter(ImagePGM image, ImagePGM imageCopy, int line, float[,] filter) { for (int i = 1; i < image.Width - 1; i++) { image.Values[line, i] = (int)pixelFilter(imageCopy, i, line, filter); } }
public void centerFilter(ImagePGM image, ImagePGM imageCopy, float[,] filter) { for (int i = 1; i < image.Height - 2; i++) { lineFilter(image, imageCopy, i, filter); } }
private int[,] getPixelNeighborhood(ImagePGM image, int y, int x) { return(new int[, ] { { image.Values[y - 1, x - 1], image.Values[y - 1, x], image.Values[y - 1, x + 1], }, { image.Values[y, x - 1], image.Values[y, x], image.Values[y, x + 1] }, { image.Values[y + 1, x - 1], image.Values[y + 1, x], image.Values[y - 1, x + 1] } }); }
public void filterImage(ImagePGM image, ImagePGM imageCopy, float[,] filter) { var watch = System.Diagnostics.Stopwatch.StartNew(); centerFilter(image, imageCopy, filter); sidesFilter(image, filter); cornersFilter(image, filter); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Console.WriteLine("czas normalnie: " + elapsedMs + "ms"); }
private float bottomPixelFilter(ImagePGM image, int x, int y, float[,] filter) { float temp = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { temp += (image.Values[y - i, x + j - 1] * filter[i, j]); } } return(temp); }
private float upperPixelFilter(ImagePGM image, int x, int y, float[,] filter) { float temp = 0; for (int i = 0; i < 3; i++) { for (int j = 1; j < 3; j++) { temp += (image.Values[x + i - 1, y + j - 1] * filter[i, j]); } } return(temp); }
public void sidesFilter(ImagePGM image, float[,] filter) { for (int i = 1; i < image.Width - 1; i++) { image.Values[0, i] = (int)upperPixelFilter(image, i, 0, filter); image.Values[image.Height - 1, i] = (int)bottomPixelFilter(image, i, image.Height - 1, filter); } for (int i = 1; i < image.Height - 1; i++) { image.Values[i, 0] = (int)leftSidePixelFilter(image, i, 0, filter); image.Values[i, image.Width - 1] = (int)rightSidePixelFilter(image, i, image.Width - 1, filter); } }
private float pixelFilter(ImagePGM image, int x, int y, float[,] filter) { float temp = 0; int[,] pixelNeighborgood = getPixelNeighborhood(image, y, x); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { temp += (pixelNeighborgood[i, j] * filter[i, j]); } } return(temp); }
public async Task asyncCenterFilter(ImagePGM image, ImagePGM imageCopy, float[,] filter) { Task.Run(() => { for (int i = 1; i < (image.Height - 2) / 2; i++) { lineFilter(image, imageCopy, i, filter); } }); Task.Run(() => { for (int i = (image.Height - 2) / 2; i < image.Height - 2; i++) { lineFilter(image, imageCopy, i, filter); } }); }
public ImageReader(ImagePGM imagePGM) { FileStream fs = new FileStream(imagePGM.Path, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); switch (NextNonCommentLine(br)) { case "P1": imagePGM.Format = ImageFormat.p1; break; case "P2": imagePGM.Format = ImageFormat.p2; break; case "P3": imagePGM.Format = ImageFormat.p3; break; case "P4": imagePGM.Format = ImageFormat.p4; break; case "P5": imagePGM.Format = ImageFormat.p5; break; default: imagePGM.Format = ImageFormat.X; break; } string temp = NextNonCommentLine(br); string[] temp2 = temp.Split(' '); imagePGM.Width = Int32.Parse(temp.Split(' ')[0]); imagePGM.Height = Int32.Parse(temp.Split(' ')[1]); imagePGM.Depth = Int32.Parse(NextNonCommentLine(br)); imagePGM.Values = new int[imagePGM.Height, imagePGM.Width]; for (int i = 0; i < imagePGM.Height; i++) { for (int j = 0; j < imagePGM.Width; j++) { imagePGM.Values[i, j] = br.ReadByte(); } } br.Close(); }
public void cornersFilter(ImagePGM image, float[,] filter) { image.Values[0, 0] = (int)((image.Values[0, 0] * filter[1, 1]) + (image.Values[0, 1] * filter[1, 2]) + (image.Values[1, 0] * filter[2, 1]) + (image.Values[1, 1] * filter[2, 2])); image.Values[0, image.Width - 1] = (int)((image.Values[0, image.Width - 1] * filter[1, 1]) + (image.Values[0, image.Width - 2] * filter[1, 0]) + (image.Values[1, image.Width - 2] * filter[2, 0]) + (image.Values[1, image.Width - 1] * filter[2, 1])); image.Values[image.Height - 1, 0] = (int)((image.Values[image.Height - 1, 0] * filter[1, 1]) + (image.Values[image.Height - 2, 0] * filter[0, 1]) + (image.Values[image.Height - 2, 1] * filter[0, 2]) + (image.Values[image.Height - 1, 1] * filter[1, 2])); image.Values[image.Height - 1, image.Width - 1] = (int)((image.Values[image.Height - 1, image.Width - 1] * filter[1, 1]) + (image.Values[image.Height - 2, image.Width - 1] * filter[0, 1]) + (image.Values[image.Height - 2, image.Width - 2] * filter[0, 0]) + (image.Values[image.Height - 1, image.Width - 2] * filter[1, 0])); }
public async Task asyncFilterImage(ImagePGM image, ImagePGM imageCopy, float[,] filter) { var cesp = new ConcurrentExclusiveSchedulerPair(); var watch = System.Diagnostics.Stopwatch.StartNew(); await asyncCenterFilter(image, imageCopy, filter); var t = Task.Run(() => { cornersFilter(image, filter); sidesFilter(image, filter); }); t.Wait(); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Console.WriteLine("czas asynchornicznie: " + elapsedMs + "ms"); }
static void Main() { float f = 1.111111111111111111E-1f; float[,] filter1 = new float[3, 3] { { f, f, f }, { f, f, f }, { f, f, f } }; float[,] filter2 = new float[3, 3] { { 0f, 1.0f, 0.0f }, { 1.0f, -4.0f, 1.0f }, { 0.0f, 1.0f, 0.0f } }; float[,] filter3 = new float[3, 3] { { 0f, -1.0f, 0.0f }, { -1.0f, 5.0f, -1.0f }, { 0.0f, -1.0f, 0.0f } }; float[,] filter4 = new float[3, 3] { { -1.0f, -1.0f, -1.0f }, { -1.0f, 8.0f, -1.0f }, { -1.0f, -1.0f, -1.0f } }; float[,] filter5 = new float[3, 3] { { 1.0f, 0.0f, -1.0f }, { 0.0f, 0.0f, 0.0f }, { -1.0f, 0.0f, 1.0f } }; ImagePGM baboonImage = new ImagePGM(baboonImagePath); ImagePGM baboonImageCopy = new ImagePGM(baboonImagePath); ImageReader imgReader = new ImageReader(baboonImage); ImageReader imgReaderCopy = new ImageReader(baboonImageCopy); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Form1 form = new Form1(); form.PictureBox1.Image = baboonImage.MakeBitmap(1); Splot s = new Splot(); Task t = s.asyncFilterImage(baboonImage, baboonImageCopy, filter4); Task.WaitAll(new Task[] { t }); s.filterImage(baboonImage, baboonImageCopy, filter4); form.PictureBox2.Image = baboonImage.MakeBitmap(1); Application.Run(form); }