Ask Your Question

flynnt's profile - activity

2014-03-15 12:24:39 -0600 commented answer to cpp

That's why I chose to start from an existing example that is provided in the examples directory by OpenCV. It uses a known, existing image (also provided in the OpenCV example source) that when you run the python script on it, displays a very clear and accurate looking image. I'm trying to figure out the c++ equivalent of that python script. I'll eventually want this running as a part of a larger c++ program. I know I'll have to learn how to tweak things so this will work for images I take myself, but for now, I'd be happy if I could get the same result in c++ that i can get via python with the same image and parameters.

2014-03-08 13:54:02 -0600 commented answer to cpp

well, that's closer. What I get now is a mostly dark gray image with a few white spots. Those white spots seem to be where the plant is closest to the viewer.

2014-02-24 01:37:51 -0600 received badge  Student (source)
2014-02-22 20:50:45 -0600 asked a question to cpp

Hi there. I'd like to generate a disparity map from a stereo image in C++. The C++ sample is a bit large and hard to strip out what I really want. The Python version was easier to strip down. Here is my stripped down python version:

#!/usr/bin/env python

if __name__ == '__main__':
    print 'loading images...'
    imgL = cv2.pyrDown( cv2.imread('aloeL.jpg') )  # downscale images for faster processing
    imgR = cv2.pyrDown( cv2.imread('aloeR.jpg') )

    # disparity range is tuned for 'aloe' image pair
    window_size = 3
    min_disp = 16
    num_disp = 112-min_disp
    stereo = cv2.StereoSGBM(minDisparity = min_disp,
        numDisparities = num_disp,
        SADWindowSize = window_size,
        uniquenessRatio = 10,
        speckleWindowSize = 100,
        speckleRange = 32,
        disp12MaxDiff = 1,
        P1 = 8*3*window_size**2,
        P2 = 32*3*window_size**2,
        fullDP = False

    print 'computing disparity...'
    disp = stereo.compute(imgL, imgR).astype(np.float32) / 16.0

    cv2.imshow('left', imgL)
    cv2.imshow('disparity', (disp-min_disp)/num_disp)

By comparing to the cpp sample, I converted the above python script to cpp. I think I did it right, but am having trouble getting data within range to view the result. I need a second set of eyes on this code. What did I screw up? Here's my cpp code:

void bringWithinRange(void *inptr, void *outptr, int width, int height, int min, int num)
    int *iptr = (int *)inptr;
    float *fptr = (float *)outptr;

    for(int i=0; i<width*height; i++)
        float value = (float)iptr[i];
        float newvalue = ((value/16.0f) - min) / num;
        fptr[i] = newvalue;

int main(int argc, char *argv[]) 
    int window_size = 3;
    int min_disp = 16;
    int num_disp = 112-min_disp;

    StereoSGBM sgbm = StereoSGBM(
         min_disp, // min_disp
         num_disp, // num_disp
         window_size, // SADWindowSize
         8*3*window_size*window_size, // P1
         32*3*window_size*window_size, // P2
         1, // disp12MaxDiff
         10, // uniquenessRatio
         100, // speckleWindowSize
         32, // speckleRange
         false // fullDP

    Mat left = imread("aloeL.jpg", -1);
    Mat right = imread("aloeR.jpg", -1);
    Mat disparity, disparity8, disparity32;

    sgbm(left, right, disparity);
    disparity.convertTo(disparity32, CV_32F);
    bringWithinRange(disparity.ptr(), disparity32.ptr(), disparity.cols, disparity.rows, min_disp, num_disp);

    namedWindow("Disparity", 0);
    imshow("disparity", disparity32);

    return 0;