[問題] opencv 搭配 C 寫影像任意角度旋轉
開發平台(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):
暫時無,有會在下方回覆,謝謝此版
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 27.247.13.49
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1497497828.A.5F6.html
推
06/15 13:13, , 1F
06/15 13:13, 1F
→
06/15 13:14, , 2F
06/15 13:14, 2F
→
06/15 13:16, , 3F
06/15 13:16, 3F
目前顯示的問題是卡在長寬那塊 數字無法存入array
※ 編輯: amateuruser (140.124.249.51), 06/15/2017 15:02:16
推
06/15 15:05, , 4F
06/15 15:05, 4F
→
06/15 15:06, , 5F
06/15 15:06, 5F
→
06/15 15:07, , 6F
06/15 15:07, 6F
→
06/15 15:07, , 7F
06/15 15:07, 7F
→
06/15 15:08, , 8F
06/15 15:08, 8F
→
06/15 15:08, , 9F
06/15 15:08, 9F
→
06/15 15:10, , 10F
06/15 15:10, 10F
→
06/15 15:11, , 11F
06/15 15:11, 11F
→
06/15 15:12, , 12F
06/15 15:12, 12F
→
06/15 15:13, , 13F
06/15 15:13, 13F
推
06/15 17:16, , 14F
06/15 17:16, 14F
→
06/15 17:18, , 15F
06/15 17:18, 15F
→
06/15 17:20, , 16F
06/15 17:20, 16F
→
06/15 17:21, , 17F
06/15 17:21, 17F
我是存粹要練習 因為有考慮以這個程式碼為依據 做基本影像處理
※ 編輯: amateuruser (140.124.249.31), 06/15/2017 18:26:21