I'm using OpenCV. I think I need to reduce the size of the transfer data, but I need your help.

Asked 2 years ago, Updated 2 years ago, 42 views

Raspberry Pi is transferring Mat objects over OpenCV to Java Server.

Some code for the client partial code.

Converts Mat objects to images, converts them back to buffer images, and sends them to the server. Mat's data size during this process is usually

It comes in about the size.

public static Image toImage(Mat m) {

        // // Check if image is grayscale or color
        int type = BufferedImage.TYPE_BYTE_GRAY;
        if (m.channels() > 1) {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }

        // // Transfer bytes from Mat to BufferedImage
        int bufferSize = m.channels() * m.cols() * m.rows();
        byte[] b = new byte[bufferSize];
        m.get(0, 0, b); // get all the pixels
        BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        System.arraycopy(b, 0, targetPixels, 0, b.length);
        return image;
    }

    public static BufferedImage toBufferedImage(Image img) {
        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }

        // // Create a buffered image with transparency
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);

        // // Draw the image on to the buffered image
        Graphics2D bGr = bimage.createGraphics();
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();

        // // Return the buffered image
        return bimage;
    }

    public static void main(String[] args) {
        String serverIp = "192.168.0.9";
        Socket socket = null;

        try 
        {
            // Connecting to a
            socket = new Socket(serverIp, 1492);
            System.out.println ("Connected to server").");

            // Register OutputStream
            OutputStream os = socket.getOutputStream();
            //Use the registered OutputStream as the ObjectOutputStream method.
            DataOutputStream oos;

            oos = new DataOutputStream(os);

            // // Register the default camera
            VideoCapture cap = new VideoCapture(0);

            // // Check if video capturing is enabled
            if (!cap.isOpened())
                System.exit(-1);

            // // Matrix for storing image
            Mat image = new Mat();
            int count = 0;

            try {
                while (true) 
                {
                    System.out.println ("while statement count: " + count);
                    cap.read(image);
                    if (!image.empty()) {

                        BufferedImage bimg = toBufferedImage(toImage(image));

                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        ImageIO.write(bimg, "jpg", baos);
                        byte[] imageInByte = baos.toByteArray();
                        oos.writeInt(imageInByte.length); // write length of the
                                                            // // imageInByte
                        oos.write(imageInByte); // write the imageInByte

                        //FileSender fs = new FileSender(socket, oos, cap, image, os);
                        //fs.start();
                        baos.close();
                    }

                    else {
                        System.out.println("No captured frame -- camera disconnected");
                    }
                    count ++;
                }

            } } catch (Exception e) {
                e.getStackTrace();
            } } finally {
                System.out.println ("★Turned off from client!!)");
                oos.close();
                os.close();
                socket.close();
            }

        } 
        catch (Exception e) { e.printStackTrace(); }
    }
}

Then, the server converts the buffer image back into an image, changes it to an image icon, and continues to spray the screen so that you can see the smooth image information.

while (true) 
            {
                System.out.println("client accept!");

                is = socket.getInputStream();

                // Use the registered InputStream as the Object InputStream method.
                ois = new DataInputStream(is);

                int length = ois.readInt(); // read length of incoming message
                byte[] data = null;

                if (length > 0) {
                    data = new byte[length];
                    ois.readFully(data, 0, data.length); // read the message
                }

                StringdirName = "C:\Users\\Ahn Hyung-wook\\workspace\\System Development Project1";
                String fileName = "test.jpg";

                System.out.println("data length : " + data.length);
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                BufferedImage imag = ImageIO.read(bais);
                //ImageIO.write(imag, "jpg", new File(dirName, fileName));

                cm.setCameraView(imag); // convert and apply received image

                // Start File Receiving Job
                // // FileReceiver fr = new FileReceiver(socket, cm);
                // // fr.start();
                bais.close();
            }

But the code I posted is that if you use a camera on a regular PC laptop, you can see a smooth screen without a problem, but if you use Raspberry Pie, you can see it cut off. I think the reason is the performance problem. I think it's because raspberry pie doesn't perform as well as PC.

So, to get to the point, I'm asking you if OpenCV has a way to access Matt's pixels or data and reduce the overall size of the data you send.

It would be nice to know if we can solve the problem of disconnecting from Raspberry Pi other than this method. Please answer me~~ Please!!!

Please let me know the code in Java

java raspberry-pi

2022-09-22 16:00

1 Answers

Use OpenCV's Imgproc.resize() function to reduce the size of the image properly. For codes using the resize() function, check the link below.

OpenCV Imgproc.resize() Function Description


2022-09-22 16:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.