2

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;
        }

enter image description here

Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42
PRCube
  • 566
  • 2
  • 6
  • 19

1 Answers1

1

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_BGRinto 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

Community
  • 1
  • 1
tfv
  • 6,016
  • 4
  • 36
  • 67
  • unfortunately, TYPE_3BYTE_RGB doesn't exist. the only rgb that exists is TYPE_INT_RGB which doesn't work. – PRCube Apr 26 '16 at 20:31
  • 1
    you can try to convert the opencv mat from rgb to bgr. c++ code would be cv::cvtColor (mat,mat, CV_RGB2BGR) maybe you xan convert that? – Micka Apr 26 '16 at 20:48