SoFunction
Updated on 2025-03-04

Example of using opencv to stretch images to expand resolution


//Scaling image file
#include <opencv2/>
using namespace std;
//Hide the console window
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
int main()
{
 const char *pstrImageName = "";
const char *pstrSaveImageName = "airplane zoom diagram.jpg";
const char *pstrWindowsSrcTitle = "original picture";
const char *pstrWindowsDstTitle = "Zoom diagram";

double fScale = 2;//Scaling multiple
CvSize czSize; //Target image size

//Read the image from the file
 IplImage *pSrcImage = cvLoadImage(pstrImageName, CV_LOAD_IMAGE_UNCHANGED);
 IplImage *pDstImage = NULL; 

//Calculate the target image size
  = pSrcImage->width * fScale;
  = pSrcImage->height * fScale;

//Create an image and zoom
 pDstImage = cvCreateImage(czSize, pSrcImage->depth, pSrcImage->nChannels);
 cvResize(pSrcImage, pDstImage, CV_INTER_AREA);
 

//Create a window
 cvNamedWindow(pstrWindowsSrcTitle, CV_WINDOW_AUTOSIZE);
 cvNamedWindow(pstrWindowsDstTitle, CV_WINDOW_AUTOSIZE);

//Show images in the specified window
 cvShowImage(pstrWindowsSrcTitle, pSrcImage);
 cvShowImage(pstrWindowsDstTitle, pDstImage);

//Waiting for key press event
 cvWaitKey();

//Save the picture
 cvSaveImage(pstrSaveImageName, pDstImage);

 cvDestroyWindow(pstrWindowsSrcTitle);
 cvDestroyWindow(pstrWindowsDstTitle);
 cvReleaseImage(&pSrcImage);
 cvReleaseImage(&pDstImage);
 return 0;
}