OpenCV imwrite saving complete black jpeg

前端 未结 3 1199
萌比男神i
萌比男神i 2021-01-04 11:26

I have done some pre processing for dft , and i am trying to save this image by imwrite.

My cropped image has this information

output.type()                


        
相关标签:
3条回答
  • 2021-01-04 11:46

    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); 
    
    0 讨论(0)
  • 2021-01-04 11:51

    A Python solution for those who come here from Google

    import numpy as np
    import cv2
    
    frame_normed = 255 * (frame - frame.min()) / (frame.max() - frame.min())
    frame_normed = np.array(frame_normed, np.int)
    
    cv2.imwrite("path/to/out/file", frame_normed)
    
    0 讨论(0)
  • 2021-01-04 11:55

    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.

    0 讨论(0)
提交回复
热议问题