Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Image zooming with OpenCV

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to achieve image zooming in OpenCV. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

In practical applications, we often need to convert an image of a certain size into an image of another size, such as zooming in or out of the image. The realization of image scaling mainly involves two key functions: cvResize and cvCreateImage.

Introduction to key functions:

1.cvResize

Void cvResize (

Const CvArr* src

CvArr* dst

Int interpolation = CV_INTER_LINEAR

);

Function description:

The first parameter represents the input image.

The second parameter represents the output image.

The third parameter specifies the interpolation method, which defaults to linear interpolation. The available interpolation methods are as follows:

CV_INTER_NN meaning: nearest neighbor interpolation

CV_INTER_LINER meaning: linear interpolation

CV_INTER_AREA meaning: regional interpolation

CV_INTER_CUBIC meaning: cubic spline interpolation

In general, we expect the mapping between the source image and the resampled target image to be as smooth as possible. The parameter interpolation is to control how the mapping is done. When the image is reduced, the pixels of the target image will be mapped to multiple parameters in the source image, and interpolation is needed. When the image is enlarged, the pixels on the target image may not be able to find the exact corresponding pixels in the source image and need to be interpolated. The simplest method is to set the pixel value of each point in the target image to the nearest pixel value in the source image, which is the algorithm used when interpolation is set to CV_INTER_NN.

Using linear interpolation algorithm (CV_INTER_LINER), the weight is calculated according to the linear weighting of four adjacent pixels in the attachment of the source image, and the weight is determined by the distance from the four pixels to the exact target point.

Regional interpolation (CV_INTER_AREA) is used to cover the original pixels with new pixels, and then calculate the average value of the coverage area.

Cubic spline interpolation (CV_INTER_CUBIC) is used to fit the 4X4 adjacent pixels of the source image attachment, and then the cubic spline value of the target pixel is taken as the value of the corresponding pixel of the target image.

2. CvCreateImage

Function prototype:

IplImage* cvCreateImage (CvSize size, intdepth, intchannels)

Function description:

The first parameter represents the size of the image

The second parameter represents the depth of the image

The third parameter represents the number of channels in the image.

The following is the program for image scaling using OpenCV.

Image_resizing.cpp

# include

# include

# include

# include

# include

Using namespace std

Int main (int argc,char * * argv)

{

Const char * pstrImageName = "Rayeager_PX2.jpg"

Const char * pstrSaveImageName = "Rayeager_PX2_Resizing.jpg"

Const char * pstrWindowsSrcTitle = "initial"

Const char * pstrWindowsDstTitle = "resizing"

Double fScale = 0.314 ram / zoom factor

CvSize czSize;// target image size

/ / read the image from the file

IplImage * pSrcImage = cvLoadImage (pstrImageName, CV_LOAD_IMAGE_UNCHANGED)

IplImage * pDstImage = NULL

/ / calculate the size of the target image

CzSize.width = pSrcImage- > width * fScale

CzSize.height = 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)

/ / display the image in the specified window

CvShowImage (pstrWindowsSrcTitle, pSrcImage)

CvShowImage (pstrWindowsDstTitle, pDstImage)

/ / wait for keystroke event

CvWaitKey ()

/ / Save the picture

CvSaveImage (pstrSaveImageName, pDstImage)

/ / destroy the window that displays the image file

CvDestroyWindow (pstrWindowsSrcTitle)

CvDestroyWindow (pstrWindowsDstTitle)

/ / release the memory allocated for the image

CvReleaseImage & pSrcImage)

CvReleaseImage & pDstImage)

Return 0

}

Makefile:

INCLUDE = $(shell pkg-config-- cflags opencv)

LIBS = $(shell pkg-config-- libs opencv)

SOURCES = image_resizing.cpp

# Target file

OBJECTS = $(SOURCES:.cpp=.o)

# executable file

TARGET = image_resizing

(TARGET): $(OBJECTS)

Gmail +-o $(TARGET) $(OBJECTS)-I $(INCLUDE) $(LIBS)

(OBJECTS): $(SOURCES)

Gmail +-c $(SOURCES)

Clean:

Rm $(OBJECTS) $(TARGET)

# compilation rule $@ represents the target file $< represents the first dependent file

%. O:%.cpp

Gmail +-I $(INCLUDE)-o $@-c $

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report