Search in sources :

Example 1 with UByteIndexer

use of org.bytedeco.javacpp.indexer.UByteIndexer in project javacv by bytedeco.

the class FrameGrabberTest method testFFmpegFrameGrabberLockingTest.

@Test
public void testFFmpegFrameGrabberLockingTest() {
    final boolean[] failed = { false };
    final int numberOfInstances = 20;
    System.out.println("FFmpegFrameGrabberLocking");
    Runnable[] runables = new Runnable[numberOfInstances];
    Thread[] threads = new Thread[numberOfInstances];
    final boolean[] finish = new boolean[numberOfInstances];
    for (int instance = 0; instance < numberOfInstances; instance++) {
        final int instance_final = instance;
        Runnable r = new Runnable() {

            public void run() {
                File tempFile = new File(Loader.getTempDir(), "test" + instance_final + ".mkv");
                try {
                    FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(new FileOutputStream(tempFile), 640, 480, 2);
                    // mp4 doesn't support streaming
                    recorder.setFormat("matroska");
                    recorder.setPixelFormat(AV_PIX_FMT_BGR24);
                    recorder.setVideoCodecName("libx264rgb");
                    // lossless
                    recorder.setVideoQuality(0);
                    recorder.setSampleFormat(AV_SAMPLE_FMT_S16);
                    recorder.setSampleRate(44100);
                    recorder.setAudioCodecName("pcm_s16le");
                    recorder.start();
                    Frame[] frames = new Frame[10];
                    for (int n = 0; n < frames.length; n++) {
                        Frame frame = new Frame(640, 480, Frame.DEPTH_UBYTE, 3);
                        UByteIndexer frameIdx = frame.createIndexer();
                        for (int i = 0; i < frameIdx.rows(); i++) {
                            for (int j = 0; j < frameIdx.cols(); j++) {
                                for (int k = 0; k < frameIdx.channels(); k++) {
                                    frameIdx.put(i, j, k, n + i + j + k);
                                }
                            }
                        }
                        recorder.record(frame);
                        frames[n] = frame;
                    }
                    Frame audioFrame = new Frame();
                    ShortBuffer audioBuffer = ShortBuffer.allocate(64 * 1024);
                    audioFrame.sampleRate = 44100;
                    audioFrame.audioChannels = 2;
                    audioFrame.samples = new ShortBuffer[] { audioBuffer };
                    for (int i = 0; i < audioBuffer.capacity(); i++) {
                        audioBuffer.put(i, (short) i);
                    }
                    recorder.record(audioFrame);
                    recorder.stop();
                    recorder.release();
                    Thread.sleep(1000);
                    FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(new FileInputStream(tempFile));
                    grabber.setSampleMode(FrameGrabber.SampleMode.FLOAT);
                    grabber.start();
                    int n = 0, m = 0;
                    Frame frame2;
                    while ((frame2 = grabber.grab()) != null) {
                        if (frame2.image != null) {
                            Frame frame = frames[n++];
                            assertEquals(frame.imageWidth, frame2.imageWidth);
                            assertEquals(frame.imageHeight, frame2.imageHeight);
                            assertEquals(frame.imageChannels, frame2.imageChannels);
                            UByteIndexer frameIdx = frame.createIndexer();
                            UByteIndexer frame2Idx = frame2.createIndexer();
                            for (int i = 0; i < frameIdx.rows(); i++) {
                                for (int j = 0; j < frameIdx.cols(); j++) {
                                    for (int k = 0; k < frameIdx.channels(); k++) {
                                        int b = frameIdx.get(i, j, k);
                                        assertEquals(b, frame2Idx.get(i, j, k));
                                    }
                                }
                            }
                        } else {
                            FloatBuffer audioBuffer2 = (FloatBuffer) frame2.samples[0];
                            while (audioBuffer2.hasRemaining()) {
                                assertEquals((float) audioBuffer.get(m++) / (Short.MAX_VALUE + 1), audioBuffer2.get(), 0);
                            }
                        }
                    }
                    assertEquals(frames.length, n);
                    assertEquals(null, grabber.grab());
                    grabber.restart();
                    grabber.stop();
                    grabber.release();
                } catch (Error | Exception e) {
                    failed[0] = true;
                    fail("Exception should not have been thrown: " + e);
                } finally {
                    tempFile.delete();
                    finish[instance_final] = true;
                }
            }
        };
        runables[instance_final] = r;
    }
    for (int instance = 0; instance < numberOfInstances; instance++) {
        threads[instance] = new Thread(runables[instance]);
        threads[instance].setName("Testthread-" + instance);
    }
    for (int instance = 0; instance < numberOfInstances; instance++) {
        threads[instance].start();
    }
    while (true) {
        boolean finished = true;
        for (int instance = 0; instance < numberOfInstances; instance++) {
            if (!finish[instance]) {
                finished = false;
                break;
            }
        }
        if (!finished) {
            System.out.println("Still waiting...");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            break;
        }
    }
    assertFalse(failed[0]);
}
Also used : FloatBuffer(java.nio.FloatBuffer) UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ShortBuffer(java.nio.ShortBuffer) Test(org.junit.Test)

