gpt4 book ai didi

java - 灰度图像算法在多线程时比顺序慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:44:17 26 4
gpt4 key购买 nike

对于学校项目,我们需要对图像处理算法进行多线程处理。我决定将图像的高度除以线程数。每个线程循环遍历高度和宽度并更改像素的颜色。奇怪的是,顺序版本总是快得多。我做错了什么?

线程执行的方法:

public synchronized void applyGrayScale(int numberOfThreads) {
int heightPerThread = imageHeight / numberOfThreads;
//Set the thread counter
int threadCounter = this.getCount();
this.setCount(count + 1);

/*The height per thread is calculated by the number of threads. We first start at 0. For the next thread we start at heightPerThread * [current thread number]
So for example; first thread runs from 0 to 80 pixels. The second thread runs from 81 to 160 pixels.
*/
for (int j = ((heightPerThread - 2) * threadCounter); j < (heightPerThread * (threadCounter + 1) - 1); j++) {
for (int i = 0; i < imageInput.getWidth() - 1; i++) {
//Get the RGB value and set it to grayscale
int rgb;
int p = RGB.getRGBW(imageInput, i, j);
rgb = (int) ((((p >> 16) & 0xFF) * 0.2125) + (((p >> 8) & 0xFF) * 0.7154) + ((p & 0xFF) * 0.0721));
rgb = (rgb << 16) | (rgb << 8) | (rgb);
//Set the new RGB value per pixel
imageOutput.setRGB(i, j, rgb);
}
}
}

运行程序的代码:

   int threadsAmount = 5;
final Thread[] threads = new Thread[threadsAmount];

BufferedImage image = null;
try {
image = ImageIO.read(new File("C:/Cat03.jpg"));
} catch (IOException e) {
e.printStackTrace();
}

//Define the starting time
long start = System.currentTimeMillis();

//Create a new grayscale object and set the image
final GrayscaleParallel grayscaleParallel = new GrayscaleParallel(image);

//Thread to apply the grayscale with the number of threads
class grayScaleThread extends Thread {
@Override
public void run() {
grayscaleParallel.applyGrayScale(threadsAmount);
}
}

//Start all threads
for (int i = 0; i < threadsAmount; i++) {
threads[i] = new grayScaleThread();
threads[i].start();
}

//Wait for all threads to finish
for (int i = 0; i < threadsAmount; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//save result to file
grayscaleParallel.createImage();

//Define how long it took
long end = System.currentTimeMillis();
float sec = (end - start) / 1000F;
System.out.println(sec + " seconds parallel");

输出是:0.897 秒平行0.798秒连续

顺序算法:

 for (int j = 0; j < _image.getHeight(); j++) {
for (int i = 0; i < _image.getWidth(); i++) {
int rgb;
int p = RGB.getRGBW(_image, i, j);

rgb = (int) ((((p >> 16) & 0xFF) * 0.2125) + (((p >> 8) & 0xFF) * 0.7154) + ((p & 0xFF) * 0.0721));
rgb = (rgb << 16) | (rgb << 8) | (rgb);

imageOutput.setRGB(i, j, rgb);
}
}
return imageOutput;

当我使用非常大的图像时,并行时间似乎比顺序图像缩短了 0.5 秒,但当我不保存结果时,并行算法再次变慢。

最佳答案

问题是您的applyGrayScale() 方法是同步 - 只有一个线程可以同时执行它,因为所有线程都在同一个对象上运行它。您的代码中没有可以并行运行的部分。所以基本上这个过程与顺序变体中的过程或多或少相同,但是您为上下文切换和跟踪哪个线程进入该方法添加了一些额外的开销。

相反,您必须事先拆分图像 - 在创建线程时“告诉”他们应该修改哪个部分。然后将方法从 synchronized 更改为 normal,让它们并行完成它们的工作。

关于java - 灰度图像算法在多线程时比顺序慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56593764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com