在我的一博客中转摘别人的一个OpenCV的文章,没有自己动手过,这次亲自操刀了。呵呵。
引入OpenCV库
首先建立个工程TestOpenCV吧。按http://blog.csdn.net/nogodoss/article/details/11926063导入相关资源。如下图:
然后再工程的-Prefix.pch文件上加入
注意必须在 __OBJC__ 之前哟。 到此工程编译没有问题。
以下是应用OpenCV库的操作。
旋转:
.h文件
#import <Foundation/Foundation.h>
@interface OpencvUtilt : NSObject {
}
-(UIImage*)rotateImage:(UIImage*)inputImage Angle:(double)angle;
@end
.mm文件---------- 这里必须是.mm文件有
@implementation OpencvUtilt
-(cv::Mat)cvMatFromUIImage:(UIImage *)image
{
CGColorSpaceRef colorSpace =CGImageGetColorSpace(image.CGImage);
CGFloat cols = image.size.width;
CGFloat rows = image.size.height;
cv::Mat cvMat(rows, cols,CV_8UC4); // 8 bits per component, 4 channels
CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
cols, // Width of bitmap
rows, // Height of bitmap
8, // Bits per component
cvMat.step[0], // Bytes per row
colorSpace, // Colorspace
kCGImageAlphaNoneSkipLast |
kCGBitmapByteOrderDefault); // Bitmap info flags
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef);
return cvMat;
}
-(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
{
NSData *data = [NSDatadataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
CGColorSpaceRef colorSpace;
if (cvMat.elemSize() ==1) {
colorSpace = CGColorSpaceCreateDeviceGray();
} else {
colorSpace = CGColorSpaceCreateDeviceRGB();
}
CGDataProviderRef provider =CGDataProviderCreateWithCFData((CFDataRef)data);
// Creating CGImage from cv::Mat
CGImageRef imageRef = CGImageCreate(cvMat.cols, //width
cvMat.rows, //height
8, //bits per component
8 * cvMat.elemSize(), //bits per pixel
cvMat.step[0], //bytesPerRow
colorSpace, //colorspace
kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
provider, //CGDataProviderRef
NULL, //decode
false, //should interpolate
kCGRenderingIntentDefault //intent
);
// Getting UIImage from CGImage
UIImage *finalImage = [UIImageimageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return finalImage;
}
-(cv::Mat)rotate:(cv::Mat &)image Angle:(double) angle {
cv::Point2f src_center(image.rows/2.0F-160, image.cols/2.0F);
double scale = 1;// 这里就没有放大缩小了。
if(angle == 0) {
return image;
}
if(angle == 180) {
cv::Mat rot_matrix =getRotationMatrix2D(src_center, angle,1);
cv::Mat rotated_img(cv::Size(image.cols, image.rows), image.type());
warpAffine(image, rotated_img, rot_matrix, rotated_img.size());
return rotated_img;
}
if(angle == 90 || angle ==270) {
cv::Mat rot_matrix =getRotationMatrix2D(src_center, angle,scale);
cv::Mat rotated_img(cv::Size(960,640), image.type());
warpAffine(image, rotated_img, rot_matrix, rotated_img.size());
return rotated_img;
}
return cv::Mat::zeros(0,0, 0);
}
-(UIImage*)rotateImage:(UIImage*)inputImage Angle:(double)angle{
cv::Mat inputMat= [selfcvMatFromUIImage:inputImage];
cv::Mat outputMat = [selfrotate:inputMat Angle:angle];
return [self UIImageFromCVMat:outputMat];
}
@end
demo:
- (void)buttonAction:(id)sender {
OpencvUtilt *utilt = [[OpencvUtilt alloc] init];
self.imageView.image = [utilt rotateImage:imageView_.image Angle:90.0];
}