0

I want to process the live video from the camera frame by frame but when I try to convert my image to a cv::Mat I always get CGBitmapContextCreate: unsupported parameter combination: set CGBITMAP_CONTEXT_LOG_ERRORS environmental variable to see the details. If I set that variable I get Assertion Failed(0) SIGABRT.

I've tried with this method as well as with that included methond from ios.h but I still get the same result. I tried everything I could have found on SO. I presume the only thing that remains is that I don't know something in connection with my image when I convert the CMSampleBuffer to UIImage. Here is my code:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
    DispatchQueue.global(qos: .background).async {
        let pixelBuff = CMSampleBufferGetImageBuffer(sampleBuffer)
        let ciImage:CIImage? = CIImage(cvPixelBuffer: pixelBuff!)
        let img = UIImage(ciImage: ciImage!)
        let i = self.cv.processImage(withOpenCV: img)
        DispatchQueue.main.async {
            self.imageView.image = i
        }

    }
}

I've also tried a lot of different methonds found on google to convert my buffer to a UIImage but still nothing. Do you have any idea?

Community
  • 1
  • 1
kemkriszt
  • 280
  • 3
  • 17
  • First, going from `CIImage` to `UIImage`, I frequently have to go through `CGImage`. See http://stackoverflow.com/a/35109332/1271826. Second, which line is generating the error you describe? Also, have you tried setting `CGBITMAP_CONTEXT_LOG_ERRORS`? – Rob Nov 22 '16 at 16:26
  • Thanks but I've already tried that thing with cgimage. From the method I've linked the call of CGBitmapContextCreate generates the error and yes, as I've described in the question, I've tried to set that variable. – kemkriszt Nov 22 '16 at 16:32
  • And what was the bytes per row? Does it match 4 * `width`? – Rob Nov 22 '16 at 16:35
  • This code is from opencv's website and they use like that. But i've found an other version on SO where they have used cols*4 so I tested it as well but got the same result – kemkriszt Nov 22 '16 at 16:37
  • Maybe something in connection with the UIImage that does not match with the settings in that method? – kemkriszt Nov 22 '16 at 19:54
  • Perhaps. It's usually something more fundamental within the `CGBitmapContextCreate` call, where the parameters are not internally consistent, i.e. the "bytes per row" is not equal to the "width" times the "bytes per pixel". For example, if it were a B&W image, it's two bytes per pixel, whereas if it was a 16-bits per channel image, it could be eight bytes per pixel. Or perhaps one of the values was `nil` or `0`. Or perhaps the color space provided doesn't match. It could be lots of things, but it's a little hard to diagnose on the basis of the information provided. – Rob Nov 22 '16 at 21:15
  • Ok, but What about the build-in method that gives the same result? For other's it is working well – kemkriszt Nov 23 '16 at 09:41

1 Answers1

0
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
    DispatchQueue.global(qos: .background).async {
        let pixelBuff = CMSampleBufferGetImageBuffer(sampleBuffer)
        let ciImage:CIImage? = CIImage(cvPixelBuffer: pixelBuff!)
        let img = UIImage(ciImage: ciImage!)
        // convert image
        let theimage = UIImage(data: img!.pngData()!)
        let i = self.cv.processImage(withOpenCV: theimage)
        DispatchQueue.main.async {
            self.imageView.image = i
        }

    }
}
  • Welcome to SO, Abler Song! It would be helpful for the OP if you posted some code to go along with your answer. – Joel Nov 01 '18 at 12:48