Hiển Thị Histogram Của ảnh Bằng Thư Viện Aforge.NET

(Cập nhật ngày 21/05/2013)

Để hiển thị/vẽ histogram cho ảnh, ta có thể sử dụng các hàm trong thư viện của EmguCV. Tuy nhiên trong bài này hướng dẫn sử dụng hàm trong thư viện của Aforge.NET framework.

AForge.NET là framework C# mã nguồn mở, được thiết kế cho các developer và reseacher thuộc lĩnh vực Thị giác Máy tính và Trí tuệ Nhân tạo (Computer Vision and Artificial Intelligence) – image processing, neural networks, genetic algorithms, fuzzy logic, machine learning, robotics, etc.

Framework này bao gồm tập hợp các thư viện và ứng dụng mẫu minh họa các đặc tính sau:

AForge.Imaging – library with image processing routines and filters; AForge.Vision – computer vision library; AForge.Video – set of libraries for video processing; AForge.Neuro – neural networks computation library; AForge.Genetic – evolution programming library; AForge.Fuzzy – fuzzy computations library; AForge.Robotics – library providing support of some robotics kits; AForge.MachineLearning – machine learning library; etc.

Chuẩn bị

  • MS Visual Studio 2010, C#
  • EmguCV 2.4.2
  • Aforge.NET 2.2.4 (tại tải http://www.aforgenet.com/framework/downloads.html)

Thực hiện

Add reference 3 file: AForge.Controls.dllAforge.Imaging.dllAforge.Math.dll Khai báo trong code

using AForge.Math; using AForge.Imaging;

Trong Visual Studio – Toolbox, click phải lên một tab bất kì > Add tab > gõ tên mới: Aforge Click phải lên tab Aforge mới tạo > Choose Items… Trong hộp thoại hiển thị – tab .NET Framwork Components, browse đến tập tin Aforge.Controls.dll > check chọn các control Aforge > OK

Kéo thả control Histogram lên form.

Khai báo các đối được thuộc lớp ImageStatisticsHistogram dành cho ảnh gốc (before) và ảnh kết quả (after).

// Khai báo biến 2 đối tượng Stats và ActiveHistogram private ImageStatistics StatsBefore; private ImageStatistics StatsAfter; private Histogram ActiveHistogramBefore; private Histogram ActiveHistogramAfter;

Đối với ảnh xám, ta chỉ hiển thị được histogram độ sáng (luminosity)

// khởi tạo các đối tượng của lớp ImageStatistics StatsBefore = new ImageStatistics(imageBefore); StatsAfter = new ImageStatistics(imageAfter); // nếu ảnh gốc là ảnh xám if (StatsBefore.IsGrayscale) { // thiết lập histogram dành cho kênh màu xám ActiveHistogramBefore = StatsBefore.Gray; ActiveHistogramAfter = StatsAfter.Gray; // thiết lập màu hiển thị của histogram là Black histogramBefore.Color = Color.Black; histogramAfter.Color = Color.Black; // gắn các histogram lên các control histogram trên form histogramBefore.Values = ActiveHistogramBefore.Values; histogramAfter.Values = ActiveHistogramAfter.Values; }

Trong đoạn code trên, imageBefore và imageAfter phải được khai báo kiểu Bitmap. histogramBefore và histogramAfter là 2 control histogram có trên form.

Đối với ảnh màu RGB, ta sử dụng đoạn code dưới đây. Trong đó qui ước kênh màu (channel) 0 – xanh dương, 1 – xanh lá, 2 – đỏ.

switch (channel) { case 0: // thiết lập histogram dành cho kênh màu blue ActiveHistogramBefore = StatsBefore.Blue; ActiveHistogramAfter = StatsAfter.Blue; // thiết lập màu hiển thị của histogram là Blue histogramBefore.Color = Color.Blue; histogramAfter.Color = Color.Blue; break; case 1: ActiveHistogramBefore = StatsBefore.Green; ActiveHistogramAfter = StatsAfter.Green; histogramBefore.Color = Color.Green; histogramAfter.Color = Color.Green; break; case 2: ActiveHistogramBefore = StatsBefore.Red; ActiveHistogramAfter = StatsAfter.Red; histogramBefore.Color = Color.Red; histogramAfter.Color = Color.Red; break; } // gắn các histogram lên các control histogram trên form histogramBefore.Values = ActiveHistogramBefore.Values; histogramAfter.Values = ActiveHistogramAfter.Values;

Ảnh minh họa khi khi chạy chương trình

histogram grayscale

histogram blue

histogram green

histogram red

Để cân bằng histogram, có thể dùng nhiều cách, chẳng hạn sử dụng hàm trong EmguCV. Code tự viết có thể xem trong bài khác trên blog.

Share this:

  • X
  • Facebook
Like Loading...

Related

Từ khóa » Thư Viện Aforge