Ask Your Question

SniFFzoR's profile - activity

2017-04-04 16:04:05 -0500 asked a question How to create a matrix and make it a copy of the camera matrix?


I have retrieved the calibration parameters for my camera, meaning that I have the camera matrix and the distortion vector. However, now that I have those, I want to undistort my image using the function:

Imgproc.undistort(Mat src, Mat dst, Mat cameraMatrix, Mat distVector);

I have them all, I just don't know how to create a matrix and fill it with values.

I would like to do something like this:

Mat cameraMatrix = new Mat(3,3);
cameraMatrix.put(0, 0, f_x);
cameraMatrix.put(0, 1, 0);
cameraMatrix.put(0, 2, c_x);
cameraMatrix.put(1, 0, 0);
cameraMatrix.put(1, 1, f_y);
cameraMatrix.put(1, 2, c_y);
cameraMatrix.put(2, 0, 0);
cameraMatrix.put(2, 1, 0);
cameraMatrix.put(2, 2, 1)

Of course, I would do the same for the distortion vector.

The problem is that I haven't found any documentation on how to create matrices in OpenCV for Java. I don't mean like Mat mat = new Mat(), but how to create a matrix and fill it with values.

I hope that you guys can help.

2017-02-25 21:45:25 -0500 commented answer Create camera calibration project in Visual Studio 2015

I do as this dude does it: If you watch from maybe 6:40 to 8:00, you will see the errors that I get as well.

I'm not entirely sure that I understand what you mean with "source file in folder". What is the source file and in what folder is it?

2017-02-25 18:09:49 -0500 commented answer Create camera calibration project in Visual Studio 2015

I have done what you stated above, but it puts me no where closer. I have a OpenCV.sln file in MyCalibration, but if I want to create a new project, copy some code from a file in F:\opencvfin\samples\cpp\tutorial_code\calib3d\camera_calibration and paste it into my project, I only get erros in the project. My problem is that I don't have a clue on how to solve this. The most ridiculous thing here is that I have a masters degree in computer engineering, and this is the most difficult thing I've ever done. Seems unecessary to be this complex.

2017-02-25 16:10:08 -0500 commented answer Create camera calibration project in Visual Studio 2015

I'm not sure that I understand. I have a OpenCV.sln-file that I can open. It is when I create a new project in Visual Studio, and paste the code from camera_calibration.cpp into the new project that makes it all strange.

2017-02-25 14:50:57 -0500 commented question Create camera calibration project in Visual Studio 2015

I did use CMake. I followed this tutorial.

2017-02-25 11:50:07 -0500 asked a question Create camera calibration project in Visual Studio 2015


