I cannot show my final cv:Mat to ofImage

Hi, All.

My Trial is OpenCV + Image Processing with FFT2.

I cannot show my final cv:Mat to ofImage.
The imshow() and imwrite() are not work. )-:

What’s wrong to me?

My Code:

#include “ofApp.h”

using namespace cv;
using namespace ofxCv;

//--------------------------------------------------------------
void ofApp::setup(){

clock_t t1, t2;
//imgSrc.load("/Users/mun/Desktop/lena.jpg");

ofSetWindowTitle("SRSM Saliency Map, pww 190115");

cv::Mat img = cv::imread("/Users/mun/Desktop/lenaGray128.png");

//cv::cvtColor(img, img, cv::COLOR_BGRA2GRAY);
//float ratio = 128.0 / img.cols;

cv::merge(planes, 2, complexImg);
cv::dft(complexImg, complexImg, cv::DFT_INVERSE | cv::DFT_SCALE);
cv::split(complexImg, planes);
cv::magnitude(planes[0], planes[1], mag);
cv::multiply(mag, mag, mag);
cv::GaussianBlur(mag, mag, cv::Size(9,9), 2.5, 2.5);
cv::normalize(mag, mag, 255.0, 0.0, cv::NORM_MINMAX);

//cv::imwrite("/Users/mun/Desktop/salMap.bmp", mag);
//cv::imshow("Saliency Map", mag);
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

toOf(mag, salMap);
salMap.save("/Users/mun/Desktop/salMap.jpg");

cout << "Elapsed= " << elapsed_secs << "[sec]";

}

I need a help ~!

Hi, it seem that you are doing all this within the ofApp::setup() function? am I correct? if such, that’s the reason why you are not able to show anything as you need to draw the image inside the draw() function. Can you provide the full code you are using? can you simply open an image and display it? (there are several examples in the examples forlder)
are the ofxCv examples working?

No.

//--------------------------------------------------------------
void ofApp::update(){

ofBackground(255);

}

//--------------------------------------------------------------
void ofApp::draw(){

ofSetColor(255);
salMap.draw(0, 0);

}

But No Image is shown.

Yes, I succesfully showed my lena image.
But, this FFT result cv::Mat data is not shown.

Thanks.

after

toOf(mag, salMap);

call

salMap.update();

which will update the texture and make it drawable.
if it still does not work comment out all the cv processes and go enabling one by one until you find the one that is making conflicts

Thanks.
But, the same result…Not shown.

I think that it is not a update problem, but a cvMat type convert problem …

May be Converting CV_32FC to CV_8UC type problems…?

Because my saved image is also not shown …

How can I fix this ?

is the whole processing pipeline done CV_32FC ? If it is you can either downconvert to CV_8UC as you mention or use an ofFloatImage instead of an ofImage.

This is original c++(OpenCV) source.

#include <opencv2/opencv.hpp>

int main()
{
cv::Mat img = cv::imread(“test.jpg”);
cv::cvtColor(img, img, CV_BGR2GRAY);
float ratio = 128.0 / img.cols;
cv::resize(img, img, cv::Size(img.colsratio, img.rowsratio));

cv::Mat planes[] = {cv::Mat_<float>(img), cv::Mat::zeros(img.size(), CV_32F)};
cv::Mat complexImg;
cv::merge(planes, 2, complexImg);
cv::dft(complexImg, complexImg);
cv::split(complexImg, planes);

cv::Mat mag, logmag, smooth, spectralResidual;
cv::magnitude(planes[0], planes[1], mag);
cv::log(mag, logmag);
cv::boxFilter(logmag, smooth, -1, cv::Size(3,3));
cv::subtract(logmag, smooth, spectralResidual);
cv::exp(spectralResidual, spectralResidual);

planes[0] = planes[0].mul(spectralResidual) / mag;
planes[1] = planes[1].mul(spectralResidual) / mag;

cv::merge(planes, 2, complexImg);
cv::dft(complexImg, complexImg, cv::DFT_INVERSE | cv::DFT_SCALE);
cv::split(complexImg, planes);
cv::magnitude(planes[0], planes[1], mag);
cv::multiply(mag, mag, mag);
cv::GaussianBlur(mag, mag, cv::Size(9,9), 2.5, 2.5);
cv::normalize(mag, mag, 1.0, 0.0, NORM_MINMAX);

cv::imshow("Saliency Map", mag);
cv::waitKey(0);

}

It is Saliency map source using FFT2 with OpenCV.

How can I porting all code to ofxCv or ofxOpenCv source ?

Thanks in advance !

With ofFloatImage, also, not shown - -;

cv::normalize(mag, mag, 1.0, 0.0, cv::NORM_MINMAX);

clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

toOf(mag, salMapFloat);
salMapFloat.update();

salMapFloat.save("/Users/mun/Desktop/salMap.jpg");

Thanks at any rate.

Hi, I am able to see something, although I am not sure of what teh expected output should look like. Where did you took this code from? do you have an example image and its expected output?

What do you mean?

I prefer to use ofxCv over ofxOpenCv, as the former is just a thin wrapper that allows to convert back and forth from/to openCv and openFrameworks. ofxOpenCv has some classes that mix ofImage and cv::mat but i feel it a bit more complicated.
just use ofxCv's toCv and toOf functions to convert the data and thats all.

Original Github Source is here:

And, example output image is here:

It is Spectral Residual SaliencyMap of Image.

Thanks.

Thank you, but…

I don’t know ofxCv’s Complex Image processing example.
Is there any similar sources ?

similar to what? to the one you just provided?
As said, ofxCv allows you to convert back and forth from OF to openCv. So you do all the image processing with regular opencv code and once you are finished you can convert it back to an ofImage.