gpt4 book ai didi

java - 多线程java图像阈值处理

转载 作者:行者123 更新时间:2023-11-30 08:08:50 24 4
gpt4 key购买 nike

我有一个学校项目,我们应该使用并行编程来使某些算法运行得更快。例如,我选择了图像阈值。

所以我创建了Java程序,它正常执行(加载图像,循环所有像素,计算阈值,再次循环所有像素并设置黑色或白色,如果它大于或小于阈值)。在我的笔记本上,处理大约 4000x3000 的图片大约需要 5 秒,处理 11500x11500 的图像大约需要 49 秒。

然后我创建了另一个程序,它应该使用线程,以使它们循环更快地完成。

现在我正在创建 4 个线程,每个线程处理 1/4 的图像。首先,他们将阈值添加到同步数组列表中,所有这些都完成后,我计算阈值。然后我创建另外 4 个线程,它们再次处理每个图像的 1/4,并将图像设置为黑色或白色。

这花了我 12 秒来处理 4000x3000 的图像,并抛出 java.lang.OutOfMemoryError: Java 堆空间(在所有线程中)和 11500x11500 的图像。

 public class PprPrahovaniParalelne{

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
final Threshold image = new Threshold(nactiObrazek("ryba.jpg"));


final int width = image.getImage().getWidth();
final int height = image.getImage().getHeight();


Thread t1 = new Thread(){
int threshold;
public void run(){
System.out.println("Thread 1 - Started");
for(int y = 0; y < height/4;y++){
for(int x = 0; x < width;x++){
Color color = new Color(image.getImage().getRGB(x,y));
threshold = (color.getRed()+color.getGreen()+color.getBlue())/3;
image.addThreshold(threshold);
}
}
System.out.println("Thread 1 - finished");
}
};

Thread t2 = new Thread(){
int threshold;
@Override
public void run(){
for(int y = height/4; y < height/4*2;y++){
for(int x = 0; x < width;x++){
Color barva = new Color(image.getImage().getRGB(x,y));
threshold = (barva.getRed()+barva.getGreen()+barva.getBlue())/3;
image.addThreshold(threshold);
}
}
System.out.println("Thread 2 - finished");
}
};

Thread t3 = new Thread(){
int threshold;
@Override
public void run(){
for(int y = height/4*2; y < height/4*3;y++){
for(int x = 0; x < width;x++){
Color barva = new Color(image.getImage().getRGB(x,y));
threshold = (barva.getRed()+barva.getGreen()+barva.getBlue())/3;
image.addThreshold(threshold);
}
}
System.out.println("Thread 3 - finished");
}
};

Thread t4 = new Thread(){
int threshold;
@Override
public void run(){
for(int y = height/4*3; y < height;y++){
for(int x = 0; x < width;x++){
Color barva = new Color(image.getImage().getRGB(x,y));
threshold = (barva.getRed()+barva.getGreen()+barva.getBlue())/3;
image.addThreshold(threshold);
}
}
System.out.println("Thread 4 - finished");
}
};

t1.start();
t2.start();
t4.start();
t3.start();

try{
t1.join();
t2.join();
t3.join();
t4.join();
}catch(InterruptedException e){
e.printStackTrace();
}


image.countThreshold();
System.out.println("Threshold je: " + image.getThreshold());

Thread t5 = new Thread(){
Color cerna = new Color(255,255,255);
Color bila = new Color(0,0,0);
int threshold;
@Override
public void run(){
for(int y = 0; y < height/4;y++){
for(int x = 0; x < width;x++){
Color barva = new Color(image.getImage().getRGB(x,y));
threshold = (barva.getRed()+barva.getGreen()+barva.getBlue())/3;
if(threshold > image.getThreshold()){
image.getImage().setRGB(x, y, cerna.getRGB());
}else{
image.getImage().setRGB(x, y, bila.getRGB());
}
}
}
System.out.println("Thread 5 - finished");
}
};

Thread t6 = new Thread(){
Color cerna = new Color(255,255,255);
Color bila = new Color(0,0,0);
int threshold;
@Override
public void run(){
for(int y = height/4; y < height/4*2;y++){
for(int x = 0; x < width;x++){
Color color = new Color(image.getImage().getRGB(x,y));
threshold = (color.getRed()+color.getGreen()+color.getBlue())/3;
if(threshold > image.getThreshold()){
image.getImage().setRGB(x, y, cerna.getRGB());
}else{
image.getImage().setRGB(x, y, bila.getRGB());
}
}
}
System.out.println("Thread 6 - finished");
}
};

Thread t7 = new Thread(){
Color cerna = new Color(255,255,255);
Color bila = new Color(0,0,0);
int threshold;
@Override
public void run(){
for(int y = height/4*2; y < height/4*3;y++){
for(int x = 0; x < width;x++){
Color color = new Color(image.getImage().getRGB(x,y));
threshold = (color.getRed()+color.getGreen()+color.getBlue())/3;
if(threshold > image.getThreshold()){
image.getImage().setRGB(x, y, cerna.getRGB());
}else{
image.getImage().setRGB(x, y, bila.getRGB());
}
}
}
System.out.println("Thread 7 - finished");
}
};

Thread t8 = new Thread(){
Color cerna = new Color(255,255,255);
Color bila = new Color(0,0,0);
int threshold;
@Override
public void run(){
for(int y = height/4*3; y < height;y++){
for(int x = 0; x < width;x++){
Color barva = new Color(image.getImage().getRGB(x,y));
threshold = (barva.getRed()+barva.getGreen()+barva.getBlue())/3;
if(threshold > image.getThreshold()){
image.getImage().setRGB(x, y, cerna.getRGB());
}else{
image.getImage().setRGB(x, y, bila.getRGB());
}
}
}
System.out.println("Thread 8 - finished");
}
};

t5.start();
t6.start();
t7.start();
t8.start();

try{
t5.join();
t6.join();
t7.join();
t8.join();
}catch(InterruptedException e){
e.printStackTrace();
}

File hotovo = new File("ryba_prahovanej.jpg");
ImageIO.write(image.getImage(), "jpg", hotovo);

}


