Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.50 KB | None | 0 0
  1.  
  2. public class Filter implements Runnable {
  3.  
  4.     private final int from;
  5.     private final int to;
  6.     private Mat y;
  7.     private Mat f;
  8.  
  9.     private static double []a = {
  10.         0.0404738053296999,
  11.         -0.0643096992131437,
  12.         -0.0139790480634063,
  13.         0.0715772831165316,
  14.         -0.0274783657038324,
  15.         -0.0634731190556242,
  16.         0.0191995380384644,
  17.         0.0236999687181153,
  18.         -0.00222016637849384,
  19.         0.0113003823627795,
  20.         -0.00113218936618811,
  21.         0.00938850762108079,
  22.         -0.00562895831074348,
  23.         -0.00481412192792152,
  24.         0.0217764167501000,
  25.         -0.0352209221161028,
  26.         0.0278540627491177,
  27.         -0.00973158992626677,
  28.         -0.00830170632505028,
  29.         0.0140575282597967,
  30.         -0.00560803411318567,
  31.         3.94522529384269e-05,
  32.         0.00226931186877385,
  33.         0.00411912796594011,
  34.         -0.00671529251454103,
  35.         0.00566318057030169,
  36.         -0.00276200445404060,
  37.         0.00129942023146980,
  38.         -0.00316761663763157,
  39.         0.00403822958532445,
  40.         -0.00395955782335479,
  41.         0.00174617650909325
  42.     };
  43.  
  44.     private static final int K = 32;
  45.     private static final int Mm = 16;
  46.     private static final int Mp = 15;
  47.     private static final int N = Mm + Mp + 1;
  48.     private static final int PROCS = 4;
  49.  
  50.     private static CyclicBarrier barrier = new CyclicBarrier(PROCS);
  51.     private static AtomicReference<Double> pixel = new AtomicReference<>(Double.valueOf(0));
  52.  
  53.     public Filter(Mat f, Mat y, int from, int to) {
  54.         this.f = f;
  55.         this.y = y;
  56.         this.from = from;
  57.         this.to = to;
  58.     }
  59.  
  60.     @Override
  61.     public void run() {
  62.         final int links = to - from;
  63.         for(int i = 0; i < f.rows(); i++) {
  64.             double[][] yk = new double[links][f.cols()];
  65.  
  66.             for(int t = 0; t < links; t++) {
  67.                 for (int j = 0; j < f.cols(); j++) {
  68.                     yk[t][j] = f.get(i, j)[0];
  69.                 }
  70.             }
  71.  
  72.             for (int j = Mm; j < f.cols() - Mp; j++) {
  73.  
  74.                 double px = 0;
  75.  
  76.                 for(int t = 0; t < links; t++) {
  77.                     final int jk = t + 1;
  78.                     if (jk == 0) {
  79.                         double f1 = f.get(i, j - Mm)[0] - f.get(i, j + Mp - 1)[0];
  80.                         yk[t][j] = yk[t][j - 1] + f1;
  81.                     } else if (jk % 2 == 0) {
  82.                         double f1 = f.get(i, j - Mm)[0] - f.get(i, j + Mp - 1)[0];
  83.                         double f12 = f.get(i, j - Mm - 1)[0] - f.get(i, j + Mp - 2)[0];
  84.                         double f3 = f1 - f12;
  85.                         yk[t][j] = 2 * Math.cos(Math.PI / N * (double)jk) * yk[t][j - 1] + yk[t][j - 2] + f3;
  86.                     } else {
  87.                         double f2 = f.get(i, j - Mm)[0] + f.get(i, j + Mp - 1)[0];
  88.                         double f22 = f.get(i, j - Mm - 1)[0] + f.get(i, j + Mp - 2)[0];
  89.                         double f4 = f2 - f22;
  90.                         yk[t][j] = 2 * Math.cos(Math.PI / N * (double)jk) * yk[t][j - 1] + yk[t][j - 2] + f4;
  91.                     }
  92.  
  93.                     yk[t][j] *= a[t];
  94.                     px += yk[t][j];
  95.                 }
  96.  
  97.                 pixel.accumulateAndGet(px, (x, y) -> x + y);
  98.  
  99.                 try {
  100.                     barrier.await();
  101.  
  102.                     if (from == 0) {
  103.                         y.put(i, j, pixel.get());
  104.                         pixel.set(Double.valueOf(0.0));
  105.                     }
  106.  
  107.                     barrier.await();
  108.                 } catch (Exception e) {
  109.                     throw new RuntimeException(e);
  110.                 }
  111.             }
  112.         }
  113.  
  114. //        for(int j = 0; j < f.cols(); j++) {
  115. //            double[][] yk = new double[links][f.rows()];
  116. //
  117. //            for(int t = 0; t < links; t++) {
  118. //                for (int i = 0; i < f.rows(); i++) {
  119. //                    yk[t][i] = f.get(i, j)[0];
  120. //                }
  121. //            }
  122. //
  123. //            for (int i = Mm; i < f.rows() - Mp; i++) {
  124. //
  125. //                double px = 0;
  126. //
  127. //                for(int t = 0; t < links; t++) {
  128. //                    final int jk = t + 1;
  129. //                    if (jk == 0) {
  130. //                        double f1 = y.get(i - Mm, j)[0] - y.get(i - Mp - 1, j)[0];
  131. //                        yk[t][i] = yk[t][i - 1] + f1;
  132. //                    } else if (jk % 2 == 0) {
  133. //                        double f1 = y.get(i - Mm, j)[0] - y.get(i + Mp - 1, j)[0];
  134. //                        double f12 = y.get(i - Mm - 1, j)[0] - y.get(i + Mp - 2, j)[0];
  135. //                        double f3 = f1 - f12;
  136. //                        yk[t][i] = 2 * Math.cos(Math.PI / N * (double)jk) * yk[t][i - 1] + yk[t][i - 2] + f3;
  137. //                    } else {
  138. //                        double f2 = y.get(i - Mm, j)[0] + y.get(i + Mp - 1, j)[0];
  139. //                        double f22 = y.get(i - Mm - 1, j)[0] + y.get(i + Mp - 2, j)[0];
  140. //                        double f4 = f2 - f22;
  141. //                        yk[t][i] = 2 * Math.cos(Math.PI / N * (double)jk) * yk[t][i- 1] + yk[t][i - 2] + f4;
  142. //                    }
  143. //
  144. //                    yk[t][i] *= a[t];
  145. //                    px += yk[t][i];
  146. //                }
  147. //
  148. //                pixel.accumulateAndGet(px, (x, y) -> x + y);
  149. //
  150. //                try {
  151. //                    barrier.await();
  152. //
  153. //                    if (from == 0) {
  154. //                        y.put(i, j, pixel.get());
  155. //                        pixel.set(Double.valueOf(0.0));
  156. //                    }
  157. //
  158. //                    barrier.await();
  159. //                } catch (Exception e) {
  160. //                    throw new RuntimeException(e);
  161. //                }
  162. //            }
  163. //        }
  164.     }
  165.  
  166.     static {
  167.         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  168.     }
  169.  
  170.     public static void main(String[] args) throws InterruptedException {
  171.         Mat image = imread("D:\\image.png");
  172.         cvtColor(image, image, COLOR_RGB2GRAY);
  173.  
  174.         CvUtils.imshow(image, "image");
  175.         Mat out = image.clone();
  176.         List<Thread> threads = new ArrayList<>();
  177.         final int threadCount = N / PROCS;
  178.         for(int i = 0, n = 0; i < PROCS; i++, n += threadCount) {
  179.             int to = i == PROCS - 1 ? K : n + threadCount;
  180.             Thread thread = new Thread(new Filter(image, out, n, to));
  181.             thread.start();
  182.             threads.add(thread);
  183.         }
  184.         for (Thread thread : threads) {
  185.             thread.join();
  186.         }
  187.         CvUtils.imshow(out, "out");
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement