2012-10-15 213 views
2

enter image description here 下面的代碼顯示的圖像如上所示轉換爲下面的圖像...他們顯示黑色背景灰線.....我想白色背景灰色行.. 請指導我..我是新來的iPhone 非常感謝提前如何將黑色更改爲白色

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Initialise video capture - only supported on iOS device NOT simulator 
#if TARGET_IPHONE_SIMULATOR 
    NSLog(@"Video capture is not supported in the simulator"); 
#else 
    _videoCapture = new cv::VideoCapture; 
    if (!_videoCapture->open(CV_CAP_AVFOUNDATION)) 
    { 
     NSLog(@"Failed to open video camera"); 
    } 
#endif 

    // Load a test image and demonstrate conversion between UIImage and cv::Mat 
    UIImage *testImage = [UIImage imageNamed:@"testimage.jpg"]; 

    double t; 
    int times = 10; 

    //-------------------------------- 
    // Convert from UIImage to cv::Mat 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    t = (double)cv::getTickCount(); 

    for (int i = 0; i < times; i++) 
    { 
     cv::Mat tempMat = [testImage CVMat]; 
    } 

    t = 1000 * ((double)cv::getTickCount() - t)/cv::getTickFrequency()/times; 

    [pool release]; 

    NSLog(@"UIImage to cv::Mat: %gms", t); 

    //------------------------------------------ 
    // Convert from UIImage to grayscale cv::Mat 
    pool = [[NSAutoreleasePool alloc] init]; 

    t = (double)cv::getTickCount(); 

    for (int i = 0; i < times; i++) 
    { 
     cv::Mat tempMat = [testImage CVGrayscaleMat]; 
    } 

    t = 1000 * ((double)cv::getTickCount() - t)/cv::getTickFrequency()/times; 

    [pool release]; 

    NSLog(@"UIImage to grayscale cv::Mat: %gms", t); 

    //-------------------------------- 
    // Convert from cv::Mat to UIImage 
    cv::Mat testMat = [testImage CVMat]; 

    t = (double)cv::getTickCount(); 

    for (int i = 0; i < times; i++) 
    { 
     UIImage *tempImage = [[UIImage alloc] initWithCVMat:testMat]; 
     [tempImage release]; 
    } 

    t = 1000 * ((double)cv::getTickCount() - t)/cv::getTickFrequency()/times; 

    NSLog(@"cv::Mat to UIImage: %gms", t); 

    // Process test image and force update of UI 
    _lastFrame = testMat; 
    [self sliderChanged:nil]; 
} 
- (IBAction)capture:(id)sender 
{ 
    if (_videoCapture && _videoCapture->grab()) 
    { 
     (*_videoCapture) >> _lastFrame; 
     [self processFrame]; 
    } 
    else 
    { 
     NSLog(@"Failed to grab frame");   
    } 
} 
- (void)processFrame 
{ 
    double t = (double)cv::getTickCount(); 

    cv::Mat grayFrame, output; 

    // Convert captured frame to grayscale 
    cv::cvtColor(_lastFrame, grayFrame, cv::COLOR_RGB2GRAY); 

    // Perform Canny edge detection using slide values for thresholds 
    cv::Canny(grayFrame, output, 
       _lowSlider.value * kCannyAperture * kCannyAperture, 
       _highSlider.value * kCannyAperture * kCannyAperture, 
       kCannyAperture); 

    t = 1000 * ((double)cv::getTickCount() - t)/cv::getTickFrequency(); 

    // Display result 
    self.imageView.image = [UIImage imageWithCVMat:output]; 
    self.elapsedTimeLabel.text = [NSString stringWithFormat:@"%.1fms", t]; 
} 

enter image description here

回答

2

反轉灰度1路墊:

Mat invertedOutput = 255 - output; 
+1

好多了!我會刪除我的答案。 – CapelliC

+1

我覺得還有Mat.inv(); –

+0

@RuiMarques我認爲它會計算矩陣的逆矩陣(http://mathworld.wolfram.com/MatrixInverse.html),而不是將黑白轉換成黑白。 – tofi9

相關問題