Interpolação bicúbica usando openCV
-
09-12-2019 - |
Pergunta
Quero realizar um redimensionamento de imagem usando interpolação bicúbica.No entanto, minha imagem de saída, new_img, parece exatamente igual à minha imagem original carregada, img.Além disso, quando encontrei a largura e a altura de new_img, ele tem as mesmas dimensões da minha imagem original.Achei que a imagem de destino deveria ter sido redimensionada.Este é o meu código.Alguém poderia ver se eu fiz algo impreciso, por favor?Agradeço antecipadamente.
#include "cv.h"
#include "highgui.h"
#include "iostream"
using namespace std;
int main( int argc, char* argv ) {
IplImage* img = NULL;
const int maxScale = 1;
img = cvLoadImage("C:\\walk mii.jpg");
if (!img){return -1;}
for(int s = 1; s <= maxScale; s++)
{
IplImage* new_img = img;
if( s > 1 ) {
new_img = cvCreateImage(cvSize(img->width*s,img->height*s), img->depth, img->nChannels );
cvResize( img, new_img, CV_INTER_CUBIC );}
cvNamedWindow( "original", CV_WINDOW_AUTOSIZE );
cvShowImage( "original", img);
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
cvShowImage( "result", new_img);
CvSize dim = cvGetSize(new_img);
cout <<" dimensions:: height:" << dim.height<<" width:"<< dim.width<< endl;
cvWaitKey(0);
cvReleaseImage( &img );
cvReleaseImage( &new_img );
cvDestroyWindow( "result" );
return 0;
}
}
Código alterado:
#include "cv.h"
#include "highgui.h"
#include "iostream"
using namespace std;
int main( int argc, char* argv ) {
IplImage* img = NULL;
IplImage* new_img = NULL;
img = cvLoadImage("C:\\walk mii.jpg");
if (!img){
return -1;}
new_img = cvCreateImage(cvSize(img->width,img->height), img->depth, img->nChannels );
cvResize( img, new_img, CV_INTER_CUBIC );
cvNamedWindow( "original", CV_WINDOW_AUTOSIZE );
cvShowImage( "original", img);
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
cvShowImage( "result", new_img);
CvSize dim = cvGetSize(new_img);
cout <<" dimensions:: height:" << dim.height<<" width:"<< dim.width<< endl;
cvWaitKey(0);
cvReleaseImage( &img );
cvReleaseImage( &new_img );
cvDestroyWindow( "result" );
return 0;
}
Solução
IplImage* new_img = img;
faz uma cópia superficial, então new_img e img apontam para os mesmos dados
Outras dicas
Você precisa escalar..você tem 2 maneiras (apenas aparecendo a escala para Altura):
1)
Mat testIn(inputRows, inputCols, CV_32F);
Mat testOut(inputRows*2, inputCols, CV_32F);
Size sz = testOut.size();
resize(testIn, testOut, sz, 0.0f, 0.0f, INTER_CUBIC);
então o openCV calcula automaticamente a proporção entre testIn e testOut usando o "sz" (exemplo:O fator "Y" é sz.height / testIn.Height)
2)
Mat testIn(inputRows, inputCols, CV_32F);
Mat testOut(inputRows*2, inputCols, CV_32F);
Size sz = testOut.size();
resize(testIn, testOut, sz, 2.0f, 0.0f, INTER_CUBIC);
então openCV usa o parâmetro "2.0f" como proporção (para "Y"), mas aqui você deve garantir que testOut tenha tamanho suficiente para tal proporção