OpenCV imwrite保存完全黑色的jpeg图片

12

我已经对离散傅里叶变换进行了一些预处理,并尝试通过imwrite保存这张图片。

我的裁剪后的图片包含以下信息。

output.type()           5   

output.channels()       1

output.depth()          5

但每次保存时都会出现黑色输出。我已经检查了stackoverflow上旧的现有线程,但似乎都不适用于我。 例如: OpenCV2.3 imwrite saves black image

我尝试了许多颜色转换和深度转换,但我不知道为什么它不起作用。

std::vector<int> qualityType;
qualityType.push_back(CV_IMWRITE_JPEG_QUALITY);
qualityType.push_back(90);

Mat out1,out2;

cv::cvtColor(output, out1, CV_GRAY2BGR);
//out1.convertTo(out2,CV_8U,1./256); // i tried this too 
cv::imwrite("dft1.jpg",out1,qualityType); // tried even using quality type

imshow可以正常显示图像,但是保存时出现问题。请帮忙。

[编辑]也许是我自己实现的dft类出了问题,因为每次使用dft函数时,输出只能在imshow中工作,但无法保存。

CDftRidgeAnalyses::CDftRidgeAnalyses(void)
{
}

CDftRidgeAnalyses::~CDftRidgeAnalyses(void)
{
}


Mat CDftRidgeAnalyses::GetRidgeAnalyses(Mat inpGray)
{
    Mat img = inpGray;
    int WidthPadded=0,HeightPadded=0;
    WidthPadded=img.cols*2;
    HeightPadded=img.rows*2;
    int M = getOptimalDFTSize( img.rows );
    //Create a Gaussian Highpass filter 5% the height of the Fourier transform
    double db  = 0.05 * HeightPadded;

    Mat fft = ForierTransform(img.clone(),HeightPadded,WidthPadded);

    Mat ghpf = CreateGaussianHighPassFilter(Size(WidthPadded, HeightPadded), db);
    Mat res;

    cv::mulSpectrums(fft,ghpf,res,DFT_COMPLEX_OUTPUT);

    Mat mag  = GetDftToImage(res,img);

    int cx = mag.cols/2;
    int cy = mag.rows/2;
    cv::Mat croped = mag(cv::Rect(0,0,cx, cy));

    cv::threshold(mag, mag, 0.019, 1, cv::THRESH_BINARY);

    Mat bgr;
    cvtColor(mag,bgr,CV_GRAY2RGB);

    //imshow("XXX",bgr);
    //imshow("croped", croped);
    //imshow("img",img);
    //
    //cv::waitKey();
    return croped;
}




Mat CDftRidgeAnalyses::ForierTransform(Mat inpGray,int M,int N)
{
    Mat img = inpGray;
    int i = img.channels();

    Mat padded;
    Mat img2;
    img.convertTo(img2,CV_64F,1./255);

    copyMakeBorder(img2, padded, 0, M - img2.rows, 0, N - img2.cols, BORDER_CONSTANT, Scalar::all(0));

    Mat element1 = Mat_<float>(padded);
    Mat element2 = Mat::zeros(padded.size(), CV_32F);
    Mat planes[] = {element1, element2};
    Mat complexImg;

    merge(planes, 2, complexImg);

    dft(complexImg, complexImg ,0, img.rows);

    //printMat(complexImg);

    return complexImg;
}



double CDftRidgeAnalyses::pixelDistance(double u, double v)
{
    return cv::sqrt(u*u + v*v);
}

double CDftRidgeAnalyses::gaussianCoeff(double u, double v, double d0)
{
    double d = pixelDistance(u, v);
    return 1.0 - cv::exp((-d*d) / (2*d0*d0));
}

cv::Mat CDftRidgeAnalyses::CreateGaussianHighPassFilter(cv::Size size, double cutoffInPixels)
{
    Mat ghpf(size, CV_32F);

    cv::Point center2((size.width*0.80), size.width/2);
    //cv::Point center2(0,0);

    for(int u = 0; u < ghpf.rows; u++)
    {
        for(int v = 0; v < ghpf.cols; v++)
        {
            ghpf.at<float>(u, v) = gaussianCoeff(u - center2.x, v - center2.y, cutoffInPixels);
        }
    }

    Mat bmp;

    int channels = ghpf.channels();
    int type = ghpf.type();
    int depth = ghpf.depth();

    cv::cvtColor(ghpf,bmp,CV_GRAY2RGB); 
    cv::cvtColor(ghpf,bmp,CV_GRAY2BGRA); 
    //imshow("XXX",bmp);
    int cx = ghpf.cols/2;
    int cy = ghpf.rows/2;
    Mat tmp;
    int iExactright =  (size.width*0.59);
    int iExactbottom = (size.height*0.86);
    //full   Mat q0(ghpf, Rect(69,10,400,290));

//  Mat whiteq(ghpf, Rect(0,390,270,330));

    int iMainleft=0, iMainright=0;
    int iMainBottom=0,iMainTop=0;


    Mat Quad;
    Mat ql(ghpf, Rect(190,0,270,330));

    /** Make the rectangle on middle default filter with respect to top right angle**/
    iMainleft=(size.width*0.111);
    iMainright=(size.width*0.402);
    iMainTop=(size.height*0.484);
    iMainBottom = (size.height*0.155);
    Quad = ghpf(Rect(iMainleft,iMainTop,iMainright+6,iMainBottom));
    Mat qTopRight(ghpf, Rect(iExactright,0, iMainright+6, iMainBottom));
    Quad.copyTo(qTopRight);

    /** Make the rectangle on middle default filter with respect to top left angle**/
    iMainright=(size.width*0.402);
    Quad = ghpf(Rect(300,iMainTop,300,iMainBottom));
    Mat qTopLeft(ghpf, Rect(0,0, 300, iMainBottom));
    Quad.copyTo(qTopLeft);


    /** Make the rectangle on middle default filter with respect to bottom left angle**/
    iMainTop = iMainTop-iMainBottom;
    iExactbottom = size.height - iMainBottom;
    Quad = ghpf(Rect(300,iMainTop,300,iMainBottom));
    Mat qBottomLeft(ghpf, Rect(0,iExactbottom, 300, iMainBottom));
    Quad.copyTo(qBottomLeft);

    /** Make the rectangle on middle default filter with respect to bottom right angle**/
    iMainleft=(size.width*0.111);
    iMainright=(size.width*0.402);

    Quad = ghpf(Rect(iMainleft,iMainTop,iMainright+6,iMainBottom));
    Mat qBottomRight(ghpf, Rect(iExactright,iExactbottom, iMainright+6, iMainBottom));
    Quad.copyTo(qBottomRight);


    // remove middle rectangle [ circle ] 
    iMainright=(size.width*0.402);
    Quad = ghpf(Rect(0,iMainTop+iMainTop,size.width,iMainBottom+iMainBottom-130));
    Mat qMiddle(ghpf,Rect(0,iMainTop+150,size.width,iMainBottom+iMainBottom-130));
    Quad.copyTo(qMiddle);
    qMiddle =ghpf(Rect(0,iMainTop-10,size.width,iMainBottom+iMainBottom-130));
    Quad.copyTo(qMiddle);

    normalize(ghpf, ghpf, 0, 1, CV_MINMAX);

    /*Mat x;
    cv::resize(ghpf,x,cv::Size(400,700));
    imshow("fftXhighpass2", x);*/
    Filter = ghpf;
    Mat padded;

    copyMakeBorder(ghpf, padded, 0, size.height - ghpf.rows, 0, size.width - ghpf.cols, BORDER_CONSTANT, Scalar::all(0));

    Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
    Mat complexImg;

    merge(planes, 2, complexImg);


    return complexImg;
}

