我有一个应用程序会对位图产生失真。我正在尝试进一步优化图像处理类。基本上我的应用程序将位图传递给filters类中的一个名为barrel()的方法。 Barrel调用sampleImage(),然后调用getArgb()。有人告诉我,getArgb()可以是线程化的,并且可以在操作像素数据的同时运行。我不知道如何做到这一点,因为我只能像桶方法迭代中的循环一样快地获得像素数据。 getARGB()调用Bitmap.getPixel,这意味着在传递给桶方法的位图中有大约20000个方法调用。有没有办法可以进一步推动这个课程?先谢谢马特。
class Filters{
private float xscale;
private float yscale;
private float xshift;
private float yshift;
private int [] s;
private int [] scalar;
private int [] s1;
private int [] s2;
private int [] s3;
private int [] s4;
private String TAG = "Filters";
public Filters(){
s = new int[4];
scalar = new int[4];
s1 = new int[4];
s2 = new int[4];
s3 = new int[4];
s4 = new int[4];
}
public Bitmap barrel (Bitmap input, float k){
float centerX=input.getWidth()/2; //center of distortion
float centerY=input.getHeight()/2;
int width = input.getWidth(); //image bounds
int height = input.getHeight();
xshift = calc_shift(0,centerX-1,centerX,k);
float newcenterX = width-centerX;
float xshift_2 = calc_shift(0,newcenterX-1,newcenterX,k);
yshift = calc_shift(0,centerY-1,centerY,k);
float newcenterY = height-centerY;
float yshift_2 = calc_shift(0,newcenterY-1,newcenterY,k);
xscale = (width-xshift-xshift_2)/width;
yscale = (height-yshift-yshift_2)/height;
int origPixel = 0;
int []arr = new int[input.getWidth()*input.getHeight()];
int color = 0;
int p = 0;
int i = 0;
long startLoop = System.currentTimeMillis();
for(int j=0;j<input.getHeight();j++){
for( i=0;i<input.getWidth();i++,p++){
origPixel= input.getPixel(i,j);
float x = getRadialX((float)j,(float)i,centerX,centerY,k);
float y = getRadialY((float)j,(float)i,centerX,centerY,k);
sampleImage(input,x,y);
color = ((s[1]&0x0ff)<<16)|((s[2]&0x0ff)<<8)|(s[3]&0x0ff);
if(((i-centerX)*(i-centerX) + (j-centerY)*(j-centerY)) <= 5500){
arr[p]=color;
}else{
arr[p]=origPixel;
}
}
}
Bitmap dst2 = Bitmap.createBitmap(arr,width,height,input.getConfig());
return dst2;
}// end of barrel()
void sampleImage(Bitmap arr, float idx0, float idx1)
{
if(idx0<0 || idx1<0 || idx0>(arr.getHeight()-1) || idx1>(arr.getWidth()-1)){
s[0]=0;
s[1]=0;
s[2]=0;
s[3]=0;
return;
}
float idx0_fl=(float) Math.floor(idx0);
float idx0_cl=(float) Math.ceil(idx0);
float idx1_fl=(float) Math.floor(idx1);
float idx1_cl=(float) Math.ceil(idx1);
s1 = getARGB(arr,(int)idx0_fl,(int)idx1_fl);
s2 = getARGB(arr,(int)idx0_fl,(int)idx1_cl);
s3 = getARGB(arr,(int)idx0_cl,(int)idx1_cl);
s4 = getARGB(arr,(int)idx0_cl,(int)idx1_fl);
float x = idx0 - idx0_fl;
float y = idx1 - idx1_fl;
// s[0]= (int) (s1[0]*(1-x)*(1-y) + s2[0]*(1-x)*y + s3[0]*x*y + s4[0]*x*(1-y));
s[1]= (int) (s1[1]*(1-x)*(1-y) + s2[1]*(1-x)*y + s3[1]*x*y + s4[1]*x*(1-y));
s[2]= (int) (s1[2]*(1-x)*(1-y) + s2[2]*(1-x)*y + s3[2]*x*y + s4[2]*x*(1-y));
s[3]= (int) (s1[3]*(1-x)*(1-y) + s2[3]*(1-x)*y + s3[3]*x*y + s4[3]*x*(1-y));
}
int [] getARGB(Bitmap buf,int x, int y){
int rgb = buf.getPixel(y, x); // Returns by default ARGB.
// scalar[0] = (rgb >>> 24) & 0xFF;
scalar[1] = (rgb >>> 16) & 0xFF;
scalar[2] = (rgb >>> 8) & 0xFF;
scalar[3] = (rgb >>> 0) & 0xFF;
return scalar;
}
float getRadialX(float x,float y,float cx,float cy,float k){
x = (x*xscale+xshift);
y = (y*yscale+yshift);
float res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
return res;
}
float getRadialY(float x,float y,float cx,float cy,float k){
x = (x*xscale+xshift);
y = (y*yscale+yshift);
float res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
return res;
}
float thresh = 1;
float calc_shift(float x1,float x2,float cx,float k){
float x3 = (float)(x1+(x2-x1)*0.5);
float res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx)));
float res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx)));
if(res1>-thresh && res1 < thresh)
return x1;
if(res3<0){
return calc_shift(x3,x2,cx,k);
}
else{
return calc_shift(x1,x3,cx,k);
}
}
}// end of filters class
[更新]
我在这里是否正确。我似乎在第31行获得了一个NPE,这是task1返回partialResult的地方。我不认为partialProcessing.call方法正确返回partialResult。我只得到2个空的黑色位图,其中应该是失真,因此可能无法填充数组。你有什么想法吗?感谢。
public class MultiProcessorFilter {
public Bitmap barrel (Bitmap input, float k){
int []arr = new int[input.getWidth()*input.getHeight()];
// replace the j, i for loops:
int jMax = input.getHeight();
int jMid = jMax / 2;
int iMax = input.getWidth();
int iMid = iMax / 2;
ExecutorService threadPool = Executors.newFixedThreadPool(2);
FutureTask<PartialResult> task1 = (FutureTask<PartialResult>) threadPool.submit(new PartialProcessing(0, jMid - 1, input, k, iMid, iMax));
FutureTask<PartialResult> task2 = (FutureTask<PartialResult>) threadPool.submit(new PartialProcessing(jMid, jMax - 1,input, k, iMid, iMax));
try{
PartialResult result1 = task1.get(); // blocks until the thread returns the result
result1.fill(arr);
PartialResult result2 = task2.get(); // blocks until the thread returns the result
result2.fill(arr);
}catch(Exception e){
e.printStackTrace();
}
Bitmap dst2 = Bitmap.createBitmap(arr,input.getWidth(),input.getHeight(),input.getConfig());
return dst2;
}
public class PartialResult {
int startP;
int endP;
int[] storedValues;
public PartialResult(int startp, int endp){
this.startP = startp;
this.endP = endp;
}
public void addValue(int p, int result) {
storedValues[p] = result;
}
public void fill(int[] arr) {
for (int p = startP; p < endP; p++)
arr[p] = storedValues[p];
}
}
public class PartialProcessing implements Callable<PartialResult> {
int startJ;
int endJ;
// ... other members needed for the computation such as
private int[] scalar;
private float xscale;
private float yscale;
private float xshift;
private float yshift;
private float thresh = 1;
private int [] s1;
private int [] s2;
private int [] s3;
private int [] s4;
private int [] s;
private Bitmap input;
private float k;
private int startI;
private int endI;
public PartialProcessing(int startj, int endj, Bitmap input, float k, int starti, int endi) {
this.startJ = startj;
this.endJ = endj;
this.input = input;
this.k = k;
this.startI = starti;
this.endI = endi;
}
int [] getARGB(Bitmap buf,int x, int y){
int rgb = buf.getPixel(y, x); // Returns by default ARGB.
// int [] scalar = new int[4];
// scalar[0] = (rgb >>> 24) & 0xFF;
scalar[1] = (rgb >>> 16) & 0xFF;
scalar[2] = (rgb >>> 8) & 0xFF;
scalar[3] = (rgb >>> 0) & 0xFF;
return scalar;
}
//... add other methods needed for the computation that where in class Filters
float getRadialX(float x,float y,float cx,float cy,float k){
x = (x*xscale+xshift);
y = (y*yscale+yshift);
float res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
return res;
}
float getRadialY(float x,float y,float cx,float cy,float k){
x = (x*xscale+xshift);
y = (y*yscale+yshift);
float res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
return res;
}
float calc_shift(float x1,float x2,float cx,float k){
float x3 = (float)(x1+(x2-x1)*0.5);
float res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx)));
float res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx)));
if(res1>-thresh && res1 < thresh)
return x1;
if(res3<0){
return calc_shift(x3,x2,cx,k);
}
else{
return calc_shift(x1,x3,cx,k);
}
}
void sampleImage(Bitmap arr, float idx0, float idx1)
{
// s = new int [4];
if(idx0<0 || idx1<0 || idx0>(arr.getHeight()-1) || idx1>(arr.getWidth()-1)){
s[0]=0;
s[1]=0;
s[2]=0;
s[3]=0;
return;
}
float idx0_fl=(float) Math.floor(idx0);
float idx0_cl=(float) Math.ceil(idx0);
float idx1_fl=(float) Math.floor(idx1);
float idx1_cl=(float) Math.ceil(idx1);
s1 = getARGB(arr,(int)idx0_fl,(int)idx1_fl);
s2 = getARGB(arr,(int)idx0_fl,(int)idx1_cl);
s3 = getARGB(arr,(int)idx0_cl,(int)idx1_cl);
s4 = getARGB(arr,(int)idx0_cl,(int)idx1_fl);
float x = idx0 - idx0_fl;
float y = idx1 - idx1_fl;
// s[0]= (int) (s1[0]*(1-x)*(1-y) + s2[0]*(1-x)*y + s3[0]*x*y + s4[0]*x*(1-y));
s[1]= (int) (s1[1]*(1-x)*(1-y) + s2[1]*(1-x)*y + s3[1]*x*y + s4[1]*x*(1-y));
s[2]= (int) (s1[2]*(1-x)*(1-y) + s2[2]*(1-x)*y + s3[2]*x*y + s4[2]*x*(1-y));
s[3]= (int) (s1[3]*(1-x)*(1-y) + s2[3]*(1-x)*y + s3[3]*x*y + s4[3]*x*(1-y));
}
// this will be called on some new thread
@Override public PartialResult call() {
PartialResult partialResult = new PartialResult(startJ, endJ);
float centerX=input.getWidth()/2; //center of distortion
float centerY=input.getHeight()/2;
int width = input.getWidth(); //image bounds
int height = input.getHeight();
xshift = calc_shift(0,centerX-1,centerX,k);
float newcenterX = width-centerX;
float xshift_2 = calc_shift(0,newcenterX-1,newcenterX,k);
yshift = calc_shift(0,centerY-1,centerY,k);
float newcenterY = height-centerY;
float yshift_2 = calc_shift(0,newcenterY-1,newcenterY,k);
xscale = (width-xshift-xshift_2)/width;
yscale = (height-yshift-yshift_2)/height;
int p = startI; // not 0! at the start since we don't start at j = 0
int origPixel = 0;
int color = 0;
for (int j = startJ; j < endJ; j++){
for (int i = startI; i < endI; i++, p++){
//... copy the rest of the code
origPixel= input.getPixel(i,j);
float x = getRadialX((float)j,(float)i,centerX,centerY,k);
float y = getRadialY((float)j,(float)i,centerX,centerY,k);
sampleImage(input,x,y);
color = ((s[1]&0x0ff)<<16)|((s[2]&0x0ff)<<8)|(s[3]&0x0ff);
if(((i-centerX)*(i-centerX) + (j-centerY)*(j-centerY)) <= 5500){
//arr[p]=color;
partialResult.addValue(p, color);
}else{
//arr[p]=origPixel;
partialResult.addValue(p, origPixel);
}
}
// partialResult.addValue(p, color);
}
return partialResult;
}
}
}//end of MultiProcesorFilter
07-25 16:35:20.552: WARN/System.err(10253): java.util.concurrent.ExecutionException: java.lang.NullPointerException
07-25 16:35:20.552: WARN/System.err(10253): at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:223)
07-25 16:35:20.552: WARN/System.err(10253): at java.util.concurrent.FutureTask.get(FutureTask.java:82)
07-25 16:35:20.557: WARN/System.err(10253): at com.tecmark.MultiProcessorFilter.barrel(MultiProcessorFilter.java:31)
07-25 16:35:20.557: WARN/System.err(10253): at com.tecmark.TouchView$2.run(TouchView.java:147)
07-25 16:35:20.557: WARN/System.err(10253): at java.lang.Thread.run(Thread.java:1096)
07-25 16:35:20.557: WARN/System.err(10253): Caused by: java.lang.NullPointerException
07-25 16:35:20.562: WARN/System.err(10253): at com.tecmark.MultiProcessorFilter$PartialProcessing.sampleImage(MultiProcessorFilter.java:160)
07-25 16:35:20.562: WARN/System.err(10253): at com.tecmark.MultiProcessorFilter$PartialProcessing.call(MultiProcessorFilter.java:235)
07-25 16:35:20.562: WARN/System.err(10253): at com.tecmark.MultiProcessorFilter$PartialProcessing.call(MultiProcessorFilter.java:1)
07-25 16:35:20.562: WARN/System.err(10253): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-25 16:35:20.567: WARN/System.err(10253): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-25 16:35:20.567: WARN/System.err(10253): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-25 16:35:20.567: WARN/System.err(10253): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-25 16:35:20.567: WARN/System.err(10253): ... 1 more
07-25 16:35:20.572: WARN/System.err(10253): java.util.concurrent.ExecutionException: java.lang.NullPointerException
07-25 16:35:20.577: WARN/System.err(10253): at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:223)
07-25 16:35:20.577: WARN/System.err(10253): at java.util.concurrent.FutureTask.get(FutureTask.java:82)
07-25 16:35:20.577: WARN/System.err(10253): at com.tecmark.MultiProcessorFilter.barrel(MultiProcessorFilter.java:31)
07-25 16:35:20.577: WARN/System.err(10253): at com.tecmark.TouchView$2.run(TouchView.java:148)
07-25 16:35:20.577: WARN/System.err(10253): at java.lang.Thread.run(Thread.java:1096)
07-25 16:35:20.582: WARN/System.err(10253): Caused by: java.lang.NullPointerException
07-25 16:35:20.582: WARN/System.err(10253): at com.tecmark.MultiProcessorFilter$PartialProcessing.sampleImage(MultiProcessorFilter.java:160)
07-25 16:35:20.582: WARN/System.err(10253): at com.tecmark.MultiProcessorFilter$PartialProcessing.call(MultiProcessorFilter.java:235)
07-25 16:35:20.582: WARN/System.err(10253): at com.tecmark.MultiProcessorFilter$PartialProcessing.call(MultiProcessorFilter.java:1)
07-25 16:35:20.587: WARN/System.err(10253): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-25 16:35:20.587: WARN/System.err(10253): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-25 16:35:20.587: WARN/System.err(10253): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-25 16:35:20.587: WARN/System.err(10253): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-25 16:35:20.587: WARN/System.err(10253): ... 1 more
答案 0 :(得分:1)
当您想要并行化代码时,必须查找for
- 循环:
for(int j=0;j < input.getHeight();j++){
for( i=0;i < input.getWidth();i++,p++){
您应该在各个线程之间划分j
的某些范围。
我没有仔细阅读你在做什么,但事实并非如此
如果计算需要在某些j
上完成计算,则工作
更高j
的。
修改强>:
一些部分代码:
public Bitmap barrel (Bitmap input, float k){
...
// replace the j, i for loops:
int jMax = input.getHeight();
ExecutorService threadPool = Executors.newFixedThreadPool(2);
int jMid = jMax / 2;
FutureTask<PartialResult> task1 = treadPool.submit(new PartialProcessing(0, jMid - 1, ...));
FutureTask<PartialResult> task2 = treadPool.submit(new PartialProcessing(jMid, jMax - 1(?) ...));
PartialResult result1 = task1.get(); // blocks until the thread returns the result
results1.fill(arr);
PartialResult result2 = task2.get(); // blocks until the thread returns the result
results2.fill(arr);
}
,其中
public class PartialResult {
int startP;
int endP;
int[] storedValues;
... constructor
public addValue(int p, int result) {
storedValues[p +/- some offset] = result;
}
public void fill(int[] arr) {
for (int p = startP; p < endP; p++)
arr[p] = storedValues[p +/- some offset];
}
}
}
和
public class PartialProcessing implements Callable<PartialResult> {
int startJ;
int endJ;
... other members needed for the computation such as
int[] scalar;
...
public PartialProcessing(int startJ, int endJ, ... others needed) {
...
}
int [] getARGB(Bitmap buf,int x, int y){
...
}
... add other methods needed for the computation that where in class Filters
// this will be called on some new thread
@Override public PartialResult call() {
PartialResult partialResult = new PartialResult(startJ, ...);
p = ...; // not 0! at the start since we don't start at j = 0
for (int j = startJ; j < (???<=) endJ; j++)
for (int i... p++)
... copy the rest of the code
partialResult.addResult(p, result);
}
}
顺便说一句,由于同步问题,您无法在arr[p]
方法中直接填充call
:一个核心上的数组arr
的副本不一定具有相同的值另一个核心的副本。
EDIT :由于大多数处理器都有2个内核,因此使用2的倍数代替3个线程会更有意义。因为许多即将推出的ARM处理器有4核。