OpenCV imwrite saving complete black jpeg

imwrite prints on a 0 to 255 scale, but your image is in a 0 to 1 scale. To scale up, use this line:

image.convertTo(image, CV_8UC3, 255.0); 

This 'feels' like a problem with floating point numbers and integers. When your image has floating point values, the imshow() of opencv expects these values to be between 0 and 1:

http://opencv.itseez.com/modules/highgui/doc/user_interface.html?highlight=imshow#cv2.imshow

I'm not quite sure what imwrite() does with floating point images, as I could not read it here:

http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite#cv2.imwrite

Anyhow, imwrite might expect integer values between 0 and 255, and might simply cast floats to integers. In this case, almost everything is casted to 0 (i.g. 0.8 is casted to 0), hence your black images.

Try to convert your images to CV_U8CX. Alternatively, here is something I use to debug such opencv problems:

void printType(Mat &mat) {
         if(mat.depth() == CV_8U)  printf("unsigned char(%d)", mat.channels());
    else if(mat.depth() == CV_8S)  printf("signed char(%d)", mat.channels());
    else if(mat.depth() == CV_16U) printf("unsigned short(%d)", mat.channels());
    else if(mat.depth() == CV_16S) printf("signed short(%d)", mat.channels());
    else if(mat.depth() == CV_32S) printf("signed int(%d)", mat.channels());
    else if(mat.depth() == CV_32F) printf("float(%d)", mat.channels());
    else if(mat.depth() == CV_64F) printf("double(%d)", mat.channels());
    else                           printf("unknown(%d)", mat.channels());
}

void printInfo(const char *prefix, Mat &mat) {
    printf("%s: ", prefix);
    printf("dim(%d, %d)", mat.rows, mat.cols);
    printType(mat);
    printf("\n");
}

void printInfo(Mat &mat) {
    printf("dim(%d, %d)", mat.rows, mat.cols);
    printType(mat);
    printf("\n");
}

This way you can find out what your cv::Mat has in it's data-field.

PS: I did not debug your code throughly, so stay open to other probleem causes.