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

如何在C#中查找图像中的像素坐标?,c#,graphics,C#,Graphics,我想开发一个降噪项目,所以 如何在图像中找到像素坐标。像素只是位于笛卡尔坐标系中某个位置的颜色,要在图像中轻松读取和写入这些颜色,可以使用和方法。您可以尝试此方法 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); 下面的代码将为您

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

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

您可以尝试此方法

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);
        }
    }
}
命名空间窗口窗体应用程序4
{
使用System.Collections.Generic;
使用系统图;
使用System.Windows.Forms;
公共部分类Form1:Form
{
私有常量字符串文件_NAME=@“C:\Temp\Capture.png”;
私有常数双BW_阈值=0.5;
专用只读颜色colorBlack=
Color.FromArgb(255,0,0,0);
专用只读颜色colorWhite=
Color.FromArgb(255、255、255、255);
私有只读位图原始图像;
私有只读位图转换图像;
私有只读列表顶点=新列表();
公共表格1()
{
初始化组件();
pictureBox1.ImageLocation=文件名;
this.originalImage=新位图(文件名);
this.convertedImage=this.Img2BW(this.originalImage,BW_阈值);
foreach(此.vertexs中的顶点顶点顶点)
{
listBox1.Items.Add(vert.ToString());
}
}
专用位图Img2BW(位图imgSrc,双阈值)
{
int width=imgSrc.width;
内部高度=内部高度;
彩色像素;
位图imgOut=新位图(imgSrc);
对于(int row=0;row
应该是
for(int row=0;row
没有
-1