用opencv来实现图像的旋转与缩放,代码如下:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
//#include <opencv.hpp>
/*** (1). implementing Bilinear Interpolation ***/
bool BilinearInterpolation(
IplImage* pSrcImg, //@pSrcImg : input gray image
IplImage* &pDstImg, //@pDstImg : output scaled gray image
float horScale = 1., //@horScale: transformation scale in horizontal direction
float verScale = 1.) //@verScale: transformation scale in vertical direction
{
if (!pSrcImg || horScale <= 0 || verScale <= 0)
return false;
/*** YOUR CODE HERE ***///50pts
int src_width = pSrcImg->width;
int src_height = pSrcImg->height;
float dst_width = src_width / horScale;
float dst_height = src_height / verScale;
pDstImg = cvCreateImage(cvSize(dst_width, dst_height), pSrcImg->depth, pSrcImg->nChannels);
int src_step = pSrcImg->widthStep; //源存储图像一行字节数
int dst_step = pDstImg->widthStep; //目标存储图像一行字节数
unsigned char* src_data = (unsigned char*)pSrcImg->imageData; //源图像数据指针
unsigned char* dst_data = (unsigned char*)pDstImg->imageData; //目标图像数据指针
int i, i1, j, j1;
double u, u1, v, v1;
for (int y = 0; y < dst_height; y++){
float srcy1 = (float)(( y + 0.5 ) * verScale - 0.5); //对应原图像的每一个y1
j = (int)srcy1;
if (j == src_height){ //对应原图像的最后一个y2
j1 = j;
}
else {
j1 = j + 1;//对应原图像的每一个y2
}
v = double(srcy1) - j;
v1 = 1.0f- v;
for (int x = 0; x < dst_width; x++){
float srcx1 = (float)(( x + 0.5 ) * horScale - 0.5); //对应原图像的每一个x1
i = (int)srcx1;
if (i == src_width){
i1 = i; //对应原图像的最后一个x2
}
else {
i1 = i + 1; //对应原图像的每一个x2
}
u = double(srcx1) - i;
u1 = 1.0f - u;
double result = 0;
result = (u1 * v1) * src_data[j * src_step + i] + (u1 * v) * src_data[j1 * src_step + i] + (u * v1) * src_data[j * src_step + i1]+ (u * v) * src_data[j1 * src_step + i1] ;
dst_data[y * dst_step + x] = (int)result;
}
}
return true;
}
/*** (2). implementing image Rotation ***/
bool ImageRotation(
IplImage* pSrcImg, //@pSrcImg: input gray image
IplImage*& pDstImg, //@pDstImg: output rotated gray image
float angle = 45) //@angle : rotated angle
{
if (!pSrcImg || angle < 0 || angle >= 360)
return false;
/*** YOUR CODE HERE ***///50pts
angle = -angle;
double radian = (float)(angle / 180.0 * CV_PI);
double a = sin(radian), b = cos(radian);
int width = pSrcImg->width;
int height = pSrcImg->height;
int width_rotate = int(height * fabs(a) + width * fabs(b));
int height_rotate = int(width * fabs(a) + height * fabs(b));
//旋转数组map
float map[6];
Mat map_matrix = Mat(2, 3, CV_32F, map);
// 旋转中心
CvPoint2D32f center = cvPoint2D32f(width / 2, height / 2);
CvMat map_matrix2 = map_matrix;
cv2DRotationMatrix(center, angle, 1.0, &map_matrix2);
map[2] += (width_rotate - width) / 2;
map[5] += (height_rotate - height) / 2;
Mat DstImg;
//对图像做仿射变换
Mat SrcImg = cvarrToMat(pSrcImg);
warpAffine(SrcImg, DstImg, map_matrix, Size(width_rotate, height_rotate),1,0,0);
pDstImg = cvCloneImage(&IplImage(DstImg));
}
void main()
{
IplImage* pSrcImg = cvLoadImage("bicycle.bmp", 0);
IplImage* pDstImg = NULL;
cvNamedWindow("src");
cvShowImage("src", pSrcImg);
//*** Demo of bilinear interpolation ***/
BilinearInterpolation(pSrcImg, pDstImg, 0.5, 0.5);
cvNamedWindow("scaleing");
cvShowImage("scaleing", pDstImg);
cvSaveImage("Bilinear.jpg", pDstImg);
cvWaitKey(0);
cvReleaseImage(&pDstImg);
pDstImg = NULL;
/*** Demo of image rotation ***/
ImageRotation(pSrcImg, pDstImg, 60);
cvNamedWindow("rotation");
cvShowImage("rotation", pDstImg);
cvSaveImage("Rotation.jpg", pDstImg);
cvWaitKey(0);文章来源:https://www.toymoban.com/news/detail-836610.html
cvReleaseImage(&pSrcImg);
cvReleaseImage(&pDstImg);
cvDestroyAllWindows();
return;
}文章来源地址https://www.toymoban.com/news/detail-836610.html
到了这里,关于通过opencv实现图像的旋转、缩放的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!