Example 2 with UByteIndexer

use of org.bytedeco.javacpp.indexer.UByteIndexer in project javacv by bytedeco.

the class ImageSegmentation method main.

public static void main(String[] args) {
    // Load the image
    Mat src = imread(args[0]);
    // Check if everything was fine
    if (src.data().isNull())
        return;
    // Show source image
    imshow("Source Image", src);
    // Change the background from white to black, since that will help later to extract
    // better results during the use of Distance Transform
    UByteIndexer srcIndexer = src.createIndexer();
    for (int x = 0; x < srcIndexer.rows(); x++) {
        for (int y = 0; y < srcIndexer.cols(); y++) {
            int[] values = new int[3];
            srcIndexer.get(x, y, values);
            if (Arrays.equals(values, WHITE)) {
                srcIndexer.put(x, y, BLACK);
            }
        }
    }
    // Show output image
    imshow("Black Background Image", src);
    // Create a kernel that we will use for accuting/sharpening our image
    Mat kernel = Mat.ones(3, 3, CV_32F).asMat();
    FloatIndexer kernelIndexer = kernel.createIndexer();
    // an approximation of second derivative, a quite strong kernel
    kernelIndexer.put(1, 1, -8);
    // do the laplacian filtering as it is
    // well, we need to convert everything in something more deeper then CV_8U
    // because the kernel has some negative values,
    // and we can expect in general to have a Laplacian image with negative values
    // BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
    // so the possible negative number will be truncated
    Mat imgLaplacian = new Mat();
    // copy source image to another temporary one
    Mat sharp = src;
    filter2D(sharp, imgLaplacian, CV_32F, kernel);
    src.convertTo(sharp, CV_32F);
    Mat imgResult = subtract(sharp, imgLaplacian).asMat();
    // convert back to 8bits gray scale
    imgResult.convertTo(imgResult, CV_8UC3);
    imgLaplacian.convertTo(imgLaplacian, CV_8UC3);
    // imshow( "Laplace Filtered Image", imgLaplacian );
    imshow("New Sharped Image", imgResult);
    // copy back
    src = imgResult;
    // Create binary image from source image
    Mat bw = new Mat();
    cvtColor(src, bw, CV_BGR2GRAY);
    threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
    imshow("Binary Image", bw);
    // Perform the distance transform algorithm
    Mat dist = new Mat();
    distanceTransform(bw, dist, CV_DIST_L2, 3);
    // Normalize the distance image for range = {0.0, 1.0}
    // so we can visualize and threshold it
    normalize(dist, dist, 0, 1., NORM_MINMAX, -1, null);
    imshow("Distance Transform Image", dist);
    // Threshold to obtain the peaks
    // This will be the markers for the foreground objects
    threshold(dist, dist, .4, 1., CV_THRESH_BINARY);
    // Dilate a bit the dist image
    Mat kernel1 = Mat.ones(3, 3, CV_8UC1).asMat();
    dilate(dist, dist, kernel1);
    imshow("Peaks", dist);
    // Create the CV_8U version of the distance image
    // It is needed for findContours()
    Mat dist_8u = new Mat();
    dist.convertTo(dist_8u, CV_8U);
    // Find total markers
    MatVector contours = new MatVector();
    findContours(dist_8u, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    // Create the marker image for the watershed algorithm
    Mat markers = Mat.zeros(dist.size(), CV_32SC1).asMat();
    // Draw the foreground markers
    for (int i = 0; i < contours.size(); i++) drawContours(markers, contours, i, Scalar.all((i) + 1));
    // Draw the background marker
    circle(markers, new Point(5, 5), 3, RGB(255, 255, 255));
    imshow("Markers", multiply(markers, 10000).asMat());
    // Perform the watershed algorithm
    watershed(src, markers);
    Mat mark = Mat.zeros(markers.size(), CV_8UC1).asMat();
    markers.convertTo(mark, CV_8UC1);
    bitwise_not(mark, mark);
    // imshow("Markers_v2", mark); // uncomment this if you want to see how the mark
    // image looks like at that point
    // Generate random colors
    List<int[]> colors = new ArrayList<int[]>();
    for (int i = 0; i < contours.size(); i++) {
        int b = theRNG().uniform(0, 255);
        int g = theRNG().uniform(0, 255);
        int r = theRNG().uniform(0, 255);
        int[] color = { b, g, r };
        colors.add(color);
    }
    // Create the result image
    Mat dst = Mat.zeros(markers.size(), CV_8UC3).asMat();
    // Fill labeled objects with random colors
    IntIndexer markersIndexer = markers.createIndexer();
    UByteIndexer dstIndexer = dst.createIndexer();
    for (int i = 0; i < markersIndexer.rows(); i++) {
        for (int j = 0; j < markersIndexer.cols(); j++) {
            int index = markersIndexer.get(i, j);
            if (index > 0 && index <= contours.size())
                dstIndexer.put(i, j, colors.get(index - 1));
            else
                dstIndexer.put(i, j, BLACK);
        }
    }
    // Visualize the final image
    imshow("Final Result", dst);
}
Also used : Mat(org.bytedeco.javacpp.opencv_core.Mat) ArrayList(java.util.ArrayList) FloatIndexer(org.bytedeco.javacpp.indexer.FloatIndexer) MatVector(org.bytedeco.javacpp.opencv_core.MatVector) Point(org.bytedeco.javacpp.opencv_core.Point) UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer) Point(org.bytedeco.javacpp.opencv_core.Point) IntIndexer(org.bytedeco.javacpp.indexer.IntIndexer)

