简体   繁体   中英

OpenCV camera image shows bluish

I'm using the below code to get an image from the camera, but the camera image shows colours differently.

public Mat mat = new Mat();
private BufferedImage img;
private byte[] dat;

    public void getSpace(Mat mat) {
            this.mat = mat;
            int w = mat.cols(), h = mat.rows();
            if (dat == null || dat.length != w * h * 3)
                dat = new byte[w * h * 3];
            if (img == null || img.getWidth() != w || img.getHeight() != h || img.getType() != BufferedImage.TYPE_3BYTE_BGR)
                img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
        }

        public BufferedImage getImage(Mat mat) {
            getSpace(mat);
            mat.get(0, 0, dat);
            img.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), dat);
            return img;
        }

在此处输入图片说明

Maybe your images is of type RGB instead of BRG? In that case you would have exchanged red and blue colour. Try changing TYPE_3BYTE_BGR into TYPE_3BYTE_RGB

[EDIT] Sorry, I am only on the Python side of OpenCV. What has happend is that you accidentially switched channels B and R (1st and 3rd layer of your image matrix), you need to switch this back. Exchange it in the matrix if necessary.

That link might help: Converting BufferedImage to Mat in opencv

[EDIT2] Have a look at the comment of Micka below

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM