Áp dụng Bộ lọc Trung vị và Wiener
Áp dụng Bộ lọc Trung vị và Wiener
Bộ lọc trung vị là một kỹ thuật lọc kỹ thuật số phi tuyến được sử dụng thường xuyên để loại bỏ nhiễu. Việc giảm nhiễu như vậy thường là bước tiền xử lý điển hình để cải thiện kết quả của việc xử lý sau này. Bộ lọc Wiener là bộ lọc tuyến tính cố định MSE (mean squared error) tối ưu cho các hình ảnh bị biến dạng do nhiễu cộng và làm mờ. Bằng cách sử dụng Aspose.PSD cho API Java, nhà phát triển có thể áp dụng bộ lọc trung vị để giảm nhiễu cho hình ảnh và có thể áp dụng bộ lọc Gauss Wiener cho hình ảnh. Bài viết này demo cách áp dụng bộ lọc trung vị và bộ lọc Gauss Wiener cho hình ảnh.
Áp dụng Bộ lọc Trung vị
Aspose.PSD cung cấp lớp MedianFilterOptions để áp dụng bộ lọc trên một RasterImage. Đoạn mã dưới đây mô tả cách áp dụng bộ lọc trung vị lên một hình ảnh raster.
String dataDir = Utils.getDataDir(ApplyMedianAndWienerFilters.class) + "Conversion/"; | |
String sourceFile = dataDir + "sample.psd"; | |
String destName = dataDir + "median_test_denoise_out.gif"; | |
try (Image image = Image.load(sourceFile); | |
// Cast the image into RasterImage | |
RasterImage rasterImage = (RasterImage) image) { | |
if (rasterImage == null) { | |
return; | |
} | |
// Create an instance of MedianFilterOptions class and set the size, Apply MedianFilterOptions filter to RasterImage object and Save the resultant image | |
MedianFilterOptions options = new MedianFilterOptions(4); | |
rasterImage.filter(image.getBounds(), options); | |
image.save(destName, new GifOptions()); | |
} |
Áp dụng Bộ lọc Gauss Wiener
Aspose.PSD cung cấp lớp GaussWienerFilterOptions để áp dụng bộ lọc trên một RasterImage. Đoạn mã dưới đây mô tả cách áp dụng bộ lọc Gauss Wiener lên một hình ảnh raster.
String dataDir = Utils.getDataDir(ApplyGausWienerFilters.class) + "Conversion/"; | |
String sourceFile = dataDir + "sample.psd"; | |
String destName = dataDir + "gauss_wiener_out.gif"; | |
try (Image image = Image.load(sourceFile); | |
RasterImage rasterImage = (RasterImage) image) { | |
if (rasterImage == null) { | |
return; | |
} | |
// Create an instance of GaussWienerFilterOptions class and set the radius size and smooth value. | |
GaussWienerFilterOptions options = new GaussWienerFilterOptions(12, 3); | |
options.setGrayscale(true); | |
// Apply MedianFilterOptions filter to RasterImage object and Save the resultant image | |
rasterImage.filter(image.getBounds(), options); | |
image.save(destName, new GifOptions()); | |
} |
Áp dụng Bộ lọc Gauss Wiener Cho Hình ảnh màu
Aspose.PSD cung cấp lớp GaussWienerFilterOptions cho các hình ảnh màu. Đoạn mã dưới đây mô tả cách áp dụng bộ lọc Gauss Wiener lên một hình ảnh màu.
String dataDir = Utils.getDataDir(ApplyGausWienerFiltersForColorImage.class) + "Conversion/"; | |
String sourceFile = dataDir + "sample.psd"; | |
String destName = dataDir + "gauss_wiener_color_out.gif"; | |
try (Image image = Image.load(sourceFile); | |
// Cast the image into RasterImage | |
RasterImage rasterImage = (RasterImage) image) { | |
if (rasterImage == null) { | |
return; | |
} | |
// Create an instance of GaussWienerFilterOptions class and set the radius size and smooth value. | |
GaussWienerFilterOptions options = new GaussWienerFilterOptions(5, 1.5); | |
options.setBrightness(1); | |
// Apply MedianFilterOptions filter to RasterImage object and Save the resultant image | |
rasterImage.filter(image.getBounds(), options); | |
image.save(destName, new GifOptions()); | |
} |
Áp dụng Bộ lọc Wiener Chuyển động
Aspose.PSD cung cấp lớp MotionWienerFilterOptions để áp dụng bộ lọc trên một RasterImage. Đoạn mã dưới đây mô tả cách áp dụng bộ lọc Wiener chuyển động lên một hình ảnh raster.
String dataDir = Utils.getDataDir(ApplyMotionWienerFilters.class) + "Conversion/"; | |
String sourceFile = dataDir + "sample.psd"; | |
String destName = dataDir + "motion_filter_out.gif"; | |
try (Image image = Image.load(sourceFile); | |
// Cast the image into RasterImage | |
RasterImage rasterImage = (RasterImage) image) { | |
if (rasterImage == null) { | |
return; | |
} | |
// Create an instance of MotionWienerFilterOptions class and set the length, smooth value and angle. | |
MotionWienerFilterOptions options = new MotionWienerFilterOptions(50, 9, 90); | |
options.setGrayscale(true); | |
// Apply MedianFilterOptions filter to RasterImage object and Save the resultant image | |
rasterImage.filter(image.getBounds(), options); | |
image.save(destName, new GifOptions()); | |
} |
Áp dụng Bộ lọc Sửa Chữa Trên Một Hình Ảnh
Bài viết này demo việc sử dụng Aspose.PSD cho Java để thực hiện bộ lọc sửa chữa trên một hình ảnh. API Aspose.PSD đã tiết lộ các phương thức hiệu quả và dễ sử dụng để đạt được mục tiêu này. Aspose.PSD cho Java đã tiết lộ các lớp BilateralSmoothingFilterOptions và SharpenFilterOptions để lọc. Lớp BilateralSmoothingFilterOptions cần một số nguyên làm kích thước. Các bước để thực hiện Thay đổi kích cỡ đơn giản như sau:
- Tải một hình ảnh bằng phương thức Factory Load được tiết lộ bởi lớp Image.
- Chuyển đổi hình ảnh thành RasterImage.
- Tạo một instances của lớp BilateralSmoothingFilterOptions và lớp SharpenFilterOptions.
- Gọi phương thức RasterImage.Filter trong khi chỉ định hình chữ nhật là biên giới hình ảnh và instance của lớp BilateralSmoothingFilterOptions.
- Gọi phương thức RasterImage.Filter trong khi chỉ định hình chữ nhật là biên giới hình ảnh và instance của lớp SharpenFilterOptions.
- Điều chỉnh độ tương phản
- Đặt độ sáng
- Lưu kết quả.
Đoạn mã dưới đây sẽ hướng dẫn bạn cách áp dụng bộ lọc sửa chữa.
try (Image image = Image.load(dataDir + "sample.psd"); | |
// Convert the image into RasterImage. | |
RasterImage rasterImage = (RasterImage) image) { | |
if (rasterImage == null) { | |
return; | |
} | |
// Get Bounds[rectangle] of image. | |
Rectangle rect = image.getBounds(); | |
// Create an instance of BilateralSmoothingFilterOptions class with size | |
// parameter. | |
BilateralSmoothingFilterOptions bilateralOptions = new BilateralSmoothingFilterOptions(3); | |
// Create an instance of SharpenFilterOptions class. | |
SharpenFilterOptions sharpenOptions = new SharpenFilterOptions(); | |
// Supply the filters to raster image. | |
rasterImage.filter(rect, bilateralOptions); | |
rasterImage.filter(rect, sharpenOptions); | |
// Adjust the contrast accordingly. | |
rasterImage.adjustContrast(-10); | |
// Set brightness using Binarize Bradley | |
rasterImage.binarizeBradley(80); | |
// Save the results to output path. | |
rasterImage.save(dataDir + "a1_out.jpg"); | |
} |
Sử dụng thuật toán ngưỡng Bradley
Ngưỡng hình ảnh được sử dụng trong các ứng dụng đồ họa. Mục tiêu của việc ngưỡng một hình ảnh là phân loại pixel là “tối” hoặc “sáng”. Aspose.PSD API cho phép bạn sử dụng ngưỡng Bradley khi chuyển đổi hình ảnh. Đoạn mã dưới đây sẽ hướng dẫn bạn cách xác định giá trị ngưỡng và sau đó gọi thuật toán ngưỡng của Bradley.
String dataDir = Utils.getDataDir(Bradleythreshold.class) + "Conversion/"; | |
String sourceFile = dataDir + "sample.psd"; | |
String destName = dataDir + "binarized_out.png"; | |
// Load an image | |
try (PsdImage image = (PsdImage) Image.load(sourceFile)) { | |
// Define threshold value, Call BinarizeBradley method and pass the threshold value as parameter and Save the output image | |
double threshold = 0.15; | |
image.binarizeBradley(threshold); | |
image.save(destName, new PngOptions()); | |
} |