OpenCV: Change depth and log transform

nkint picture nkint · Jul 16, 2012 · Viewed 12.2k times · Source

I'm trying to learn OpenCV and I want to apply this simple formula: enter image description here

That is a per-pixel operation and transform the pixel from r to s.

This is my code:

int main(int /*argc*/, char** /*argv*/) {

    Mat _img = imread("lena.jpg"); 
    cvtColor(_img, img, CV_32F);

    cout << "original image size: " << img.rows << " " << img.cols << endl;
    cout << "original type: " << img.type() << endl;
    cout << "original depth: " << img.depth() << endl;

    Mat _src = img.clone();
    _src += 1;
    log(_src, logContrast);
    logContrast *= log_c;

    /// ...

And I get this error:

OpenCV Error: Assertion failed (depth == CV_32F || depth == CV_64F) in log, file /home/user/Documents/Code/OpenCV-2.3.1/modules/core/src/mathfuncs.cpp, line 1772
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/alberto/Documents/Code/OpenCV-2.3.1/modules/core/src/mathfuncs.cpp:1772: error: (-215) depth == CV_32F || depth == CV_64F in function log

I tried with gray and coloured image, and with cvtColor with CV_8U, CV_32F and C1, C3, but I cout always 0 as depth..

I've spent hours on OpenCV manuals and on OpenCV-2.3.1/modules/core/include/opencv2/core/ types_c.h but.. I can't find a solution. I think I've done some confusion on how transform depth of a Mat object.

Answer

Abid Rahman K picture Abid Rahman K · Jul 16, 2012

are you sure you converted the image to float32 ?

Please check out the code below, i just wrote. Don't know if output is correct, but it didn't throw any errors.

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;

int main ()
{
    cv::Mat binary = cv::imread("kick.jpg",0);

    cv::Mat fg;
    binary.convertTo(fg,CV_32F);
    fg = fg + 1;
    cv::log(fg,fg);
    cv::convertScaleAbs(fg,fg);
    cv::normalize(fg,fg,0,255,cv::NORM_MINMAX);
    cv::imshow("a",fg);
cv::waitKey(0);
}

And below is the output i got :

enter image description here