2015-05-27 233 views
0

我是OpenCV庫的新手,我想用它來檢測從iPad背部攝像頭捕獲的視頻流中的圓圈。我想出瞭如何使用OpenCV 2.4.2,它可以在少於10行的代碼中完成。但它不適合我,我想我錯過了某些東西,因爲我得到了一些奇怪的行爲。cv:圓圈函數用一次調用繪製多個圓圈

該代碼非常簡單,每當攝像頭捕獲到新幀時,就從Objective-C回調觸發器開始。以下是我在這個回調做:

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
     didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection 
{ 
    // Convert CMSampleBufferRef to CVImageBufferRef 
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

    // Lock pixel buffer 
    CVPixelBufferLockBaseAddress(imageBuffer, kCVPixelBufferLock_ReadOnly); 

    // Construct VideoFrame struct 
    uint8_t *baseAddress = (uint8_t*)CVPixelBufferGetBaseAddress(imageBuffer); 
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 
    size_t stride = CVPixelBufferGetBytesPerRow(imageBuffer); 

    // Unlock pixel buffer 
    CVPixelBufferUnlockBaseAddress(imageBuffer, 0); 

    std::vector<unsigned char> data(baseAddress, baseAddress + (stride * height)); 

    // Call C++ function with these arguments => (data, (int)width, (int)height) 
} 

這裏是與OpenCV的處理圖像的C++函數:

void proccessImage(std::vector<unsigned char>& imageData, int width, int height) 
{ 
    // Create cv::Mat from std::vector<unsigned char> 
    Mat src(width, height, CV_8UC4, const_cast<unsigned char*>(imageData.data())); 
    Mat final; 

    // Draw a circle at position (300, 200) with a radius of 30 
    cv::Point center(300, 200); 
    circle(src, center, 30.f, CV_RGB(0, 0, 255), 3, 8, 0); 

    // Convert the gray image to RGBA 
    cvtColor(src, final, CV_BGRA2RGBA); 

    // Reform the std::vector from cv::Mat data 
    std::vector<unsigned char> array; 
    array.assign((unsigned char*)final.datastart, (unsigned char*)final.dataend); 

    // Send final image data to GPU and draw it 
} 

圖像從iPad的背部攝像頭獲取在BGRA(32位)格式。

我期望的是一個來自iPad背部攝像頭的圖像,在位置x = 300px,y = 200px並且半徑爲30px的位置繪製了一個簡單的圓圈。

這是我得到:http://i.stack.imgur.com/bWfwa.jpg

你知道什麼是錯我的代碼?

在此先感謝。

回答

0

感謝您的幫助,我終於想通了,發生什麼事,這是我整個的錯...

當你創建你需要將它傳遞圖像的高度,第一個參數,而不是寬新墊。如果我切換參數,圓形繪製得當。