In order to be able to calibrate my camera, I downloaded visual studio and tried to create a camera calibration project. However, when I create a project (I choose Win32 Console Application -> Application Type = Console Application. Then, in the file that is created (called Testing.cpp) I try to paste the code from C:\OpenCV\opencv\samples\cpp\tutorial_code\calib3d\camera_calibration\camera_calibration.cpp into my Testing.cpp-file. When I do that, every line that contains "#include" is red.

Since I don't normally work with OpenCV in Visual Studio, I don't know how to resolve this. It would be awesome if anyone could point me in the correct direction.

2017-02-25 11:42:31 -0500 received badge  Enthusiast
2017-02-23 17:48:19 -0500 asked a question How to use camera calibration in Java


I haven't calibrated my camera as of yet, but I'm planning to just run a C++ program in some way, and grab the necessary matrices. Now, my question is sort of similar to this one. I want to know how (in Java) I should use the information grabbed in C++. I don't seem to be able to find an answer to this question, but I'm sure that some of the OpenCV Java users must have their cameras calibrated.

2016-10-16 16:20:07 -0500 commented question Cannot open some video files in OpenCV 2.4.13 for Java

FourCC = 541215044 if I cast it to an integer. What is that value anyway?

2016-10-16 12:56:38 -0500 commented question Cannot open some video files in OpenCV 2.4.13 for Java

I already have that piece of code in place, and for the video file that doesn't work, I always enter the else-block in your example.I have also edited the original post, just to notify that I've tried to have both of the files in the same folder, but without luck.

2016-10-16 12:14:50 -0500 commented question Cannot open some video files in OpenCV 2.4.13 for Java

Okay, so I did what I thought that you meant and ended up with this:

vidCap.get(CV_CAP_PROP_FPS) = 4 vidCap.get(CV_CAP_PROP_FOURCC) = 5.41215044E8 vidCap.get(CV_CAP_PROP_FRAME_WIDTH) = 640 vidCap.get(CV_CAP_PROP_FRAME_HEIGHT) = 480

For the other video that doesn't work, I get 0.0 on all of the values.

2016-10-16 12:05:09 -0500 commented question Cannot open some video files in OpenCV 2.4.13 for Java

I guess you mean that I should check those values for one video file that does work, and compare it with another one that doesn't? Have I understood that correctly?

2016-10-16 10:11:58 -0500 received badge  Editor (source)
2016-10-16 09:42:39 -0500 asked a question Cannot open some video files in OpenCV 2.4.13 for Java


I have a problem regarding opening video files. I recently created an .avi-file from a bunch of images that I had taken from a camera. When I tried to open this using the following line of code,

VideoCapture vidCap = new VideoCapture("C:/somePathOnC/fileName.avi");

everything worked fine. However, when I try another video file, for instance the example video from this example. The video file in that example is called bouncingBall.avi. This, however, won't work. The program does not open up the video file, and no errors are shown.

The same happens with another video file I took from my IP-camera. The program I used to record the file, iSpy, saved the file as .mp4. Since this didn't work, I tried to convert it to .avi with different "codecs". I have so far tried all of the settings that the converter program offers. These are:

  • H.264 /MPEG-4 AVC video format
  • AVI-audio-video interleaved
  • XViD-movie
  • Lossless uncompressed AVI
  • AVI with DV-coded

Also, please not that I've tried to have both of the files in the same folder, but it still didn't work.

So now, I'm out of ideas on how to solve this. I have done some search about an issue regarding FFMPEG, but I do not understand how to resolve that.

Thank you.

2016-10-12 06:46:28 -0500 commented answer Use BackgroundSubtractorMOG2 in OpenCV 2.4.13 with Java

Yeah, I know that. I simply overlooked the fact due to I looked to much on the old code. Anyway, thanks again.

2016-10-12 06:45:50 -0500 received badge  Supporter (source)
2016-10-12 06:45:48 -0500 received badge  Scholar (source)
2016-10-12 04:31:40 -0500 commented question Use BackgroundSubtractorMOG2 in OpenCV 2.4.13 with Java

It worked. So all it needed was the "new" added before. Btw, it also seems to work with BackgroundSubtractorMOG2, not just the ordinary MOG.

2016-10-12 04:05:22 -0500 asked a question Use BackgroundSubtractorMOG2 in OpenCV 2.4.13 with Java


I have been running my openCV setup on windows until now, but now I need to change to Ubuntu. Because of this, I was unable to create a .jar file for openCV version 3.x so I ended up with version 2.4.13.

This means that some of the lines of code that I had (perfectly working in Windows with openCV 3.1) no longer work.

I had the following line:

BackgroundSubtractorMOG2 mog2 = Video.createBackgroundSubtractorMOG2();

Now, I am trying to change this to work with openCV version 2.4.13, but I'm simply not able to.

I have tried the following line

mog2 =;

but this gives me the following: cannot be resolved to a type.

What can I do to make this run on my current setup?

2016-09-30 06:57:17 -0500 commented question Capture video from d-link DCS-930LB

I think that the problem is within my internet settings, maybe the router. Does anybody else think that the settings could matter, making it impossible to see the feed?

2016-09-23 08:20:44 -0500 commented question Improve background segmentation through BackgroundSubtractMOG2

Okay, I can get that to work with this line of Java code (for people that are looking for the syntax): Imgproc.morphologyEx(foreground, foreground, Imgproc.MORPH_CLOSE, Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(7,7)), new Point(-1, -1), 5);

I just want to know; how did you know what parameters to pass to the getStructuringElement method? Also, how did you know what parameters to pass to the Point class?

2016-09-22 14:55:05 -0500 commented question Improve background segmentation through BackgroundSubtractMOG2

Okay, but how do I call for that particular method, and which kernel should I use?

2016-09-22 08:30:03 -0500 asked a question Improve background segmentation through BackgroundSubtractMOG2


