【问题标题】:Image processing code help - C#图像处理代码帮助 - C#
【发布时间】:2013-05-27 01:10:23
【问题描述】:

好的,是时候寻求更多 n00b 帮助了。
我一直在用 C# 编写这段代码(确切地说是单声道)。它旨在拍摄图像,逐像素分解,并将 R、G 和 B 值作为颜色坐标写入文本文件。
问题是,我没有得到任何处理。我只是得到一个红色的 0(for() 循环正上方的百分比函数和一个空文件。没有生成文本/坐标。为了清楚起见,我将在代码本身下方发布控制台输出。

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Image_Interpreter_I
{

class MainClass
{
    public static void Main (string[] args)
    {
            Console.WriteLine ("Enter filepath for bitmap image");
            Console.ForegroundColor = ConsoleColor.Blue;
            string filepath = Console.ReadLine ();
            Console.ResetColor();
            Bitmap image1 = new Bitmap(System.Drawing.Image.FromFile(filepath, true));
            Console.WriteLine ("Enter filepath for file interpretation output");
            Console.ForegroundColor = ConsoleColor.Blue;
            string filepath2 = Console.ReadLine();
            Console.ResetColor();
            int i;
            i = 1;
            int ImW, ImH;
            string ImWstr, ImHstr;
            Console.WriteLine ("Enter width of image:");
            Console.ForegroundColor = ConsoleColor.Blue;
            ImWstr = Console.ReadLine ();
            Console.ResetColor ();
            ImW = Convert.ToInt16 (ImWstr);
            Console.WriteLine ("Enter height of image:");
            Console.ForegroundColor = ConsoleColor.Blue;
            ImHstr = Console.ReadLine ();
            ImH = Convert.ToInt16 (ImHstr);
            decimal percent;
            percent = (i / (ImH * ImW)) * 100;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine (percent);
            Console.ResetColor ();
            for (int y = 1; y <= ImH; y++)
            {
                for (int x = 1; x <= ImW; x++)
                {
                    Color pixelColor = image1.GetPixel(x,y);
                    byte r = pixelColor.R;
                    byte g = pixelColor.G;
                    byte b = pixelColor.B;
                    string pixData = String.Format ("({0},{1},{2}", new object[] {r, g, b});
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath2))
                    {
                        file.WriteLine(pixData);

                    }
                }
            }
        i++;    
}
 }
 }

控制台输出:

输入位图图像的文件路径
/Users/user0/Desktop/IMG_1528.JPG
输入文件解释输出的文件路径
/Users/user0/Desktop/imageData.txt
输入图片宽度:
2448 输入图像高度: 3264 0

【问题讨论】:

  • 我的怀疑是,当您在 for 循环中间执行 System.IO.StreamWriter file = new System.IO.StreamWriter(filepath2) 时,您会尝试打开一个已由其他句柄独占打开的文件进行写入。如果是这样,解决方案:在两个 for 循环之外的使用中打开它只写一次并且只写一次。如果这不起作用,请尝试附加调试器并查看它卡在什么地方。

标签: c# image-processing bitmap jpeg


【解决方案1】:
        static void Main(string[] args)
    {
        string filepath = @"C:\SomePath\SomeFile.JPG";
        Bitmap image1 = new Bitmap(System.Drawing.Image.FromFile(filepath, true));
        string filepath2 = @"C:\temp\myFile.txt";

        using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath2))
        {
            for (int y = 0; y < image1.Height; y++)
            {
                for (int x = 0; x < image1.Width; x++)
                {
                    Color pixelColor = image1.GetPixel(x, y);
                    byte r = pixelColor.R;
                    byte g = pixelColor.G;
                    byte b = pixelColor.B;

                    string pixData = String.Format("({0},{1},{2})", new object[] { r, g, b });

                    file.WriteLine(pixData);
                }
            }
        }
    }

【讨论】:

    【解决方案2】:

    第一件事:您在每个循环条目上创建新的流写入器 - 每次都会覆盖现有文件并且也会很慢。先创建这个流,再做处理。

    using (var file = new System.IO.StreamWriter(filepath2))
    {
        // for loops
    }
    

    其次,根据文档:Bitmap.GetPixel 像素的索引为 0 到 Width-1/Height-1 - 这会在您的 for 循环中提供不正确的 lvaues。从 0 开始,将循环结束条件更改为 y &lt; ImHx &lt; ImW

    另一件事 - 尝试更好地格式化代码以备将来使用,提取方法以便于理解和阅读代码。

    【讨论】:

      【解决方案3】:

      我会尝试在循环外打开文件:

      using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath2))
      {
      
         for (int y = 1; y <= ImH; y++)
         {
            for (int x = 1; x <= ImW; x++)
            {
                Color pixelColor = image1.GetPixel(x,y);
                byte r = pixelColor.R;
                byte g = pixelColor.G;
                byte b = pixelColor.B;
                string pixData = String.Format ("({0},{1},{2}", new object[] {r, g, b});
                file.WriteLine(pixData);
             }
          }
      }
      

      这应该会有所帮助:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-12
        • 2010-11-10
        相关资源
        最近更新 更多