看板 C_and_CPP 關於我們 聯絡資訊
開發平台(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",&degree); // 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
libertyleave: 你 轉置 function 中, i,j 似乎沒有給初始值就拿來06/15 13:13
libertyleave: 用了, 這樣你 x1 y1 的給array用可能會有問題06/15 13:14
libertyleave: 你迴圈是用x,y 看不出來 i j是哪裡來的06/15 13:16
目前顯示的問題是卡在長寬那塊 數字無法存入array ※ 編輯: amateuruser (140.124.249.51), 06/15/2017 15:02:16
libertyleave: 因為你 tran(...) 裡面有問題呀06/15 15:05
libertyleave: 還有像你有一個 if理面是06/15 15:06
libertyleave: frame_out[(y*width+x)*3+z] = fram_in[...]06/15 15:07
libertyleave: 到了 else 中是06/15 15:07
libertyleave: frame_out[(y*width+i)*3+z] = 006/15 15:08
libertyleave: x 怎麼會變成 i 了06/15 15:08
libertyleave: 如果你是指 frame_in[i] = Image1->imageData[i]06/15 15:10
libertyleave: 存不進去 你要確定 Image1->imageData[] 的大小有到06/15 15:11
libertyleave: hight*width*3 呀 話說為甚麼要*306/15 15:12
libertyleave: 我覺得 Image1->imageData[] 的大小應該只會有長X寬06/15 15:13
Zero0910: opencv不是有提供旋轉的函式 還是你是要練習? :p06/15 17:16
Zero0910: *3是因為RGB吧 雖然應該是這樣寫:(y*width*3+x)+z06/15 17:18
Zero0910: 然後直接用width不太對 因為opecv在配記憶體會幫你align06/15 17:20
Zero0910: 正確應該用(y*image1->widthStep+x)+z (假設為8bit的圖)06/15 17:21
我是存粹要練習 因為有考慮以這個程式碼為依據 做基本影像處理 ※ 編輯: amateuruser (140.124.249.31), 06/15/2017 18:26:21