0

有人可以为我提供一些关于如何将此代码移植到渲染脚本以获得更好性能的指南。

private void someMethod() {
 for (int i = 0; i < src.rows(); i++) {
  for (int j = 0; j < src.cols(); j++) {
   double hsv[] = src.get(i, j);
   double modifedHSV[] = this.modifyHSV(new Scalar(hsv), selectedRepaintColor, mean);
   res.put(i, j, modifedHSV);
  }
 }
}
private double[] modifyHSV(Scalar hsvImage, Scalar selectedHsv, Scalar mean) {

 Double h_final = hsvImage.val[0] - mean.val[0] + selectedHsv.val[0];
 Double s_final = hsvImage.val[1] - mean.val[1] + selectedHsv.val[1];
 Double v_final = hsvImage.val[2] - mean.val[2] + selectedHsv.val[2];

 h_final = (h_final <= 0) ? h_final + 180 : h_final;
 s_final = (s_final <= 0) ? 0 : s_final;
 v_final = (v_final <= 0) ? 0 : v_final;

 double[] final_hsv = new double[3];
 final_hsv[0] = h_final;
 final_hsv[1] = s_final;
 final_hsv[2] = v_final;
 return final_hsv;
}
4

1 回答 1

1

你可以尝试这样的事情:

hsv.rs:

#pragma rs_fp_relaxed
float3 mean;
float3 selectedHsv;
float3 RS_KERNEL process_hsv(float3 input) {
    float3 hsv_final = input - mean + selectedHsv;

    hsv_final.x = (hsv_final.x <= 0.f) ? hsv_final.x + 180.f : hsv_final.x;
    hsv_final.y = (hsv_final.y <= 0.f) ? 0.f : hsv_final.y;
    hsv_final.z = (hsv_final.z <= 0.f) ? 0.f : hsv_final.z;

    return hsv_final;
}

爪哇:

ScriptC_hsv script;
void init() {
    script = new ScriptC_hsv(rs);
}
float[] process_hsv(float[] input_array, Float3 selectedHsv, Float3 mean) {
    script.set_mean(mean);
    script.set_selectedHsv(selectedHsv);
    output_array = new float[size];
    Type t = Type.createXY(rs, Element.F32_3(rs), columns, rows);
    Allocation input = Allocation.createTyped(rs, t, size, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, t, size, Allocation.USAGE_SCRIPT);
    input.copyFrom(input_array);
    script.forEach_process_hsv(input, output);
    output.copyTo(output_array);
    return output_array;
}
于 2017-07-13T21:48:52.620 回答