猿问

请教关于图片大小不变,怎么把图片移到中间去,图片跟框框相切?

#include"cv.h"#include"highgui.h"
#include"iostream" 
using namespace cv;using namespace std;
int main()
{
Mat image=imread("mini.jpg");
if(image.empty())
{
cout<<"read file failure"<<endl;
return -1;
}
Point2f center=Point2f(image.cols/2,image.rows/2);//旋转中心
double angle=30;//旋转角度
double scale=1;//缩放尺度
double degree=angle*CV_PI/180;
double a=sin(degree),b=cos(degree);
Mat image_rotate;
image_rotate.cols= (image.cols*fabs(b)+image.rows*fabs(a));
image_rotate.rows=(image.cols*fabs(a)+image.rows*fabs(b));
// Point2f center=Point2f(image_rotate.cols/2,image_rotate.rows/2);//也不能把图像移到中间
// float map[6];
Mat rotateMat(2,3,CV_32FC1,map);
rotateMat=getRotationMatrix2D(center,angle,scale);
// map[2]+=(image_rotate.cols-image.cols)/2;
//map[5]+=(image_rotate.rows-image.rows)/2;
Mat rotateImage;
warpAffine(image,rotateImage,rotateMat,image_rotate.size());
imwrite("rotate_image.jpg",rotateImage);
namedWindow("window",1);
imshow("window",rotateImage);
waitKey(0);
return 0;


//#include"iostream"
//#include"cv.h"
//#include"highgui.h" 
//using namespace cv;//using namespace std;
//int main()//{
//Point2f scrTri[3];
//Point2f dsttri[3];
//Mat warp_mat(2,3,CV_32FC1);
//Mat rot_mat(2,3,CV_32FC1);
//Mat scr,warp_dst,warp_rotate_dst;
//scr=imread("lena.jpg");
//warp_dst=Mat::zeros(scr.type(),scr.cols,scr.rows);//重置为0 
//}

交互式爱情
浏览 136回答 1
1回答

开心每一天1111

#include&nbsp;"StdAfx.h"#include&nbsp;"cv.h"#include&nbsp;"highgui.h"#include&nbsp;"math.h"int&nbsp;main&nbsp;(){IplImage&nbsp;*src&nbsp;=&nbsp;0;IplImage&nbsp;*dst&nbsp;=&nbsp;0;/*&nbsp;the&nbsp;first&nbsp;command&nbsp;line&nbsp;parameter&nbsp;must&nbsp;be&nbsp;image&nbsp;file&nbsp;name&nbsp;*/if&nbsp;(&nbsp;(src&nbsp;=&nbsp;cvLoadImage&nbsp;("d:\\Image\\mini.png",&nbsp;-1))&nbsp;!=&nbsp;0){int&nbsp;delta&nbsp;=&nbsp;1;int&nbsp;angle&nbsp;=&nbsp;0;int&nbsp;opt&nbsp;=&nbsp;0;//&nbsp;1:&nbsp;旋转加缩放//&nbsp;0:&nbsp; 仅仅旋转double&nbsp;factor;dst&nbsp;=&nbsp;cvCloneImage&nbsp;(src);cvNamedWindow&nbsp;("src",&nbsp;1);cvShowImage&nbsp;("src",&nbsp;src);for&nbsp;(;;){float&nbsp;m[6];//&nbsp;Matrix&nbsp;m&nbsp;looks&nbsp;like:////&nbsp;[&nbsp;m0&nbsp; m1&nbsp; m2&nbsp;]&nbsp;===>&nbsp; [&nbsp;A11&nbsp; A12&nbsp; &nbsp;b1&nbsp;]//&nbsp;[&nbsp;m3&nbsp; m4&nbsp; m5&nbsp;]&nbsp; &nbsp; &nbsp; &nbsp;[&nbsp;A21&nbsp; A22&nbsp; &nbsp;b2&nbsp;]//CvMat&nbsp;M&nbsp;=&nbsp;cvMat&nbsp;(2,&nbsp;3,&nbsp;CV_32F,&nbsp;m);int&nbsp;w&nbsp;=&nbsp;src->width;int&nbsp;h&nbsp;=&nbsp;src->height;if&nbsp;(opt)//&nbsp;旋转加缩放factor&nbsp;=&nbsp;(cos&nbsp;(angle&nbsp;*&nbsp;CV_PI&nbsp;/&nbsp;180.)&nbsp;+&nbsp;1.0)&nbsp;*&nbsp;2;else//&nbsp; 仅仅旋转factor&nbsp;=&nbsp;1;m[0]&nbsp;=&nbsp;(float)&nbsp;(factor&nbsp;*&nbsp;cos&nbsp;(-angle&nbsp;*&nbsp;2&nbsp;*&nbsp;CV_PI&nbsp;/&nbsp;180.));m[1]&nbsp;=&nbsp;(float)&nbsp;(factor&nbsp;*&nbsp;sin&nbsp;(-angle&nbsp;*&nbsp;2&nbsp;*&nbsp;CV_PI&nbsp;/&nbsp;180.));m[3]&nbsp;=&nbsp;-m[1];m[4]&nbsp;=&nbsp;m[0];//&nbsp;将旋转中心移至图像中间m[2]&nbsp;=&nbsp;w&nbsp;*&nbsp;0.5f;m[5]&nbsp;=&nbsp;h&nbsp;*&nbsp;0.5f;//&nbsp; dst(x,y)&nbsp;=&nbsp;A&nbsp;*&nbsp;src(x,y)&nbsp;+&nbsp;bcvZero&nbsp;(dst);cvGetQuadrangleSubPix&nbsp;(src,&nbsp;dst,&nbsp;&M);cvNamedWindow&nbsp;("dst",&nbsp;1);cvShowImage&nbsp;("dst",&nbsp;dst);if&nbsp;(cvWaitKey&nbsp;(1)&nbsp;==&nbsp;27)//ESCbreak;angle&nbsp;=&nbsp;(int)&nbsp;(angle&nbsp;+&nbsp;delta)&nbsp;%&nbsp;360;}//&nbsp;for-loop}return&nbsp;0;}参考程序如上所述,如果原始图像大小不变是无法输出到原始图像的。比如你原始图像为200*200,旋转45度,输出到一个200*200的大小的图像空间里,这个原始图片的大小已经是原来的一半成了&nbsp;根2*100*根2*100,你要保证角点相切的话,这个原始图像的大小需要根据旋转角度的大小随时改变。比如旋转45度,先把源图像变到100*100(我说的源图像是里面有内容的图像),而你处理的图像还是200*200的带着黑框的图片。进行旋转之后就是切边缘了。
随时随地看视频慕课网APP
我要回答