黑色

时间:2015-11-07 22:28:25

标签: c++ opencv

我有一些简单的代码可以执行精确边缘检测并覆盖原始图像上的边缘。

代码有效,但我希望边缘用黑色绘制,目前它们是用白色绘制的。

static void sketchImage(Mat srcColor, Mat& dst, bool sketchMode, int debugType)
{
   Mat srcGray;
   Mat edgesBgr;
   cvtColor(srcColor, srcGray, CV_BGRA2GRAY);
   cvtColor(srcColor, srcColor, CV_BGRA2BGR);

   GaussianBlur(srcGray, srcGray, cvSize(5, 5),1.2,1.2);
   GaussianBlur(srcColor, srcColor, cvSize(5,5), 1.4, 1.4);

   CvSize size = srcColor.size();
   Mat edges = Mat(size, CV_8U);

   Canny(srcGray, edges, 150, 150);

   cvtColor(edges, edgesBgr, cv::COLOR_GRAY2BGR);

   dst = srcColor + edgesBgr;
}

我确信这很简单,但我对openCV还是新手,我很感激任何帮助。

所要求的完整代码:

#import "ViewController.h"
#import "opencv2/highgui.hpp"
#import "opencv2/core.hpp"
#import "opencv2/opencv.hpp"
#import "opencv2/imgproc.hpp"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *display;
@property (strong, nonatomic) UIImage* image;
@property (strong, nonatomic) UIImage* backup;
@property NSInteger clickflag;
@end

@implementation ViewController

using namespace cv;

- (IBAction)convert_click:(id)sender {

NSLog(@"Clicked");
if (_clickflag == 0)
{
    cv::Mat cvImage, cvBWImage;
    UIImageToMat(_image, cvImage);
    //cv::cvtColor(cvImage, cvBWImage, CV_BGR2GRAY);

    //cvBWImage = cvImage;
    cartoonifyImage(cvImage, cvBWImage, false, 0);

    _image = MatToUIImage(cvBWImage);
    [_display setImage:_image];
    _clickflag = 1;
}
else if(_clickflag == 1)
{
    _image = _backup;
    [_display setImage:_image];
    _clickflag = 0;
}


}



static UIImage* MatToUIImage(const cv::Mat& m)
{
//CV_Assert(m.depth() == CV_8U);
NSData *data = [NSData dataWithBytes:m.data length:m.step*m.rows];
CGColorSpaceRef colorSpace = m.channels() == 1 ?
CGColorSpaceCreateDeviceGray() : CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

// Creating CGImage from cv::Mat
CGImageRef imageRef = CGImageCreate(m.cols, m.rows, m.elemSize1()*8, m.elemSize()*8,
                                    m.step[0], colorSpace, kCGImageAlphaNoneSkipLast|kCGBitmapByteOrderDefault,
                                    provider, NULL, false, kCGRenderingIntentDefault);
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return finalImage;
}

static void UIImageToMat(const UIImage* image, cv::Mat& m)
{
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGFloat cols = image.size.width;
CGFloat rows = image.size.height;

m.create(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
CGContextRef contextRef = CGBitmapContextCreate(m.data, m.cols, m.rows, 8,
                                                m.step[0], colorSpace, kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault);
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef);

}

static void sketchImage(Mat srcColor, Mat& dst, bool sketchMode, int debugType)

{

Mat srcGray;
Mat edgesBgr;

cvtColor(srcColor, srcGray, CV_BGRA2GRAY);
cvtColor(srcColor, srcColor, CV_BGRA2BGR);

GaussianBlur(srcGray, srcGray, cvSize(5, 5),1.2,1.2);
GaussianBlur(srcColor, srcColor, cvSize(5,5), 1.4, 1.4);

CvSize size = srcColor.size();
Mat edges = Mat(size, CV_8U);

Canny(srcGray, edges, 150, 150);

cvtColor(edges, edgesBgr, cv::COLOR_GRAY2BGR);
//edgesBgr = edgesBgr.inv();
NSLog(@"%d, %d\n", srcColor.size().height, srcColor.size().width);
NSLog(@"%d, %d\n", edgesBgr.size().height, edgesBgr.size().width);

dst = edgesBgr + srcColor;
}




- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

_image = [UIImage imageNamed:@"Robben.jpg"];
_backup = [UIImage imageNamed:@"Robben.jpg"];
_clickflag = 0;

[_display setImage:_image];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

2 个答案:

答案 0 :(得分:2)

您可以应用bitwise_not(dst,dst)以使白色变为黑色,黑色变为白色!

  

void bitwise_not(InputArray src,OutputArray dst,InputArray   掩模= noArray())

答案 1 :(得分:2)

static void sketchImage(Mat srcColor, Mat& dst, bool sketchMode, int debugType)
{
   Mat srcGray;

   cvtColor(srcColor, srcGray, CV_BGRA2GRAY);
   cvtColor(srcColor, srcColor, CV_BGRA2BGR);

   GaussianBlur(srcGray, srcGray, cvSize(5, 5),1.2,1.2);
   GaussianBlur(srcColor, srcColor, cvSize(5,5), 1.4, 1.4);

   CvSize size = srcColor.size();
   Mat edges = Mat(size, CV_8U);

   Canny(srcGray, edges, 150, 150);

   dst = srcColor.clone();
   dst.setTo(0,edges);
}