【问题标题】:C# get dominant color in an imageC# 获取图像中的主色
【发布时间】:2017-01-22 23:51:21
【问题描述】:

我正在构建一个从视频中截取屏幕截图的程序。 它从视频中提取帧(使用 ffmpeg),然后将它们合并到一个文件中。

一切正常,除了有时我得到(几乎)黑色图像,主要是在视频的开头和结尾。

我能想到的一个可能的解决方案是检测提取的帧是否是暗的。如果天很黑,请从稍有不同的时间提取另一帧。

如何检测提取的帧是否为深色/黑色?还是有其他方法可以解决这个问题?

private void getScreenshots_Click(object sender, EventArgs e)
{
    int index = 0;
    foreach (string value in this.filesList.Items)
    {
        string file = selectedFiles[index] + "\\" + value;

        // ------------------------------------------------
        //   MediaInfo
        // ------------------------------------------------
        // https://github.com/Nicholi/MediaInfoDotNet
        //
        // get file width, height, and frame count
        //
        // get aspect ratio of the video
        // and calculate height of thumbnail 
        // using width and aspect ratio
        //
        MediaInfo MI = new MediaInfo();
        MI.Open(file);
        var width = MI.Get(StreamKind.Video, 0, "Width");
        var height = MI.Get(StreamKind.Video, 0, "Height");
        decimal d = Decimal.Parse(MI.Get(StreamKind.Video, 0, "Duration"));
        decimal frameCount = Decimal.Parse(MI.Get(StreamKind.Video, 0, "FrameCount"));
        MI.Close();
        decimal ratio = Decimal.Divide(Decimal.Parse(width), Decimal.Parse(height));
        int newHeight = Decimal.ToInt32(Decimal.Divide(newWidth, ratio));
        decimal startTime = Decimal.Divide(d, totalImages);
        //totalImages - number of thumbnails the final image will have
        for (int x = 0; x < totalImages; x++)
        {
            // increase the time where the thumbnail is taken on each iteration
            decimal newTime = Decimal.Multiply(startTime, x);
            string time = TimeSpan.FromMilliseconds(double.Parse(newTime.ToString())).ToString(@"hh\:mm\:ss");

            string outputFile = this.tmpPath + "img-" + index + x + ".jpg";

            // create individual thumbnails with ffmpeg
            proc = new Process();
            proc.StartInfo.FileName = "ffmpeg.exe";
            proc.StartInfo.Arguments = "-y -seek_timestamp 1 -ss " + time + " -i \"" + file + "\" -frames:v 1 -qscale:v 3 \"" + outputFile + "\"";
            proc.Start();
            proc.WaitForExit();
        }

        // set width and height of final image
        int w = (this.cols * newWidth) + (this.spacing * this.cols + this.spacing);
        int h = (this.rows * newHeight) + (this.spacing * this.rows + this.spacing);

        int left, top, i = 0;
        // combine individual thumbnails into one image
        using (Bitmap bmp = new Bitmap(w, h))
        {
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(this.backgroundColor);
                // this.rows - number of rows
                for (int y = 0; y < this.rows; y++)
                {
                    // put images on a column
                    // this.cols - number of columns
                    // when x = number of columns go to next row
                    for (int x = 0; x < this.cols; x++)
                    {
                        Image imgFromFile = Image.FromFile(this.tmpPath + "img-" + index + i + ".jpg");
                        MemoryStream imgFromStream = new MemoryStream();
                        imgFromFile.Save(imgFromStream, imgFromFile.RawFormat);
                        imgFromFile.Dispose();

                        left = (x * newWidth) + ((x + 1) * this.spacing);
                        top = (this.spacing * (y + 1)) + (newHeight * y);
                        g.DrawImage(Image.FromStream(imgFromStream), left, top, newWidth, newHeight);
                        i++;
                    }
                }
            }

            // save the final image
            bmp.Save(selectedFiles[index] + "\\" + value + ".jpg");
        }
        index++;
    }
}

【问题讨论】:

    标签: c# image-processing ffmpeg


    【解决方案1】:

    您需要分析各个帧。循环遍历每帧/图像的像素并计算整体亮度。

    这实际上在SO post 中概述了关于确定图像的整体亮度。您可以将相同的想法应用于您的需求。

    根据您希望如何实现它,它可能会相当繁重。如果这需要即时完成,您很可能希望将帧分析范围缩小到更小的部分,而不是整个图像。但是,如果您将其作为更多的后处理技术,那么它可能就没有那么重要了。

    【讨论】:

    • @CK13 太好了。如果这回答了你的问题,你能接受这个作为结束问题的答案吗?谢谢。
    【解决方案2】:

    感谢gmiley 我设法解决了我的问题(希望如此)。

    方法如下:

    1. 我在课堂上加入了function linked by gmiley
    2. 我添加了一个获取图像并将其转换为位图的函数,找到here
    3. 我将创建缩略图的代码移到了它自己的函数中

      private void ffmpegGetThumb(string input, string output, double time, int getNewFrame = 0)
      {
          // create individual thumbnails with ffmpeg
          string timeString = TimeSpan.FromMilliseconds(time).ToString(@"hh\:mm\:ss");
          proc = new Process();
          proc.StartInfo.FileName = "ffmpeg.exe";
          proc.StartInfo.Arguments = "-y -seek_timestamp 1 -ss " + timeString + " -i \"" + input + "\" -frames:v 1 -qscale:v 3 \"" + output + "\"";
          proc.Start();
          proc.WaitForExit();
      }
      
    4. 我创建了一个函数,可以将缩略图转换为位图,检查其亮度并根据需要调整缩略图的拍摄时间

      private void brightThumb(string input, string output, double time, int getNewFrame, bool goBack)
      {
          //get initial thumbnail
          this.ffmpegGetThumb(input, output, time);
          Bitmap frame = this.ConvertToBitmap(output);
          double brightness = CalculateAverageLightness(frame);
          double newTime = time;
      
          if (CalculateAverageLightness(frame) < 0.1)
          {
              // brightness is to low
              // get a new thumbnail by increasing the time at which it's taken
              if (getNewFrame == 1 && goBack == false)
              {
                  newTime = time + 1000;
              }
              // if goBack is true (the time is beyond 75% of the total duration)
              // we decrease the time (if curent time is at credits no point going forward)
              else if (getNewFrame == 1 && goBack == true)
              {
                  newTime = time - 1000;
              }
              // take a new thumbnail at the adujted time
              this.brightThumb(input, output, newTime, 1, goBack);
          }
      }
      
    5. 调用函数,其中创建拇指的代码在getScreenshots_Click()

      bool goBack = false;
      if (x > (totalImages/4) * 3)
      {
          goBack = true;
      }
      this.brightThumb(file, outputFile, time, 0, goBack);
      

    在性能方面,与执行此检查之前相比,我认为执行时间没有太大变化。如果它发现许多暗图像,那么它将重新拍摄它们,因此这显然会增加执行时间。

    我能想到一个问题。如果视频以足够长的黑暗场景结束以包含 2 个缩略图。在这种情况下,缩略图将是相同的,因为它会倒退一秒,直到找到足够亮的图像。

    【讨论】:

      猜你喜欢
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 2016-09-06
      • 2021-09-15
      • 1970-01-01
      • 2010-11-28
      • 2011-06-30
      相关资源
      最近更新 更多