Java でのカーネル画像処理プロジェクト [終了]
温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
I have a project made by ChatGPT and I wanted to ask , can someone explain me the parallelization, threads division between processors , synchronization mechanisms and also the methods that have been used in the code?
The project is about Kernel Image Processing
It also have distributed and sequential part too.
Thanks a lot.
package org.example;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ParallelKernelImageProcessing {
private static final int NUM_THREADS = Runtime.getRuntime().availableProcessors();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the path to the image:");
String imagePath = scanner.nextLine();
File imageFile = new File(imagePath);
try {
BufferedImage originalImage = ImageIO.read(imageFile);
long startTime = System.currentTimeMillis();
BufferedImage resultImage = applyParallelKernel(originalImage, getDefaultKernel());
long endTime = System.currentTimeMillis();
// Save the result image with a filename including "Paralleled"
String outputFileName = getOutputFileName(imageFile.getName());
File outputImageFile = new File(outputFileName);
ImageIO.write(resultImage, "jpg", outputImageFile);
long runtime = endTime - startTime;
System.out.println("Runtime: " + runtime + " milliseconds");
} catch (IOException e) {
e.printStackTrace();
} finally {
scanner.close();
}
}
private static float[][] getDefaultKernel() {
// Replace this with your desired kernel
return new float[][]{
{-1, -1, -1},
{-1, 8, -1},
{-1, -1, -1}
};
}
private static BufferedImage applyParallelKernel(BufferedImage inputImage, float[][] kernel) {
int width = inputImage.getWidth();
int height = inputImage.getHeight();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
try {
for (int i = 0; i < NUM_THREADS; i++) {
final int startRow = i * height / NUM_THREADS;
final int endRow = (i + 1) * height / NUM_THREADS;
executorService.submit(() -> processImageChunk(inputImage, outputImage, kernel, startRow, endRow));
}
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return outputImage;
}
private static void processImageChunk(BufferedImage inputImage, BufferedImage outputImage,
float[][] kernel, int startRow, int endRow) {
int width = inputImage.getWidth();
for (int y = startRow + 1; y < endRow - 1; y++) {
for (int x = 1; x < width - 1; x++) {
float sum = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int rgb = inputImage.getRGB(x + j, y + i);
float pixelValue = (rgb & 0xFF) * kernel[i + 1][j + 1];
sum += pixelValue;
}
}
int resultPixel = Math.min(255, Math.max(0, Math.round(sum)));
outputImage.setRGB(x, y, (resultPixel << 16) | (resultPixel << 8) | resultPixel);
}
}
}
private static String getOutputFileName(String originalFileName) {
int dotIndex = originalFileName.lastIndexOf('.');
if (dotIndex != -1) {
return originalFileName.substring(0, dotIndex) + "ParalleledKerneled.jpg";
} else {
return originalFileName + "ParalleledKerneled.jpg";
}
}
}
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2474
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
-
浏览(193) 评论(0)