I have created a small program that is supposed to detect changes in the foreground over a background. It works, but not as well as I would like it to. Here is what I have done so far, to try to improve it on my own.

  • Tweaked parameters (such as history length, learningRate, threshold, shadows etc)
  • I have tried to use GaussianBlur, but that did not help much (maybe I'm using it wrong?)
  • I have tried to convert it to grayscale. I'm not sure it made anything better though.
  • I have thought about using some sort of morphology, but I don't seem to be able to get it to work. I'm thinking that I have the wrong kernel, and I'm also very uncertain about which kernel to use (or how to create my own)

Here are two images. The one to on top is one that is produced by the program (me holding a tube of shave balm). The one below is one that I have created in paint, and it is like that I want the program to be able to produce on its own. Also note that if I were to have my face in the image instead, the program would produce a much worse result than when I used my hand.

image description image description

Below, you will see my code. Please come up with suggestions on how to improve my segmentation.

public class Testing {
static {

static Mat imag = null;
static Mat foreground = null;
static String fileName = "randomTest.png";

public static void main(String[] args) {

    JFrame jFrame = new JFrame("Background subtraction");
    JLabel videoPanel = new JLabel();
    jFrame.setSize(640, 480);

    double learningRate = 0.001;

    JFrame jForeGround = new JFrame("Foreground shown");
    JLabel foreGroundPanel = new JLabel();
    jForeGround.setSize(640, 480);

    Mat frame = new Mat();

    VideoCapture camera = new VideoCapture(0);

    Size size = new Size(640, 480);
    BackgroundSubtractorMOG2 mog = Video.createBackgroundSubtractorMOG2(100, 16, false);
    //Mat kernel = Imgproc.getGaussianKernel(3, 1.5);

    while (true) {
        if ( {
            Imgproc.resize(frame, frame, size);

            imag = frame.clone();
            foreground = new Mat(frame.size(), CvType.CV_8UC1);
            Imgproc.cvtColor(imag, foreground, Imgproc.COLOR_BGR2GRAY);
            Imgproc.GaussianBlur(foreground, foreground, new Size(7,7), 0);

            mog.apply(imag, foreground, learningRate);
            //Imgproc.dilate(foreground, foreground, kernel);
            ImageIcon image = new ImageIcon(mat2bufferedImage(imag));
            ImageIcon imageForeground = new ImageIcon(mat2bufferedImage(foreground));
            //Imgcodecs.imwrite(fileName, foreground);


private static BufferedImage mat2bufferedImage(Mat image) {
    MatOfByte bytemat = new MatOfByte();
    Imgcodecs.imencode(".jpg", image, bytemat);
     * Encodes an image into a memory buffer. The first argument is the file
     * extension. The second argument is the image to be written, and the
     * third is the output buffer
    byte[] bytes = bytemat.toArray();
     * Creates an array of bytes, and puts bytemat in it
    InputStream in = new ByteArrayInputStream(bytes);
    BufferedImage img = null;
    try {
        img =;
    } catch (IOException e) {
    return img;
2016-09-07 10:57:04 -0500 asked a question Capture video from d-link DCS-930LB


I have a problem when I'm trying to capture video from my IP-camera, mentioned in the topic.

I have tried to solve it using the tips and examples displayed here, as well as the advice given here, but nothing seems to work.

My code, originally taken from this example, looks as follows.

import javax.swing.JFrame;
import org.opencv.core.Core;
import org.opencv.videoio.VideoCapture;

public class OpenCVTest {

  public OpenCVTest() {

    // TODO Auto-generated constructor stub

 * @Param args
 * @throws IOException
public static void main(String[] args) throws IOException {


    VideoCapture ip_camera = new VideoCapture("http://username:password@ip-address/video.cgi?x.mjpeg");

    // VideoCapture camera = new VideoCapture(0);
    if (ip_camera.isOpened()) {
        System.out.println("Video is captured");
    } else {
    VideoCamera cam = new VideoCamera(ip_camera);

    JFrame frame = new JFrame();
    frame.setSize(800, 800);

    while (ip_camera.isOpened()) {




import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import javax.swing.JPanel;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;

public class VideoCamera extends JPanel {
VideoCapture camera;

public VideoCamera(VideoCapture cam) {

    camera = cam;


 * @Param args
public static void main(String[] args) {

    // TODO Auto-generated method stub


public BufferedImage Mat2BufferedImage(Mat m) {

    int type = BufferedImage.TYPE_BYTE_GRAY;
    if (m.channels() > 1) {
        type = BufferedImage.TYPE_3BYTE_BGR;
    int bufferSize = m.channels() * m.cols() * m.rows();
    byte[] b = new byte[bufferSize];
    m.get(0, 0, b); // get all the pixels
    BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
    final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
    System.arraycopy(b, 0, targetPixels, 0, b.length);
    return img;


public void paintComponent(Graphics g) {
    Mat mat = new Mat();

    if ( {


    BufferedImage image = Mat2BufferedImage(mat);
    // Mat gray = turnGray(mat);
    // MatOfRect objects = new MatOfRect();
    // CascadeClassifier cas = new CascadeClassifier();
    // cas.detectMultiScale(gray,objects);
    // Mat thresh = threash( gray);

    // BufferedImage image = Mat2BufferedImage(thresh);
    g.drawImage(image, 10, 10, image.getWidth(), image.getHeight(), null);


public Mat turnGray(Mat img)

    Mat mat1 = new Mat();
    Imgproc.cvtColor(img, mat1, Imgproc.COLOR_RGB2GRAY);
    return mat1;

public Mat threash(Mat img) {
    Mat threshed = new Mat();
    int SENSITIVITY_VALUE = 100;
    Imgproc.threshold(img, threshed, SENSITIVITY_VALUE, 255, Imgproc.THRESH_BINARY);
    return threshed;


It is worth mentioning that when I try the URL passed to the VideoCapture object in my browser, I get access to the camera feed.

My error message looks like this.

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0 at java.awt.image.SampleModel.<init>( at java.awt.image.ComponentSampleModel.<init>( at java.awt.image.PixelInterleavedSampleModel.<init>( at java.awt.image.Raster.createInterleavedRaster( at java.awt.image.Raster.createInterleavedRaster( at java.awt.image.Raster.createInterleavedRaster( at java.awt.image.ComponentColorModel.createCompatibleWritableRaster( at java.awt.image ... (more)