________QT________
QImage img("E:/IMG.bmp"); // load ảnh trên ổ E ( ảnh chụp màn hình 1280x1024 )
QElapsedTimer tmr; // bộ đếm thời gian thực
void MainWindow::on_pushButton_clicked()
{
int depth = img.depth()/8; // số byte / 1 pixel
int stride=depth*img.width(); // số byte trên 1 line ngang
int height = img.height(); // số line dữ liệu
unsigned char** dataptr = new unsigned char*[height]; // tạo mảng con trỏ
for(int i=0;i<height;i++)
dataptr[i]=img.scanLine(i); // gán địa chỉ cho mỗi con trỏ
qint64 time=0; // biến lưu thời gian thực thi
tmr.restart(); // reset thời gian + bắt đầu tính thời gian
for(int i=0;i<100;i++) // lặp 100 lần
{
for(int y=0;y<height;y++) // duyệt từng line trên ảnh
{
//duyệt từng điểm ảnh trên line
for(int x=0;x<stride;x+=depth)
{
// Lúc trước dùng thế này chậm 7 lần
//img.scanLine(y)[x]++; // tăng màu blue
//img.scanLine(y)[x+1]++; // tăng màu green
//img.scanLine(y)[x+2]++; // tăng màu red
// bây giờ dùng thế này thì nhanh hơn C# khoảng 1.1 - 1.2 lần.
dataptr[y][x]++; // tăng màu blue
dataptr[y][x+1]++; // tăng màu green
dataptr[y][x+2]++; // tăng màu red
}
}
}
time=tmr.elapsed(); // kết thúc, lấy thời gian thực thi.
ui->lblDisplay->setPixmap(QPixmap::fromImage(img)); // hiển thị ảnh sau xử lý
ui->lblTime->setText("Thời gian xử lý : " + QString::number(time) + " ms"); // hiển thị thời gian
int fps = 100000/time; // tính fps
ui->lblFps->setText("Frame per second : " + QString::number(fps)); // hiển thị fps
}
[/PHP]
QImage img("E:/IMG.bmp"); // load ảnh trên ổ E ( ảnh chụp màn hình 1280x1024 )
QElapsedTimer tmr; // bộ đếm thời gian thực
void MainWindow::on_pushButton_clicked()
{
int depth = img.depth()/8; // số byte / 1 pixel
int stride=depth*img.width(); // số byte trên 1 line ngang
int height = img.height(); // số line dữ liệu
unsigned char** dataptr = new unsigned char*[height]; // tạo mảng con trỏ
for(int i=0;i<height;i++)
dataptr[i]=img.scanLine(i); // gán địa chỉ cho mỗi con trỏ
qint64 time=0; // biến lưu thời gian thực thi
tmr.restart(); // reset thời gian + bắt đầu tính thời gian
for(int i=0;i<100;i++) // lặp 100 lần
{
for(int y=0;y<height;y++) // duyệt từng line trên ảnh
{
//duyệt từng điểm ảnh trên line
for(int x=0;x<stride;x+=depth)
{
// Lúc trước dùng thế này chậm 7 lần
//img.scanLine(y)[x]++; // tăng màu blue
//img.scanLine(y)[x+1]++; // tăng màu green
//img.scanLine(y)[x+2]++; // tăng màu red
// bây giờ dùng thế này thì nhanh hơn C# khoảng 1.1 - 1.2 lần.
dataptr[y][x]++; // tăng màu blue
dataptr[y][x+1]++; // tăng màu green
dataptr[y][x+2]++; // tăng màu red
}
}
}
time=tmr.elapsed(); // kết thúc, lấy thời gian thực thi.
ui->lblDisplay->setPixmap(QPixmap::fromImage(img)); // hiển thị ảnh sau xử lý
ui->lblTime->setText("Thời gian xử lý : " + QString::number(time) + " ms"); // hiển thị thời gian
int fps = 100000/time; // tính fps
ui->lblFps->setText("Frame per second : " + QString::number(fps)); // hiển thị fps
}
[/PHP]
Code:
for ( int row = 1; row < img.height() + 1; ++row )
{
for ( int col = 1; col < img.width() + 1; ++col )
{
(img.pixel( row, col )).setBlueF(val1);
(img.pixel( row, col )).setGreenF(val2);
(img.pixel( row, col )).setRedF(val3);
//Hoặc sử dụng hàm sau
//(img.pixel( row, col )).setRgbF(val1, val2, val3, val4);
}
}
______SCharp______
[PHP
Bitmap img = new Bitmap("E:\\IMG.bmp"); // load ảnh trên ổ E ( ảnh chụp màn hình 1280x1024)
Stopwatch st = new Stopwatch(); // bộ đếm thời gian thực
private unsafe void button1_Click(object sender, EventArgs e) // hàm xử lý
{
Rectangle __rect = new Rectangle(0, 0, img.Width, img.Height); // hình chữ nhật bằng kích thước ảnh
BitmapData __bmpdata = img.LockBits(__rect,ImageLockMode.ReadWrite, img.PixelFormat); // khóa dữ liệu ảnh
int stride = __bmpdata.Stride; // lấy số byte trên 1 line ngang
int height = __bmpdata.Height; // lấy số line của ảnh
int depth = stride / __bmpdata.Width; // lấy số byte trên 1 pixel
byte* ptr = (byte*)__bmpdata.Scan0.ToPointer(); // lấy con trỏ dữ liệu ảnh
byte*[] pdata = new byte*[height]; // tạo mảng con trỏ dữ liệu ảnh
for (int i = 0; i < height; i++)
pdata[i] = ptr + i * stride; // gán địa chỉ cho mỗi con trỏ , mỗi con trỏ sẽ trỏ tới byte đầu tiên của 1 line
img.UnlockBits(__bmpdata); // mở khóa ảnh để cho phép các truy cập khác
long time = 0; // biến lưu thời gian xử lý
st.Restart(); // reset bộ đếm + bắt đầu tính thời gian thực thi
for(int i=0;i<100;i++) // lặp 100 lần
{
for(int y=0;y<height;y++) // duyệt từng line
{
for(int x=0;x<stride;x+=depth) // duyệt từng pixel
{
pdata[y][x]++; // tăng blue
pdata[y][x + 1]++; // tăng green
pdata[y][x + 2]++; // tăng red
}
}
}
time = st.ElapsedMilliseconds; // kết thúc xử lý, lấy thời gian thực thi
st.Stop(); // dừng bộ đếm
ptxDisplay.Image = img; // hiển thị ảnh
lblTime.Text = "Thời gian xử lý : " + time.ToString() + " ms"; // hiển thị thời gian
int fps = (int)(100000 / time); // tính fps
lblFps.Text = "Frame per second : " + fps.ToString(); // hiển thị fps
}
}
[/PHP]
Code:
public class CompvisBitmap
{
Bitmap source = null;
IntPtr Iptr = IntPtr.Zero;
BitmapData bitmapData = null;
public byte[] Pixels { get; set; }
public int Depth { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }
public CompvisBitmap(Bitmap source)
{
this.source = source;
}
public void LockBits()
{
try
{
// Query kích thước ảnh
Width = source.Width;
Height = source.Height;
//Tổng số pixel cần xử lý
int PixelCount = Width * Height;
//Rectangle cần lock
Rectangle rect = new Rectangle(0, 0, Width, Height);
//Depth của ảnh
Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);
// Kiểm tra bpp: 8, 24, hoặc 32
if (Depth != 8 && Depth != 24 && Depth != 32)
{
throw new ArgumentException("Chỉ có các ảnh với bpp: 8, 24 và 32 đươc sử dụng");
}
//Lấy dữ liệu bitmap
bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
source.PixelFormat);
//Sao chép pixels
int step = Depth / 8;
Pixels = new byte[PixelCount * step];
Iptr = bitmapData.Scan0;
// Chuyển từ con trỏ sang mảng
Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
}
catch (Exception ex)
{
throw ex;
}
}
public void UnlockBits()
{
try
{
// Chuyển từ mảng sang con trỏ
Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);
// khôi phục dữ liệu ảnh
source.UnlockBits(bitmapData);
}
catch (Exception ex)
{
throw ex;
}
}
//Lấy về color của điểm ảnh
public Color GetPixel(int x, int y)
{
Color clr = Color.Empty;
// Số thành phần màu
int cCount = Depth / 8;
// Chỉ số bắt đầu của điểm ảnh tại vị trí (x,y)
int i = ((y * Width) + x) * cCount;
if (i > Pixels.Length - cCount)
throw new IndexOutOfRangeException();
if (Depth == 32) // Với ảnh có bpp=32 -->> [Red, Green, Blue và Alpha]
{
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
byte a = Pixels[i + 3]; // a
clr = Color.FromArgb(a, r, g, b);
}
if (Depth == 24) // Với ảnh có bpp=24 -->> [Red, Green và Blue]
{
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
clr = Color.FromArgb(r, g, b);
}
if (Depth == 8) // Với ảnh có bpp=8 bpp -->> [Red, Green và Blue]
{
byte c = Pixels[i];
clr = Color.FromArgb(c, c, c);
}
return clr;
}
//Gán giá trị màu của điểm ảnh (x,y)
public void SetPixel(int x, int y, Color color)
{
// Số thành phần màu
int cCount = Depth / 8;
// Chỉ số bắt đầu của điểm ảnh tại vị trí (x,y)
int i = ((y * Width) + x) * cCount;
if (Depth == 32) // Với ảnh có bpp=32 -->> [Red, Green, Blue và Alpha]
{
Pixels[i] = color.B;
Pixels[i + 1] = color.G;
Pixels[i + 2] = color.R;
Pixels[i + 3] = color.A;
}
if (Depth == 24) // Với ảnh có bpp=24 -->> [Red, Green và Blue]
{
Pixels[i] = color.B;
Pixels[i + 1] = color.G;
Pixels[i + 2] = color.R;
}
if (Depth == 8) // [Red, Green và Blue]
{
Pixels[i] = color.B;
}
}
}
//Dành để kiểm tra thời gian thực thi
public class TimeBenchmark
{
private static DateTime sDate = DateTime.MinValue;
private static DateTime eDate = DateTime.MinValue;
public static TimeSpan Span { get { return eDate.Subtract(sDate); } }
public static void Start() { sDate = DateTime.Now; }
public static void End() { eDate = DateTime.Now; }
public static double GetSeconds()
{
if (eDate == DateTime.MinValue) return 0.0;
else return Span.TotalSeconds;
}
}
Code:
public void HowToUse()
{
Bitmap bmp = (Bitmap)Image.FromFile("d:\\test.png");
TimeBenchmark.Start();
CompvisBitmap lBitmap = new CompvisBitmap(bmp);
lockBitmap.LockBits();
Color cmpClr = Color.FromArgb(255, 255, 255, 255);
for (int y = 0; y < lBitmap.Height; y++)
{
for (int x = 0; x < lBitmap.Width; x++)
{
if (lBitmap.GetPixel(x, y) == cmpClr)
{
lBitmap.SetPixel(x, y, Color.Red);
}
}
}
lBitmap.UnlockBits();
TimeBenchmark.End();
double seconds = TimeBenchmark.GetSeconds();
bmp.Save("d:\\ketqua_day.png"); }
Với QT:
Code:
for(int i=0;i<height;i++)
{
delete[] dataptr[i];
}
delete[] dataptr;
Code:
for(int i=0;i<height;i++)
{
delete[] pdata [i];
}
delete[] pdata ;


Comment