Example 3 with UByteIndexer

use of org.bytedeco.javacpp.indexer.UByteIndexer in project javacv by bytedeco.

the class FrameConverterTest method testAndroidFrameConverter.

@Test
public void testAndroidFrameConverter() {
    System.out.println("AndroidFrameConverter");
    AndroidFrameConverter converter = new AndroidFrameConverter();
    int width = 512;
    int height = 1024;
    byte[] yuvData = new byte[3 * width * height / 2];
    for (int i = 0; i < yuvData.length; i++) {
        yuvData[i] = (byte) i;
    }
    Mat yuvImage = new Mat(3 * height / 2, width, CV_8UC1, new BytePointer(yuvData));
    Mat bgrImage = new Mat(height, width, CV_8UC3);
    cvtColor(yuvImage, bgrImage, CV_YUV2BGR_NV21);
    Frame bgrFrame = converter.convert(yuvData, width, height);
    UByteIndexer bgrImageIdx = bgrImage.createIndexer();
    UByteIndexer bgrFrameIdx = bgrFrame.createIndexer();
    assertEquals(bgrImageIdx.rows(), bgrFrameIdx.rows());
    assertEquals(bgrImageIdx.cols(), bgrFrameIdx.cols());
    assertEquals(bgrImageIdx.channels(), bgrFrameIdx.channels());
    for (int i = 0; i < bgrImageIdx.rows(); i++) {
        for (int j = 0; j < bgrImageIdx.cols(); j++) {
            for (int k = 0; k < bgrImageIdx.channels(); k++) {
                assertEquals((float) bgrImageIdx.get(i, j, k), (float) bgrFrameIdx.get(i, j, k), 1.0f);
            }
        }
    }
    bgrImageIdx.release();
    bgrFrameIdx.release();
    Frame grayFrame = new Frame(1024 + 1, 768, Frame.DEPTH_UBYTE, 1);
    Frame colorFrame = new Frame(640 + 1, 480, Frame.DEPTH_UBYTE, 3);
    UByteIndexer grayFrameIdx = grayFrame.createIndexer();
    for (int i = 0; i < grayFrameIdx.rows(); i++) {
        for (int j = 0; j < grayFrameIdx.cols(); j++) {
            grayFrameIdx.put(i, j, i + j);
        }
    }
    UByteIndexer colorFrameIdx = colorFrame.createIndexer();
    for (int i = 0; i < colorFrameIdx.rows(); i++) {
        for (int j = 0; j < colorFrameIdx.cols(); j++) {
            for (int k = 0; k < colorFrameIdx.channels(); k++) {
                colorFrameIdx.put(i, j, k, i + j + k);
            }
        }
    }
    width = grayFrame.imageWidth;
    height = grayFrame.imageHeight;
    int stride = grayFrame.imageStride;
    int rowBytes = width * 4;
    ByteBuffer in = (ByteBuffer) grayFrame.image[0];
    ByteBuffer buffer = converter.gray2rgba(in, width, height, stride, rowBytes);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // GRAY -> RGBA
            byte B = in.get(y * stride + x);
            assertEquals(buffer.get(y * rowBytes + 4 * x), B);
            assertEquals(buffer.get(y * rowBytes + 4 * x + 1), B);
            assertEquals(buffer.get(y * rowBytes + 4 * x + 2), B);
            assertEquals(buffer.get(y * rowBytes + 4 * x + 3), (byte) 0xFF);
        }
    }
    width = colorFrame.imageWidth;
    height = colorFrame.imageHeight;
    stride = colorFrame.imageStride;
    rowBytes = width * 4;
    in = (ByteBuffer) colorFrame.image[0];
    buffer = converter.bgr2rgba(in, width, height, stride, rowBytes);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // BGR -> RGBA
            byte B = in.get(y * stride + 3 * x);
            byte G = in.get(y * stride + 3 * x + 1);
            byte R = in.get(y * stride + 3 * x + 2);
            assertEquals(buffer.get(y * rowBytes + 4 * x), R);
            assertEquals(buffer.get(y * rowBytes + 4 * x + 1), G);
            assertEquals(buffer.get(y * rowBytes + 4 * x + 2), B);
            assertEquals(buffer.get(y * rowBytes + 4 * x + 3), (byte) 0xFF);
        }
    }
    colorFrameIdx.release();
    grayFrameIdx.release();
}
Also used : BytePointer(org.bytedeco.javacpp.BytePointer) UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with UByteIndexer

