Ask Your Question

dejnus's profile - activity

2018-04-26 13:58:22 -0600 received badge  Famous Question (source)
2017-07-14 08:35:13 -0600 received badge  Notable Question (source)
2017-04-24 07:57:01 -0600 received badge  Popular Question (source)
2016-07-20 06:56:57 -0600 commented question Opencv 3.1 capture from multiple cameras.

I just took your advice and seems like it was the problem, I've tried every possible combination plugging it in the back of my pc and finally it worked!

2016-07-19 12:17:11 -0600 asked a question Opencv 3.1 capture from multiple cameras.

I'm using 2 Logitech C170 usb webcams connected to USB 3.0 ports on my front panel on Windows 10 Opencv 3.1 Visual Studio 2015. I have no problem getting image from one camera at a time either 0 or 1 but while I'm trying to get both at the same time i get a message:

R doesn't work

in console which is linked to second camera not working in the same time, followed by error:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow

Code I'm using:

#include <opencv2\opencv.hpp>
#include <opencv\highgui.h>

using namespace cv;
using namespace std;

int main()
{
    VideoCapture captureL(0);
    if (!captureL.isOpened()) cout << "L doesn't work" << endl;
    VideoCapture captureR(1);
    if (!captureR.isOpened()) cout << "R doesn't work" << endl;

    Mat imageL, imageR;

    while (1)
    {
        captureL >> imageL;
        captureR >> imageR;
        imshow("OriginalL", imageL);
        imshow("OriginalR", imageR);
        waitKey(1);
    }
captureL.release();
captureR.release();
return 0;
}

Is there any way to fix that? It must be possible to get multiple captures at once. Maybe I'm doing something wrong since it is the newest Opencv 3.1.

2016-07-17 11:12:57 -0600 received badge  Enthusiast
2016-07-04 18:42:45 -0600 asked a question How to easily get a depth map in opencv python?

So for starters, I'm using a raspberry Pi B with 2 Logitech C170 webcams this is a little project I'm trying to do for weeks and can't seem to get a good result. Here is what I came up to while reading plenty of tutorials and sample codes(it is a combination of calibrate.py and stereo_match.py from python samples which works perfectly fine):

from __future__ import print_function

import numpy as np
import cv2

from common import splitfn

import os

if __name__ == '__main__':
    import sys
    import getopt
    from glob import glob

    img_mask = '../../project/left*.png'
    img_mask2 = '../../project/right*.png'

    img_names = glob(img_mask)
    img_names2 = glob(img_mask2)
    square_size = 2.3
    pattern_size = (9, 6)
    pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
    pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)
    pattern_points *= square_size

    obj_points = []
    obj_points2 = []
    img_points = []
    img_points2 = []
    h, w = 0, 0
    img_names_undistort = []

    #calibrate1

    for fn in img_names:
        print('processing %s... ' % fn, end='')
        img = cv2.imread(fn, 0)
        if img is None:
            print("Failed to load", fn)
            continue

        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
            cv2.drawChessboardCorners(img, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            outfile = './output/' + name + '_chess.png'
            cv2.imwrite(outfile, img)
            if found:
                img_names_undistort.append(outfile)

        if not found:
            print('chessboard not found')
            continue

        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)

        print('ok')

    #calibrate2

    for fn in img_names2:
        print('processing %s... ' % fn, end='')
        img = cv2.imread(fn, 0)
        if img is None:
            print("Failed to load", fn)
            continue

        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
            cv2.drawChessboardCorners(img, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            outfile = './output/' + name + '_chess.png'
            cv2.imwrite(outfile, img)
            if found:
                img_names_undistort.append(outfile)

        if not found:
            print('chessboard not found')
            continue

        img_points2.append(corners.reshape(-1, 2))
        obj_points2.append(pattern_points)

        print('ok')

    rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None)
    print('calib1done\n')

    rms2, camera_matrix2, dist_coefs2, rvecs2, tvecs2 = cv2.calibrateCamera(obj_points2, img_points2, (w, h), None, None)
    print('calib2done\n')

    print('\nFirst camera:\n')
    print("RMS:\n", rms)
    print("camera matrix:\n", camera_matrix)
    print("distortion coefficients: ", dist_coefs.ravel())
    print('\nSecond camera:\n')
    print("RMS:\n", rms2)
    print("camera matrix:\n", camera_matrix2)
    print("distortion coefficients: ", dist_coefs2.ravel())

    #depth map

    print('loading images...')
    imgL = cv2.imread('left0.png')
    imgR = cv2.imread('right0.png')

    newcameramtx, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h))
    newcameramtx2, roi2 = cv2.getOptimalNewCameraMatrix(camera_matrix2, dist_coefs2, (w, h), 1, (w, h))

    newimgL = cv2.undistort(imgL, camera_matrix, dist_coefs, None, newcameramtx)
    newimgR = cv2.undistort(imgR, camera_matrix2, dist_coefs2, None, newcameramtx2)
    newimgL = cv2.cvtColor(newimgL, cv2.COLOR_BGR2GRAY)
    newimgR = cv2.cvtColor(newimgR, cv2.COLOR_BGR2GRAY)
    cv2.imwrite("undistortedleft0.png", newimgL)
    cv2.imwrite("undistortedright0.png", newimgR)

    window_size = 3
    min_disp = 16
    num_disp = 112-min_disp
    stereo = cv2.StereoSGBM_create(minDisparity = min_disp,
        numDisparities = num_disp,
        blockSize = 16,
        P1 = 8*1*window_size**2 ...
(more)