Diễn đàn    Dotnet framework    Một số kỹ thuật xử lý hình ảnh trong dotnet

Thành viênTrả lời
awas


57  bài
29-11-2008 21:31:49
Chuyển 1 ảnh từ màu thành ảnh xám.

Công thức đơn giản

xám = (red + green + blue)/3

rồi áp tất cả 3 màu thành xám thế là xong.

public static Bitmap Grayscale(Bitmap source)
{
Bitmap bm = new Bitmap(source.Width, source.Height);
for (int y = 0; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
Color c = source.GetPixel(x, y);
int luma = (int)((c.R + c.G + c.B) / 3);
bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
}
}

return bm;
}
 
awas


57  bài
29-11-2008 21:33:19
Cắt một phần ảnh từ vị trí x,y với độ rộng wid, hei. Hoặc cắt 1 đoạn ảnh được định nghĩa bởi rectangle.

public static Bitmap Select(Bitmap source, Rectangle rec)
{
Bitmap bmp = new Bitmap(rec.Width, rec.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(source, 0, 0, rec, GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}
 
awas


57  bài
29-11-2008 21:34:1
Thu nhỏ hoặc phóng to một ảnh

public static Bitmap Zoom(Bitmap source, int percent)
{
int wid = (percent * source.Width) / 100;
int hei = (percent * source.Height) / 100;
Bitmap bmp = new Bitmap(wid, hei);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(source, 0, 0, wid, hei);
g.Dispose();
return bmp;
}
---
 
awas


57  bài
29-11-2008 21:37:30
Lựa chọn 1 vùng giống magic wand trong photo shop

Đây là kỹ thuật khó. Ban đầu chúng ta sẽ xuất phát từ 1 điểm, nếu điểm bên cạnh có màu tương đương. Tức là hiệu của màu nằm trong 1 ngưỡng nào đó giả sử là distance thì coi như chúng có màu tương đương. Với 2 màu tương đương ở cạnh nhau ta mở rộng vùng được chọn. Như vậy Load Selection sẽ từ 1 điểm lan ra các phía để tạo thành vùng selêction kết quả là 1 mảng int với tọa độ chính là tọa độ x,y của vùng trên ảnh.

Code
--------------------------------------

private static bool Inbound(int x, int y, int wid, int hei)
{
if (x < 0 || x >= wid) return false;
if (y < 0 || y >= hei) return false;
return true;
}

public static int[,] LoadSelection(Bitmap source, int start_x, int start_y)
{
Color c = source.GetPixel(start_x, start_y);
int wid = source.Width;
int hei = source.Height;

int[,] sel = new int[wid, hei];
for (int x = 0; x < wid; x++)
{
for (int y = 0; y < hei; y++)
sel[x, y] = 0;

}

for (int x = 0; x < wid; x++)
{
for (int y = 0; y < hei; y++)
sel[x, y] = 0;
}

sel[start_x, start_y] = 1;
Stack st = new Stack();
st.Push(new Point(start_x, start_y));


do
{
Point p = (Point)st.Pop();

Color c2 = source.GetPixel(p.X, p.Y);
if (System.Math.Abs(c2.R - c.R) < distance)
{
int x = p.X;
int y = p.Y;
sel[x, y] = 1;
if (Inbound(x - 1, y, wid, hei))
if(sel[x-1, y] == 0) st.Push(new Point(x - 1, y));
if (Inbound(x + 1, y, wid, hei))
if (sel[x + 1, y] == 0) st.Push(new Point(x + 1, y));
if (Inbound(x, y - 1, wid, hei))
if (sel[x, y - 1] == 0) st.Push(new Point(x, y - 1));
if (Inbound(x, y + 1, wid, hei))
if (sel[x, y + 1] == 0) st.Push(new Point(x, y + 1));
}
}
while (st.Count > 0);

return sel;
}
---
 
awas


57  bài
29-11-2008 21:38:26
Có vùng selection rồi fill một màu vào vùng đó

public static Bitmap FillSelection(Bitmap source, int[,] sel, int c)
{
for (int x = 0; x < source.Width; x++)
{
for (int y = 0; y < source.Height; y++)
{
if (sel[x, y] == 1)
source.SetPixel(x, y, Color.FromArgb(c, c, c));
}
}
return source;
}
---
 
lyngochung


17  bài
6-12-2008 2:16:22
Cám ơn bài viết của bác. Bác có thuật toán hay code về cắt ảnh ký tự trong một chuỗi ký tự ảnh không ạ. Em đang làm bài tập về nhận dạng ký tự viết tay.
---
 
quanlv


26  bài
7-9-2009 9:49:23
Các đoạn code rất hay ! Bạn có sản phẩm demo đưa lên cho anh em chiêm ngưỡng đi.
---
 
awas


57  bài
7-9-2009 17:30:38
Các đoạn code rất hay ! Bạn có sản phẩm demo đưa lên cho anh em chiêm ngưỡng đi.
---



http://tracnghiem.awas.vn/
http://tracnghiem.awas.vn/?step=1
http://tracnghiem.awas.vn/?step=2
http://tracnghiem.awas.vn/?step=3
http://tracnghiem.awas.vn/?step=4
http://tracnghiem.awas.vn/?step=5

Chấm điểm đề thi trắc nghiệm
---
 
aabbccdd


1  bài
22-3-2010 9:15:54
Cắt một phần ảnh từ vị trí x,y với độ rộng wid, hei. Hoặc cắt 1 đoạn ảnh được định nghĩa bởi rectangle.

public static Bitmap Select(Bitmap source, Rectangle rec)
{
Bitmap bmp = new Bitmap(rec.Width, rec.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(source, 0, 0, rec, GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}


Bạn có thể hướng hướng dẫn cách code để chụp ảnh 1 window có scrolbar không .Tks
 

Chủ đề gần đây :

Cùng loại :

Tên file Mô tả chi tiết Ngày
NWeb.zip (1) Module đơn giản Newsweb trên Dotnetnuke v10.x.x.x10/18/2025 8:08:11 AM
vspforum.zip (11) Ma nguon vspforum ngay xua4/18/2023 6:38:37 AM
pdfjs.rar (2) pdfjs 2017 : hiển thị tốt trên iphone 11, 12, 13 không lỗi, bản 2012 sẽ lỗi trên iphone6/21/2022 11:52:48 AM
pdfjs2.rar (2) Xem file pdf bằng viewer.hml cua pdfjs (thư viện chuẩn mozilla) 2012. https://mozilla.github.io/pdf.js/getting_started/#download có thể download bản prebuild tại đây6/21/2022 11:52:04 AM
runner.zip (0) using three.js, orbitcontrol to view an object move random on map. Di chuyển 1 đồ vật ngẫu nhiên trên bản đồ, sử dụng với demo nhân viên di chuyển trong văn phòng. Toàn js download về là chạy12/5/2019 5:55:14 PM
gmap.zip (1) google map + marker7/17/2019 2:25:05 PM
vinsmarthomeservice.zip (1) java post json to api, use AsyncTask, event listener7/9/2019 5:00:10 PM
fblogin.zip (0) Login facebook bang javascript SDK7/9/2019 9:16:37 AM
autocomplete-location.zip (2) autocomplete location geo from google place, html + js7/4/2019 4:37:55 PM
WebAPI.zip (8) api for android access db (v1.0.0)7/4/2019 9:14:17 AM