2013-02-27 137 views
0

我正在做一個項目,涉及到一個實時攝像頭飼料,並顯示在用戶的窗口。cvFlip()閃爍或返回空

由於攝像機圖像以錯誤的方式默認情況下,我使用cvFlip翻轉它(所以電腦屏幕就像一面鏡子),像這樣:

while (true) 
{ 
    IplImage currentImage = grabber.grab(); 
    cvFlip(currentImage,currentImage, 1); 

    // Image then displayed here on the window. 
} 

也能正常工作的大部分時間。但是,對於很多用戶(主要是在速度更快的電腦上),相機饋送劇烈閃爍。基本上顯示一個未翻轉的圖像,然後翻轉的圖像,然後翻轉,一遍又一遍。

所以我再變事情有點發現問題...

while (true) 
{ 
    IplImage currentImage = grabber.grab(); 
    IplImage flippedImage = null; 
    cvFlip(currentImage,flippedImage, 1); // l-r = 90_degrees_steps_anti_clockwise 
    if(flippedImage == null) 
    { 
     System.out.println("The flipped image is null"); 
     continue; 
    } 
    else 
    { 
     System.out.println("The flipped image isn't null"); 
     continue; 
    } 
} 

被翻動的圖像似乎總是返回null。爲什麼?我究竟做錯了什麼?這真讓我抓狂。

如果這是cvFlip()的問題,那麼還有其他什麼方法來翻轉IplImage?

感謝任何人的幫助!

回答

1

在將結果存儲在其中之前,您需要用空圖像而不是NULL初始化翻轉的圖像。此外,您應該只創建一次圖像,然後重新使用內存以提高效率。所以更好的方法做到這一點會像下面(未經測試):

IplImage current = null; 
IplImage flipped = null; 

while (true) { 
    current = grabber.grab(); 

    // Initialise the flipped image once the source image information 
    // becomes available for the first time. 
    if (flipped == null) { 
    flipped = cvCreateImage(
     current.cvSize(), current.depth(), current.nChannels() 
    ); 
    } 

    cvFlip(current, flipped, 1); 
} 
+0

謝謝!這真是太神奇了,這是我第一次聽到有人說以這種方式初始化圖像,但它現在似乎適用於我! – 2013-03-01 09:33:35