开发平台(Platform): (Ex: Win10, Linux, ...)
W10
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
Visual studio 2010
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
Opencv ver2.4.9
问题(Question):
无法正确执行结果
喂入的资料(Input):
任意jpg图片
预期的正确结果(Expected Output):
可以输入角度
正确输出旋转后的图片
错误结果(Wrong Output):
显示内存违规,不知道那里下手
程式码(Code):(请善用置底文网页, 记得排版)
#include " highgui.h"
#include <cv.h>
#include <math.h>
#include <stdio.h>
#include <Windows.h>
using namespace std;
void tran( unsigned char * frame_in, unsigned char * frame_out, int height, int width,int degree)
{
int x1,y1,i,j;
double pi=3.1415926;
double angle=degree*pi/180;
int x,y,z;
for(x=0; x<width; x++)
{
for(y=0;y<height; y++)
{
for(z=0;z<3;z++)
{
x1=i*cos(angle)-j*sin(angle);
y1=j*cos(angle)+i*sin(angle);
if((x1>=0)&&(x1<width)&&(y1>=0)&&(y1<height))
{
frame_out[(y*width+x)*3+z] = frame_in[(x1*width+y1)*3+z];
}else{
frame_out[(y*width+i)*3+z] = 0;
}
}
}
}
}
int main()
{
//
int degree;
printf("choose the degree to rotate the picture\n");
scanf("%d",°ree);
//
IplImage *Image1 ;
Image1=cvLoadImage("lena.jpg",1);
int height, width;
height = Image1->height;
width = Image1->width;
cvNamedWindow("Original Image", CV_WINDOW_AUTOSIZE);
cvShowImage("Original Image",Image1);
unsigned char * frame_in;
unsigned char * frame_out;
frame_in = (unsigned char *)malloc(height*width*3*sizeof(unsigned char));
frame_out = (unsigned char *)malloc(height*width*3*sizeof(unsigned char));
/* Load Image to frame_in */
for(int i=0 ; i<height*width*3 ; i++)
{
frame_in[i] = Image1->imageData[i];
}
tran(frame_in, frame_out, height, width, degree); //转置
/* 从frame_out存回Image1 */
for(int i=0 ; i<height*width*3 ; i++)
{
Image1->imageData[i] = frame_out[i];
}
cvNamedWindow("Result", CV_WINDOW_AUTOSIZE);
cvShowImage("Result",Image1);
cvWaitKey(0);
free(frame_in);
free(frame_out);
return 0;
}
补充说明(Supplement):
暂时无,有会在下方回复,谢谢此版
你 转置 function 中, i,j 似乎没有给初始值就拿来用了, 这样你 x1 y1 的给array用可能会有问题你循环是用x,y 看不出来 i j是哪里来的因为你 tran(...) 里面有问题呀还有像你有一个 if理面是frame_out[(y*width+x)*3+z] = fram_in[...]到了 else 中是frame_out[(y*width+i)*3+z] = 0x 怎么会变成 i 了如果你是指 frame_in[i] = Image1->imageData[i]存不进去 你要确定 Image1->imageData[] 的大小有到hight*width*3 呀 话说为什么要*3我觉得 Image1->imageData[] 的大小应该只会有长X宽