Last active
June 8, 2019 19:38
-
-
Save jebai/8108287 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) parseBuffer:(CMSampleBufferRef) sampleBuffer | |
{ | |
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); | |
CVPixelBufferLockBaseAddress( pixelBuffer, 0 ); | |
//Processing here | |
int bufferWidth = CVPixelBufferGetWidth(pixelBuffer); | |
int bufferHeight = CVPixelBufferGetHeight(pixelBuffer); | |
unsigned char *pixel = (unsigned char *)CVPixelBufferGetBaseAddress(pixelBuffer); | |
// put buffer in open cv, no memory copied | |
cv::Mat mat = cv::Mat(bufferHeight,bufferWidth,CV_8UC4,pixel); | |
//End processing | |
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One suggestion, based on my experience using this: some iPhone device video streams have padding at the end of each pixelBuffer row, which will result in corrupted images if the Mat auto-calculates the step size. To fix it, manually set the 5th 'step' argument to cv::Mat to
CVPixelBufferGetBytesPerRow(pixelBuffer)
. So line 14 would be:cv::Mat mat = cv::Mat(bufferHeight,bufferWidth,CV_8UC4,pixel,CVPixelBufferGetBytesPerRow(pixelBuffer));