本文介绍了OpenCV写保存完成黑色jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对dft做了一些预处理,我想通过imwrite保存这个图像。



我裁剪的图片包含此信息

  output.type 5 

output.channels()1

output.depth()5

但每次当我保存它给出黑色输出。我检查了旧的现有线程stackoverflow,但似乎不工作为我。
例如



我已经尝试过许多颜色转换和深度转换,但我不知道为什么它不工作。

  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); //我试过这个太
cv :: imwrite(dft1.jpg,out1,qualityType); //尝试甚至使用质量类型

imshow显示此图片只有问题出现时,我保存它。 / p>

请帮助



也许我的dft类有问题,因为我每当我使用dft函数那么输出只能与inshow一起工作,但保存不起作用。

  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);
//创建一个高斯高通滤波器5%的傅立叶变换的高度
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 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 cutoff InPixels)
{
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 {
for(int v = 0; 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));

/ **将矩形放在相对于右上角的中间默认过滤器上** /
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);

/ **将矩形放在相对于左上角的中间默认过滤器上** /
iMainright =(size.width * 0.402);
Quad = ghpf(Rect(300,iMainTop,300,iMainBottom));
Mat qTopLeft(ghpf,Rect(0,0,300,iMainBottom));
Quad.copyTo(qTopLeft);


/ **将矩形放在相对于左下角的中间默认过滤器上** /
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);

/ **将矩形放在相对于右下角的中间默认过滤器上** /
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);


//删除中矩形[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;
填充

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


返回complexImg;
}

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

填充
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);

//裁剪谱图,如果它有奇数行或列
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

我希望你们现在可以帮助我更好...也许从复杂的过滤器转换后我失去了一些点? ?

解决方案

这种感觉像浮点数和整数的问题。当图像有浮点值时,opencv的imshow()期望这些值在0和1之间:





我不太确定什么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(\\\
);
}

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

这种方式你可以找到你的cv :: Mat在它的数据-field。



PS:我没有调试你的代码,所以保持开放其他问题的原因。


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()           5

output.channels()       1

output.depth()          5

But everytime when i save it gives black output. I have checked old existing threads of stackoverflow but all seems not working for me.e.g.OpenCV2.3 imwrite saves black image

I have tried many color conversions and depth conversion as well but i dont know why it does not work.

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 display this image fine only problem comes when i save it.

please help

[EDIT] Maybe my dft class that i made has problem because i whenever i use dft function then the output can only work with inshow but for save it does not work.

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

The output that i am trying to save is from

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

here the matrix xyz has these values

output.type()           5

output.channels()       1

output.depth()          5

I hope you guys now can help me more better... maybe after converion from complex filter i am losing some points????

解决方案

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.

这篇关于OpenCV写保存完成黑色jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:17