OpenCV iOS - Unsupported Parameter Combination

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • OpenCV iOS - Unsupported Parameter Combination

    Moin,

    ich versuche aktuell die OpenCV Komponente für iOS zu verwenden und habe mir auf der Seite in den Dokumentationen die Testprojekte angesehen. Dort wurde folgende Methode verwendet, um ein 'schwarz-weiß Bild' zu erzeugen

    C-Quellcode

    1. - (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image
    2. {
    3. CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
    4. CGFloat cols = image.size.width;
    5. CGFloat rows = image.size.height;
    6. cv::Mat cvMat = cv::Mat(rows, cols, CV_8UC1); // 8 bits per component, 1 channel
    7. CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to backing data
    8. cols, // Width of bitmap
    9. rows, // Height of bitmap
    10. 8, // Bits per component
    11. cvMat.step[0], // Bytes per row
    12. colorSpace, // Colorspace
    13. kCGImageAlphaNone |
    14. kCGBitmapByteOrderDefault); // Bitmap info flags
    15. CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
    16. CGContextRelease(contextRef);
    17. CGColorSpaceRelease(colorSpace);
    18. return cvMat;
    19. }
    Alles anzeigen


    Leider führt dies zu folgender Konsolenausgabe:

    Shell-Script

    1. Aug 17 11:14:24 OpenCVDemo[1250] <Error>: CGBitmapContextCreate: unsupported parameter combination: set CGBITMAP_CONTEXT_LOG_ERRORS environmental variable to see the details
    2. Aug 17 11:14:24 OpenCVDemo[1250] <Error>: CGContextDrawImage: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
    Im Anschluss noch mal die gesamte datei, damit ihr die Zusammenhänge einsehen könnt. beide anderen test-Methoden laufen durch. nur die cvMatGrayFromUIImage wirft einen Fehler.


    C-Quellcode

    1. //
    2. // SPOpenCVImageView.m
    3. // OpenCVDemo
    4. //
    5. // Created by Bocksteger, Daniel on 17.08.15.
    6. // Copyright (c) 2015 Bocksteger, Daniel. All rights reserved.
    7. //
    8. #import "SPOpenCVImageView.h"
    9. #import <opencv2/opencv.hpp>
    10. @implementation SPOpenCVImageView
    11. // Only override drawRect: if you perform custom drawing.
    12. // An empty implementation adversely affects performance during animation.
    13. - (void)drawRect:(CGRect)rect {
    14. // Drawing code
    15. UIImage *wwImage = [UIImage imageNamed:@"ww_logo.jpg"];
    16. cv::Mat wwMat = [self cvMat3ChannelFromUIImage:wwImage];
    17. [imageView setImage:[self UIImageFromCVMat:wwMat]];
    18. }
    19. #pragma mark - OpenCV
    20. - (cv::Mat)cvMatFromUIImage:(UIImage *)image
    21. {
    22. CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
    23. CGFloat cols = image.size.width;
    24. CGFloat rows = image.size.height;
    25. cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)
    26. CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
    27. cols, // Width of bitmap
    28. rows, // Height of bitmap
    29. 8, // Bits per component
    30. cvMat.step[0], // Bytes per row
    31. colorSpace, // Colorspace
    32. kCGImageAlphaNoneSkipLast |
    33. kCGBitmapByteOrderDefault); // Bitmap info flags
    34. CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
    35. CGContextRelease(contextRef);
    36. return cvMat;
    37. }
    38. - (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image
    39. {
    40. CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
    41. CGFloat cols = image.size.width;
    42. CGFloat rows = image.size.height;
    43. cv::Mat cvMat = cv::Mat(rows, cols, CV_8UC1); // 8 bits per component, 1 channel
    44. CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to backing data
    45. cols, // Width of bitmap
    46. rows, // Height of bitmap
    47. 8, // Bits per component
    48. cvMat.step[0], // Bytes per row
    49. colorSpace, // Colorspace
    50. kCGImageAlphaNoneSkipLast |
    51. kCGBitmapByteOrderDefault); // Bitmap info flags
    52. CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
    53. CGContextRelease(contextRef);
    54. CGColorSpaceRelease(colorSpace);
    55. return cvMat;
    56. }
    57. - (cv::Mat)cvMat3ChannelFromUIImage:(UIImage *)image
    58. {
    59. CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
    60. CGFloat cols = image.size.width;
    61. CGFloat rows = image.size.height;
    62. cv::Mat rgba(rows, cols, CV_8UC4, cvScalar(1,2,3,4)); // 8 bits per component, 4 channels
    63. CGContextRef contextRef = CGBitmapContextCreate(rgba.data, // Pointer to backing data
    64. cols, // Width of bitmap
    65. rows, // Height of bitmap
    66. 8, // Bits per component
    67. rgba.step[0], // Bytes per row
    68. colorSpace, // Colorspace
    69. kCGImageAlphaNoneSkipLast |
    70. kCGBitmapByteOrderDefault); // Bitmap info flags
    71. CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
    72. CGContextRelease(contextRef);
    73. cv::Mat bgr( rgba.rows, rgba.cols, CV_8UC3 );
    74. cv::Mat alpha( rgba.rows, rgba.cols, CV_8UC1 );
    75. cv::Mat out[] = { bgr, alpha };
    76. // rgba[0] -> bgr[2], rgba[1] -> bgr[1],
    77. // rgba[2] -> bgr[0], rgba[3] -> alpha[0]
    78. int from_to[] = { 0,2, 1,1, 2,0, 3,3 };
    79. mixChannels( &rgba, 1, out, 2, from_to, 4 );
    80. return bgr;
    81. }
    82. -(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
    83. {
    84. NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
    85. CGColorSpaceRef colorSpace;
    86. if (cvMat.elemSize() == 1) {
    87. colorSpace = CGColorSpaceCreateDeviceGray();
    88. } else {
    89. colorSpace = CGColorSpaceCreateDeviceRGB();
    90. }
    91. CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
    92. // Creating CGImage from cv::Mat
    93. CGImageRef imageRef = CGImageCreate(cvMat.cols, //width
    94. cvMat.rows, //height
    95. 8, //bits per component
    96. 8 * cvMat.elemSize(), //bits per pixel
    97. cvMat.step[0], //bytesPerRow
    98. colorSpace, //colorspace
    99. kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
    100. provider, //CGDataProviderRef
    101. NULL, //decode
    102. false, //should interpolate
    103. kCGRenderingIntentDefault //intent
    104. );
    105. // Getting UIImage from CGImage
    106. UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    107. CGImageRelease(imageRef);
    108. CGDataProviderRelease(provider);
    109. CGColorSpaceRelease(colorSpace);
    110. return finalImage;
    111. }
    112. @end
    Alles anzeigen
    Eventuell kann mich jemand darüber aufklären, in wie fern dort falsche Parameter kombiniert werden?
    Man kann alles schaffen. Man muss es nur wollen ;)
    www.regetskcob.github.io
  • Mac & i Test Abo