use of org.bytedeco.javacpp.indexer.UByteIndexer in project javacv by bytedeco.

the class FrameGrabberChangingResolutionTest method makeTestfile.

private void makeTestfile() throws Exception {
    FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(new FileOutputStream(tempFile), 640, 480, 2);
    // mp4 doesn't support streaming
    recorder.setFormat("matroska");
    recorder.setPixelFormat(AV_PIX_FMT_BGR24);
    recorder.setVideoCodecName("libx264rgb");
    // lossless
    recorder.setVideoQuality(0);
    recorder.setFrameRate(30);
    recorder.start();
    Frame[] frames = new Frame[60];
    for (int n = 0; n < frames.length; n++) {
        Frame frame = new Frame(640, 480, Frame.DEPTH_UBYTE, 3);
        UByteIndexer frameIdx = frame.createIndexer();
        for (int i = 0; i < frameIdx.rows(); i++) {
            for (int j = 0; j < frameIdx.cols(); j++) {
                for (int k = 0; k < frameIdx.channels(); k++) {
                    frameIdx.put(i, j, k, n + i + j + k);
                }
            }
        }
        recorder.record(frame);
        frames[n] = frame;
    }
    recorder.stop();
    recorder.release();
}
Also used : FileOutputStream(java.io.FileOutputStream) UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer)

Example 5 with UByteIndexer

