Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Filter implements Runnable {
- private final int from;
- private final int to;
- private Mat y;
- private Mat f;
- private static double []a = {
- 0.0404738053296999,
- -0.0643096992131437,
- -0.0139790480634063,
- 0.0715772831165316,
- -0.0274783657038324,
- -0.0634731190556242,
- 0.0191995380384644,
- 0.0236999687181153,
- -0.00222016637849384,
- 0.0113003823627795,
- -0.00113218936618811,
- 0.00938850762108079,
- -0.00562895831074348,
- -0.00481412192792152,
- 0.0217764167501000,
- -0.0352209221161028,
- 0.0278540627491177,
- -0.00973158992626677,
- -0.00830170632505028,
- 0.0140575282597967,
- -0.00560803411318567,
- 3.94522529384269e-05,
- 0.00226931186877385,
- 0.00411912796594011,
- -0.00671529251454103,
- 0.00566318057030169,
- -0.00276200445404060,
- 0.00129942023146980,
- -0.00316761663763157,
- 0.00403822958532445,
- -0.00395955782335479,
- 0.00174617650909325
- };
- private static final int K = 32;
- private static final int Mm = 16;
- private static final int Mp = 15;
- private static final int N = Mm + Mp + 1;
- private static final int PROCS = 4;
- private static CyclicBarrier barrier = new CyclicBarrier(PROCS);
- private static AtomicReference<Double> pixel = new AtomicReference<>(Double.valueOf(0));
- public Filter(Mat f, Mat y, int from, int to) {
- this.f = f;
- this.y = y;
- this.from = from;
- this.to = to;
- }
- @Override
- public void run() {
- final int links = to - from;
- for(int i = 0; i < f.rows(); i++) {
- double[][] yk = new double[links][f.cols()];
- for(int t = 0; t < links; t++) {
- for (int j = 0; j < f.cols(); j++) {
- yk[t][j] = f.get(i, j)[0];
- }
- }
- for (int j = Mm; j < f.cols() - Mp; j++) {
- double px = 0;
- for(int t = 0; t < links; t++) {
- final int jk = t + 1;
- if (jk == 0) {
- double f1 = f.get(i, j - Mm)[0] - f.get(i, j + Mp - 1)[0];
- yk[t][j] = yk[t][j - 1] + f1;
- } else if (jk % 2 == 0) {
- double f1 = f.get(i, j - Mm)[0] - f.get(i, j + Mp - 1)[0];
- double f12 = f.get(i, j - Mm - 1)[0] - f.get(i, j + Mp - 2)[0];
- double f3 = f1 - f12;
- yk[t][j] = 2 * Math.cos(Math.PI / N * (double)jk) * yk[t][j - 1] + yk[t][j - 2] + f3;
- } else {
- double f2 = f.get(i, j - Mm)[0] + f.get(i, j + Mp - 1)[0];
- double f22 = f.get(i, j - Mm - 1)[0] + f.get(i, j + Mp - 2)[0];
- double f4 = f2 - f22;
- yk[t][j] = 2 * Math.cos(Math.PI / N * (double)jk) * yk[t][j - 1] + yk[t][j - 2] + f4;
- }
- yk[t][j] *= a[t];
- px += yk[t][j];
- }
- pixel.accumulateAndGet(px, (x, y) -> x + y);
- try {
- barrier.await();
- if (from == 0) {
- y.put(i, j, pixel.get());
- pixel.set(Double.valueOf(0.0));
- }
- barrier.await();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
- // for(int j = 0; j < f.cols(); j++) {
- // double[][] yk = new double[links][f.rows()];
- //
- // for(int t = 0; t < links; t++) {
- // for (int i = 0; i < f.rows(); i++) {
- // yk[t][i] = f.get(i, j)[0];
- // }
- // }
- //
- // for (int i = Mm; i < f.rows() - Mp; i++) {
- //
- // double px = 0;
- //
- // for(int t = 0; t < links; t++) {
- // final int jk = t + 1;
- // if (jk == 0) {
- // double f1 = y.get(i - Mm, j)[0] - y.get(i - Mp - 1, j)[0];
- // yk[t][i] = yk[t][i - 1] + f1;
- // } else if (jk % 2 == 0) {
- // double f1 = y.get(i - Mm, j)[0] - y.get(i + Mp - 1, j)[0];
- // double f12 = y.get(i - Mm - 1, j)[0] - y.get(i + Mp - 2, j)[0];
- // double f3 = f1 - f12;
- // yk[t][i] = 2 * Math.cos(Math.PI / N * (double)jk) * yk[t][i - 1] + yk[t][i - 2] + f3;
- // } else {
- // double f2 = y.get(i - Mm, j)[0] + y.get(i + Mp - 1, j)[0];
- // double f22 = y.get(i - Mm - 1, j)[0] + y.get(i + Mp - 2, j)[0];
- // double f4 = f2 - f22;
- // yk[t][i] = 2 * Math.cos(Math.PI / N * (double)jk) * yk[t][i- 1] + yk[t][i - 2] + f4;
- // }
- //
- // yk[t][i] *= a[t];
- // px += yk[t][i];
- // }
- //
- // pixel.accumulateAndGet(px, (x, y) -> x + y);
- //
- // try {
- // barrier.await();
- //
- // if (from == 0) {
- // y.put(i, j, pixel.get());
- // pixel.set(Double.valueOf(0.0));
- // }
- //
- // barrier.await();
- // } catch (Exception e) {
- // throw new RuntimeException(e);
- // }
- // }
- // }
- }
- static {
- System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
- }
- public static void main(String[] args) throws InterruptedException {
- Mat image = imread("D:\\image.png");
- cvtColor(image, image, COLOR_RGB2GRAY);
- CvUtils.imshow(image, "image");
- Mat out = image.clone();
- List<Thread> threads = new ArrayList<>();
- final int threadCount = N / PROCS;
- for(int i = 0, n = 0; i < PROCS; i++, n += threadCount) {
- int to = i == PROCS - 1 ? K : n + threadCount;
- Thread thread = new Thread(new Filter(image, out, n, to));
- thread.start();
- threads.add(thread);
- }
- for (Thread thread : threads) {
- thread.join();
- }
- CvUtils.imshow(out, "out");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement