[問題] opencv 搭配 C 寫影像任意角度旋轉

看板C_and_CPP作者 (U文心得大師)時間8年前 (2017/06/15 11:37), 8年前編輯推噓3(3014)
留言17則, 2人參與, 最新討論串1/1
開發平台(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

06/15 13:13, , 1F
你 轉置 function 中, i,j 似乎沒有給初始值就拿來
06/15 13:13, 1F

06/15 13:14, , 2F
用了, 這樣你 x1 y1 的給array用可能會有問題
06/15 13:14, 2F

06/15 13:16, , 3F
你迴圈是用x,y 看不出來 i j是哪裡來的
06/15 13:16, 3F
目前顯示的問題是卡在長寬那塊 數字無法存入array ※ 編輯: amateuruser (140.124.249.51), 06/15/2017 15:02:16

06/15 15:05, , 4F
因為你 tran(...) 裡面有問題呀
06/15 15:05, 4F

06/15 15:06, , 5F
還有像你有一個 if理面是
06/15 15:06, 5F

06/15 15:07, , 6F
frame_out[(y*width+x)*3+z] = fram_in[...]
06/15 15:07, 6F

06/15 15:07, , 7F
到了 else 中是
06/15 15:07, 7F

06/15 15:08, , 8F
frame_out[(y*width+i)*3+z] = 0
06/15 15:08, 8F

06/15 15:08, , 9F
x 怎麼會變成 i 了
06/15 15:08, 9F

06/15 15:10, , 10F
如果你是指 frame_in[i] = Image1->imageData[i]
06/15 15:10, 10F

06/15 15:11, , 11F
存不進去 你要確定 Image1->imageData[] 的大小有到
06/15 15:11, 11F

06/15 15:12, , 12F
hight*width*3 呀 話說為甚麼要*3
06/15 15:12, 12F

06/15 15:13, , 13F
我覺得 Image1->imageData[] 的大小應該只會有長X寬
06/15 15:13, 13F

06/15 17:16, , 14F
opencv不是有提供旋轉的函式 還是你是要練習? :p
06/15 17:16, 14F

06/15 17:18, , 15F
*3是因為RGB吧 雖然應該是這樣寫:(y*width*3+x)+z
06/15 17:18, 15F

06/15 17:20, , 16F
然後直接用width不太對 因為opecv在配記憶體會幫你align
06/15 17:20, 16F

06/15 17:21, , 17F
正確應該用(y*image1->widthStep+x)+z (假設為8bit的圖)
06/15 17:21, 17F
我是存粹要練習 因為有考慮以這個程式碼為依據 做基本影像處理 ※ 編輯: amateuruser (140.124.249.31), 06/15/2017 18:26:21
文章代碼(AID): #1PGW3aNs (C_and_CPP)