Mat CDftRidgeAnalyses::GetDftToImage(Mat res,Mat orgImage)
{
    idft(res,res,DFT_COMPLEX_OUTPUT,orgImage.rows);

    Mat padded;
    copyMakeBorder(orgImage, padded, 0,orgImage.rows, 0, orgImage.cols, BORDER_CONSTANT, Scalar::all(0));
    Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
    split(res, planes);
    magnitude(planes[0], planes[1], planes[0]);
    Mat mag = planes[0];
    mag += Scalar::all(1);
  //  log(mag, mag);

    // crop the spectrum, if it has an odd number of rows or columns
    mag = mag(Rect(0, 0, mag.cols & -2, mag.rows & -2));

    normalize(mag, mag, 1, 0, CV_MINMAX);

    return mag;
}

我试图保存的输出来自于:

Mat org = imread("4.png",CV_LOAD_IMAGE_GRAYSCALE);
Mat re;
resize(org,re,cv::Size(311,519));
Mat xyz = CDftRidgeAnalyses::GetRidgeAnalyses(re);
cv::imwrite("dft1.jpg",xyz);

这里矩阵 XYZ 具有以下数值

output.type()           5   

output.channels()       1

output.depth()          5

我希望你们现在能够更好地帮助我...也许在从复杂过滤器转换后,我会丢失一些重要点数?


一个初步的备注:在你的示例代码中,你试图编写一个bmp文件,同时提供jpeg参数。你先检查了吗? - sansuiso
我甚至在不使用参数的情况下也检查了bmp,之前我是用jpg进行检查,但两者都没有起作用。是的,我忘记删除质量类型了,但这不是问题所在。当我保存图像时,它仍然始终是黑色的。 - wolvorinePk
1
如果您能说明.type()和.depth()的整数值代表什么,那将会很有帮助。它是一个浮点图像(取值范围为0-1)吗?如果是,您是否在保存之前应用了适当的缩放? - Sammy
1
我尝试了与你完全相同的代码,只是我从我的磁盘读取文件,然后使用你上面的示例进行写入。它在我的Mac OS上使用opencv 2.4非常顺利。你应该再次检查你的输入图像(类型、值等)。 - sansuiso
你的深度是16位浮点数。imwrite只使用8位,除了jpeg2000文件。 - Bobbi Bennett
显示剩余2条评论
3个回答

24

imwrite 输出的数据范围是 0 到 255,但你的图像数据范围是 0 到 1。如果要进行放大,请使用以下代码:

image.convertTo(image, CV_8UC3, 255.0); 

这个 convertTo 函数是什么?我尝试运行它,但它抛出了一个 AttributeError: 'numpy.ndarray' object has no attribute 'convertTo' 的错误。 - Reine_Ran_
哦,糟糕,抱歉我刚意识到这不是用于Python的。 - Reine_Ran_

10
这似乎是与浮点数和整数有关的问题。当图像具有浮点值时,opencv的imshow()函数期望这些值在0和1之间:

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

我不太确定imwrite()对浮点图像的处理方式,因为我在这里没有找到相关信息:

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

无论如何,imwrite 可能希望传入介于 0 和 255 之间的整数值,并可能只是将浮点数强制转换为整数。在这种情况下,几乎所有内容都会被转换为 0(例如,0.8 被转换为 0),因此您得到黑色图像。

尝试将您的图像转换为 CV_U8CX。或者,这是我用来调试此类 opencv 问题的东西:

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");
}

这样您就可以查看cv::Mat数据字段中包含的内容。

PS: 我没有彻底调试您的代码,因此请保持开放心态以寻找其他问题原因。


7

以下是一个适用于通过Google搜索到该页面的用户的Python解决方案

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接