1
所有的時間尺寸是1920x1080,但我希望它是例如10x10,因此該功能中的過程將更快。我不介意位圖的大小或其質量,我只是希望它更快。如何在此函數中調整位圖大小?
在這個函數中,我創建每個位圖的直方圖。問題是FOR循環需要很長時間。
public static long[] GetHistogram(Bitmap b)
{
long[] myHistogram = new long[256]; // histogram סופר כמה יש מכל גוון
BitmapData bmData = null;
//b = new Bitmap(100, 100);
//b.SetPixel(0,0,Color.FromArgb(10,30,40,50)); //run and make it come to here
try
{
ResizeBitmap(b, 10, 10);
//Lock it fixed with 32bpp
bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int scanline = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nWidth = b.Width;
int nHeight = b.Height;
for (int y = 0; y < nHeight; y++)
{
for (int x = 0; x < nWidth; x++)
{
long Temp = 0;
Temp += p[0]; // p[0] - blue, p[1] - green , p[2]-red
Temp += p[1];
Temp += p[2];
Temp = (int)Temp/3;
myHistogram[Temp]++;
//we do not need to use any offset, we always can increment by pixelsize when
//locking in 32bppArgb - mode
p += 4;
}
}
}
b.UnlockBits(bmData);
}
catch
{
try
{
b.UnlockBits(bmData);
}
catch
{
}
}
return myHistogram; // to save to a file the histogram of all the bitmaps/frames each line contain 0 to 256 values of a frame.
}
所以我調用這個函數:ResizeBitmap(b,10,10); 並嘗試調整每個位,但此行後,我看到的是,位圖大小仍然是1920×1080
這是ResizeBitmap功能:
public static Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
Bitmap result = new Bitmap(nWidth, nHeight);
using (Graphics g = Graphics.FromImage((Image)result))
g.DrawImage(b, 0, 0, nWidth, nHeight);
return result;
}
也許還有另一種方法,使GetHistogram功能工作得更快?
古法你能告訴我如何做到這一步?告訴我我的代碼應該如何?請。謝謝。 – user1741587
確定改變了我在/ Get函數頂部添加的大小:public static long [] GetHistogram(Bitmap b) {[0] myHistogram = new long [256]; BitmapData bmData = null; 位圖newBitmap = ResizeBitmap(b,10,10); b.Dispose(); b = newBitmap;現在該做什麼以及如何邁步? – user1741587
@ user1741587:每行之後:'p + = scanline - nWidth * 4;'。 – Guffa