public static BufferedImage nactiObrazek(String nazev){
BufferedImage img = null;
try {
img = ImageIO.read(new File(nazev));
} catch (IOException e) {
}
return img;
}
}

和阈值类:

public class Threshold {
private BufferedImage image;
final private List<Integer> list;
private int threshold;

public int getThreshold() {
return threshold;
}

public List<Integer> getList(){
return list;
}

public Threshold(BufferedImage obrazek) {
this.list = Collections.synchronizedList(new ArrayList<Integer>());
this.image = obrazek;
}

public void setObrazek(BufferedImage obrazek){
this.image = obrazek;
}

public BufferedImage getImage(){
return this.image;
}

public void addThreshold(int threshold){
list.add(threshold);
}

public void countThreshold(){
long sum = 0;
for (Iterator<Integer> it = list.iterator(); it.hasNext();) {
int item = it.next();
sum += item;
}
this.threshold = (int) (sum/list.size());
}
}

那么为什么多线程时会变慢呢?除了列表之外,我不会在这里同步任何内容,因为线程不应该在像素数组中使用相同的索引。

此处的分析器图片:

系列: Serial

并行: Paralel

最佳答案

在这种并行化情况下,您需要考虑一些事情。

  1. 您的代码仅对图像进行异步处理(计算阈值并创建阈值图像),但您的图像 IO(写入)会被阻止,直到所有线程完成处理。
  2. 另一个更重要的因素是您是如何提出 4 线程解决方案的。您选择 4 个线程作为理想线程数的原因是什么?在像您这样的 CPU 和内存密集型多线程程序中,理想的线程数 = CPU 数量 + 1。拥有更多线程并不会使程序执行得更快,事实上反而会降低性能。
  3. 图像处理当然是内存密集型的,当运行带有大图像的程序时,您需要增加堆空间。

请考虑上述内容。

编辑

您可以首先使代码更具可读性并减少代码重复。您可以利用 CyclicBarrier 来实现并行任务的顺序执行。

import java.awt.image.*;
import java.io.*;
import java.awt.*;
import javax.imageio.*;
import java.util.concurrent.*;

public class PprPrahovaniParalelne {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
final Threshold image = new Threshold(nactiObrazek("DSC03691.jpg"));


final int width = image.getImage().getWidth();
final int height = image.getImage().getHeight();

final int nCpu = Runtime.getRuntime().availableProcessors() + 1;

ExecutorService threadPool = Executors.newFixedThreadPool(nCpu);

System.out.println("Number of CPUs : "+nCpu);

CyclicBarrier cyclicBarrier = new CyclicBarrier(4, new Runnable() {
private int count = 1;

public void run() {
if(count == 1) {
image.countThreshold();
System.out.println("Threshold je: " + image.getThreshold());

} else {
try {
File hotovo = new File("ryba_prahovanej.jpg");
ImageIO.write(image.getImage(), "jpg", hotovo);
} catch(IOException e) {
System.err.println("Error while writing : " + e);
}
threadPool.shutdownNow();
}
count++;
}
});

threadPool.submit(new ImageProcessingTask(0, height/4, width, image, cyclicBarrier));
threadPool.submit(new ImageProcessingTask(height/4, height/4*2, width, image, cyclicBarrier));
threadPool.submit(new ImageProcessingTask(height/4*2, height/4*3, width, image, cyclicBarrier));
threadPool.submit(new ImageProcessingTask(height/4*3, height, width, image, cyclicBarrier));

}


public static BufferedImage nactiObrazek(String nazev){
BufferedImage img = null;
try {
img = ImageIO.read(new File(nazev));
} catch (IOException e) {
}
return img;
}
}

class ImageProcessingTask implements Runnable {

private int start;
private int height;
private int width;
private Threshold image;
private CyclicBarrier barrier;

public ImageProcessingTask(int start, int height, int width, Threshold image, CyclicBarrier barrier) {
this.start = start;
this.height = height;
this.width = width;
this.image = image;
this.barrier = barrier;
}

public void run(){
int threshold;
System.out.println(Thread.currentThread().getName()+" - Started");
for(int y = start; y < height;y++){
for(int x = 0; x < width;x++){
Color color = new Color(image.getImage().getRGB(x,y));
threshold = (color.getRed()+color.getGreen()+color.getBlue())/3;
image.addThreshold(threshold);
}
}
try {
int count = barrier.await();
if(count == 0) {
barrier.reset();
System.out.println("Resetting Cyclic Barrier");
}
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
} catch(Exception e) {
e.printStackTrace();
}
Color cerna = new Color(255,255,255);
Color bila = new Color(0,0,0);
for(int y = start; y < height;y++){
for(int x = 0; x < width;x++){
Color barva = new Color(image.getImage().getRGB(x,y));
threshold = (barva.getRed()+barva.getGreen()+barva.getBlue())/3;
if(threshold > image.getThreshold()){
image.getImage().setRGB(x, y, cerna.getRGB());
}else{
image.getImage().setRGB(x, y, bila.getRGB());
}
}
}
try {
barrier.await();
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
} catch(Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" - finished");

}
}

关于java - 多线程java图像阈值处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30707643/

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