什麼是在C#.NET中對圖像進行像素化的好算法?C#.NET中的一個很好的像素化算法是什麼?
3
A
回答
3
一個簡單但不夠高效的解決方案是將尺寸調整爲較小尺寸,然後使用像素重複調整尺寸。
更好的解決方案將是(僞代碼):
(時間爲O(n),另外的空間(除了可變源圖像):O(1))
// Pixelize in x axis (choose a whole k s.t. 1 <= k <= Width)
var sum = Pixel[0, 0];
for (y = 0; y < Height; y++)
{
for (x = 0; x < Width + 1; x++)
{
if (x % k == 0)
{
sum /= k;
for (xl = Max(0, x-k); xl < x; xl++)
Pixel[y, xl] = sum;
sum = 0;
}
if (x == Width)
break;
sum += Pixel[y, x];
}
}
// Now do the same in the y axis
// (make sure to keep y the outer loop - for better performance)
// If your image has more than one channel, then then Pixel should be a struct.
0
雖然我不知道這個知名的算法,但我確實必須寫類似的東西。我使用的技術非常簡單,但我認爲對於大圖像效率不高。基本上我會拍攝圖像,並在5(或更高的像素塊)像素塊中進行顏色平均,然後使所有這些像素具有相同的顏色。你可以通過對對角像素進行平均來加快速度,這可以節省很多週期,但不夠準確。
1
在this forum的人過有一個相當不錯的算法。它通過獲取每個「塊」中所有顏色的平均值來工作。
我只是用他在C#/ GDI +今天實現:
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Linq;
using System.Text;
/// <summary>
/// Applies a pixelation effect to an image.
/// </summary>
[SuppressMessage(
"Microsoft.Naming",
"CA1704",
Justification = "'Pixelate' is a word in my book.")]
public class PixelateEffect : EffectBase
{
/// <summary>
/// Gets or sets the block size, in pixels.
/// </summary>
private int blockSize = 10;
/// <summary>
/// Gets or sets the block size, in pixels.
/// </summary>
public int BlockSize
{
get
{
return this.blockSize;
}
set
{
if (value <= 1)
{
throw new ArgumentOutOfRangeException("value");
}
this.blockSize = value;
}
}
/// <summary>
/// Applies the effect by rendering it onto the target bitmap.
/// </summary>
/// <param name="source">The source bitmap.</param>
/// <param name="target">The target bitmap.</param>
public override void DrawImage(Bitmap source, Bitmap target)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (target == null)
{
throw new ArgumentNullException("target");
}
if (source.Size != target.Size)
{
throw new ArgumentException("The source bitmap and the target bitmap must be the same size.");
}
using (var graphics = Graphics.FromImage(target))
{
graphics.PageUnit = GraphicsUnit.Pixel;
for (int x = 0; x < source.Width; x += this.BlockSize)
{
for (int y = 0; y < source.Height; y += this.BlockSize)
{
var sums = new Sums();
for (int xx = 0; xx < this.BlockSize; ++xx)
{
for (int yy = 0; yy < this.BlockSize; ++yy)
{
if (x + xx >= source.Width || y + yy >= source.Height)
{
continue;
}
var color = source.GetPixel(x + xx, y + yy);
sums.A += color.A;
sums.R += color.R;
sums.G += color.G;
sums.B += color.B;
sums.T++;
}
}
var average = Color.FromArgb(
sums.A/sums.T,
sums.R/sums.T,
sums.G/sums.T,
sums.B/sums.T);
using (var brush = new SolidBrush(average))
{
graphics.FillRectangle(brush, x, y, (x + this.BlockSize), (y + this.BlockSize));
}
}
}
}
}
/// <summary>
/// A structure that holds sums for color averaging.
/// </summary>
private struct Sums
{
/// <summary>
/// Gets or sets the alpha component.
/// </summary>
public int A
{
get;
set;
}
/// <summary>
/// Gets or sets the red component.
/// </summary>
public int R
{
get;
set;
}
/// <summary>
/// Gets or sets the blue component.
/// </summary>
public int B
{
get;
set;
}
/// <summary>
/// Gets or sets the green component.
/// </summary>
public int G
{
get;
set;
}
/// <summary>
/// Gets or sets the total count.
/// </summary>
public int T
{
get;
set;
}
}
}
買者自負,工作我的機器上,&等
相關問題
- 1. 什麼將是一個很好的算法來找到圖像移位(Java)
- 2. .NET支持SSL的一個很好的XMPP庫是什麼?
- 3. 什麼是一個很好的jQuery timePicker?
- 4. 什麼是一個很好的教程/如何在.net/c#套接字編程
- 5. 什麼是一個很好的方法來檢查一個double是否是C#中的整數?
- 6. 是否有一個很好的Javascript算術加法運算符?
- 7. 對此,什麼是一個好的集中趨勢算法?
- 8. Java中LTRIM和RTRIM的一個很好的選擇是什麼?
- 9. 什麼是.net 4.0表達式樹的一個很好的用例?
- 10. 什麼是一些很好的替代序列化格式?
- 11. 什麼是一個很好的解析nifi中的傳入網址的方法?
- 12. 什麼是一個很好的緩存命中/失敗率?
- 13. 什麼是一個很好的3D庫與布爾運算的iOS
- 14. 什麼是一些很好的示例桌面應用程序項目,我可以在C#.NET中做什麼
- 15. 什麼是一個好的風扇速度算法?
- 16. 什麼是一個好的快速尋路算法?
- 17. 什麼是好的(半)異步算法?
- 18. 什麼是好的重疊組算法?
- 19. 什麼是洗牌最好的算法?
- 20. 什麼是好的輪詢算法
- 21. 在.Net中實現這個最好的方法是什麼?
- 22. 什麼是使一個類不可序列化的好方法?
- 23. 什麼將是一個很好的部件在QT4的「積分」
- 24. Flyweight模式的一個很好的例子是什麼?
- 25. 什麼是一個很好的典型的網絡超時?
- 26. 什麼是Mediastore對象的一個很好的simpleCursorAdapter實現?
- 27. 什麼是CSS依賴javascript的一個很好的例子?
- 28. 使用SQL視圖的一個很好的理由是什麼?
- 29. 開源Java遊戲的一個很好的例子是什麼?
- 30. 在C#中縮放圖像的好方法是什麼?
你想幹什麼 「像素化」 意味着什麼? – Seth 2009-12-31 20:49:54
你需要這個速度有多快?好醇收縮擴展效果很好,但速度很慢。如果你只需要做一次,就不要幻想。 – 2010-04-09 16:21:56