如何在C#中找到图像中的像素坐标?

时间:2011-12-17 15:43:47

标签: c# graphics

我想开发一个降噪项目,所以 如何在图像中找到像素坐标。

2 个答案:

答案 0 :(得分:1)

你可以试试这个

Bitmap bmp = new Bitmap("filename");
//to get the pixel color
Color c= bmp.GetPixel(50,50);
//to set the color of the pixel
bmp.SetPixel(50, 50, Color.Green);

以下代码将为您提供图像中的所有像素坐标

namespace WindowsFormsApplication4
{
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;

    public partial class Form1 : Form
    {
        private const string FILE_NAME = @"C:\Temp\Capture.png";
        private const double BW_THRESHOLD = 0.5;
        private readonly Color colorBlack =
          Color.FromArgb(255, 0, 0, 0);
        private readonly Color colorWhite =
          Color.FromArgb(255, 255, 255, 255);
         private readonly Bitmap originalImage;
        private readonly Bitmap convertedImage;
        private readonly List<Vertex> vertices = new List<Vertex>();
        public Form1()
        {
            InitializeComponent();
            pictureBox1.ImageLocation = FILE_NAME;
            this.originalImage = new Bitmap(FILE_NAME);
            this.convertedImage = this.Img2BW(this.originalImage, BW_THRESHOLD);
            foreach (Vertex vert in this.vertices)
            {
                listBox1.Items.Add(vert.ToString());
            }
        }
        private Bitmap Img2BW(Bitmap imgSrc, double threshold)
        {
            int width = imgSrc.Width;
            int height = imgSrc.Height;
            Color pixel;
            Bitmap imgOut = new Bitmap(imgSrc);
            for (int row = 0; row < height - 1; row++)
            {
                for (int col = 0; col < width - 1; col++)
                {
                    pixel = imgSrc.GetPixel(col, row);
                    if (pixel.GetBrightness() < threshold)
                    {
                        this.vertices.Add(new Vertex(col, row));
                        imgOut.SetPixel(col, row, this.colorBlack);
                    }
                    else
                    {
                        imgOut.SetPixel(col, row, this.colorWhite);
                    }
                }
            }
             return imgOut;
        }
    }
    public class Vertex
    {
        public Vertex(int i, int j)
        {
            this.X = i;
            this.Y = j;
        }
        public int X { get; set; }
        public int Y { get; set; }
        public string ToString()
        {
            return string.Format("({0}/{1})", this.X, this.Y);
        }
    }
}

答案 1 :(得分:0)

像素只是位于笛卡尔坐标系中某个位置的颜色,可以在图像中轻松读取和写入这些颜色,您可以使用Bitmap class和{GetPixelSetPixel 3}}方法。