I am trying to use the multi-threaded code of OpenCV4Android. I split the 432x432 image into 9 144x144 segments and pass each one to a different stream:
Thread[] threads = new Thread[9]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { threads[3*i+j] = new Thread(new MyThread(image.rowRange(144*i, 144*(i+1)).colRange(144*j, 144*(j+1)))); threads[3*i+j].start(); } } for (Thread thread : threads) try {thread.join();} catch (InterruptedException e) {};
Here is the stream class:
public class MyThread implements Runnable { final Mat block; public MyThread(Mat block) { this.block = block; } public void run() { Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(19,19)); Mat closed = new Mat(); Imgproc.morphologyEx(block, closed, Imgproc.MORPH_CLOSE, kernel); Core.divide(block, closed, block, 1, CvType.CV_32F); Core.normalize(block, block, 0, 255, Core.NORM_MINMAX); block.convertTo(block, CvType.CV_8UC1); Imgproc.threshold(block, block, -1, 255, Imgproc.THRESH_BINARY_INV+Imgproc.THRESH_OTSU); } }
I have two problems:
Although threads correctly modify individual blocks, modifications are not displayed in the final image. This would make sense if the Mat block passed by value to the stream, but Java instead passed a reference to the stream.
The execution time is longer than the inaccurate code - in my emulator it increases from ~ 1200 to ~ 1500 ms. Is this a problem with the emulator, or is for some reason a multithreaded idea really bad?
source share