use of org.bytedeco.javacpp.indexer.UByteIndexer in project javacpp by bytedeco.

the class IndexerTest method testUByteIndexer.

@Test
public void testUByteIndexer() {
    System.out.println("UByteIndexer");
    long size = 7 * 5 * 3 * 2;
    long[] sizes = { 7, 5, 3, 2 };
    long[] strides = { 5 * 3 * 2, 3 * 2, 2, 1 };
    final BytePointer ptr = new BytePointer(size);
    for (int i = 0; i < size; i++) {
        ptr.position(i).put((byte) i);
    }
    UByteIndexer arrayIndexer = UByteIndexer.create(ptr.position(0), sizes, strides, false);
    UByteIndexer directIndexer = UByteIndexer.create(ptr.position(0), sizes, strides, true);
    int n = 0;
    for (int i = 0; i < sizes[0]; i++) {
        assertEquals(n, arrayIndexer.get(i * strides[0]));
        assertEquals(n, directIndexer.get(i * strides[0]));
        for (int j = 0; j < sizes[1]; j++) {
            assertEquals(n, arrayIndexer.get(i, j * strides[1]));
            assertEquals(n, directIndexer.get(i, j * strides[1]));
            for (int k = 0; k < sizes[2]; k++) {
                assertEquals(n, arrayIndexer.get(i, j, k * strides[2]));
                assertEquals(n, directIndexer.get(i, j, k * strides[2]));
                for (int m = 0; m < sizes[3]; m++) {
                    long[] index = { i, j, k, m * strides[3] };
                    assertEquals(n, arrayIndexer.get(index));
                    assertEquals(n, directIndexer.get(index));
                    arrayIndexer.put(index, n + 1);
                    directIndexer.put(index, n + 2);
                    n++;
                }
            }
        }
    }
    try {
        arrayIndexer.get(size);
        fail("IndexOutOfBoundsException should have been thrown.");
    } catch (IndexOutOfBoundsException e) {
    }
    try {
        directIndexer.get(size);
        fail("IndexOutOfBoundsException should have been thrown.");
    } catch (IndexOutOfBoundsException e) {
    }
    System.out.println("arrayIndexer" + arrayIndexer);
    System.out.println("directIndexer" + directIndexer);
    for (int i = 0; i < size; i++) {
        assertEquals(i + 2, ptr.position(i).get() & 0xFF);
    }
    arrayIndexer.release();
    for (int i = 0; i < size; i++) {
        assertEquals(i + 1, ptr.position(i).get() & 0xFF);
    }
    System.gc();
    try {
        long longSize = 0x80000000L + 8192;
        final BytePointer longPointer = new BytePointer(longSize);
        assertEquals(longSize, longPointer.capacity());
        UByteIndexer longIndexer = UByteIndexer.create(longPointer);
        assertEquals(longIndexer.pointer(), longPointer);
        for (long i = 0; i < 8192; i++) {
            longPointer.put(longSize - i - 1, (byte) i);
        }
        for (long i = 0; i < 8192; i++) {
            assertEquals(longIndexer.get(longSize - i - 1), i & 0xFF);
        }
        System.out.println("longIndexer[0x" + Long.toHexString(longSize - 8192) + "] = " + longIndexer.get(longSize - 8192));
    } catch (OutOfMemoryError e) {
        System.out.println(e);
    }
    System.out.println();
}
Also used : UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer) Test(org.junit.Test)

Aggregations

UByteIndexer (org.bytedeco.javacpp.indexer.UByteIndexer)8 Test (org.junit.Test)6 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 ShortBuffer (java.nio.ShortBuffer)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 FloatBuffer (java.nio.FloatBuffer)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 BytePointer (org.bytedeco.javacpp.BytePointer)1 FloatIndexer (org.bytedeco.javacpp.indexer.FloatIndexer)1 IntIndexer (org.bytedeco.javacpp.indexer.IntIndexer)1 Mat (org.bytedeco.javacpp.opencv_core.Mat)1 MatVector (org.bytedeco.javacpp.opencv_core.MatVector)1 Point (org.bytedeco.javacpp.opencv_core.Point)1