- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在努力解决这个问题,因为在我从相机拍摄的图像中,我需要找到某些图案的存在和位置。
为此,我发现使用了opencv的matchTemplate
方法。我使用了opencv示例中使用的图像并编写了代码,但结果不同。
http://opencv.itseez.com/doc/tutorials/imgproc/histograms/template_matching/template_matching.html
这是告诉我们有关 matchTemplate 的链接。
当我实现它时,它显示了结果:-
我的代码如下:-
-(void)matchPatchNet
{
IplImage *res;
CvPoint minloc, maxloc;
double minval, maxval;
int img_width, img_height;
int tpl_width, tpl_height;
int res_width, res_height;
NSString *pathPatron = [[NSBundle mainBundle] pathForResource:@"timage" ofType:@"jpg"];
UIImage *tim = [UIImage imageWithContentsOfFile:pathPatron];
NSString *pathPatron2 = [[NSBundle mainBundle] pathForResource:@"simage" ofType:@"jpg"];
UIImage *tim2 = [UIImage imageWithContentsOfFile:pathPatron2];
IplImage *img = [self CreateIplImageFromUIImage:tim2];//
IplImage *tpl = [self CreateIplImageFromUIImage:tim];
cv::Mat forground1 = [tim2 CVMat];
cv::Mat forground2 = [tim CVMat];
img_width = img->width;
img_height = img->height;
tpl_width = tpl->width;
tpl_height = tpl->height;
res_width = img_width - tpl_width + 1;
res_height = img_height - tpl_height + 1;
res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F, 1 );
cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF_NORMED );
UIImage *ipala=[self UIImageFromIplImage:res];
cv::Mat forground3 = [ipala CVMat];
cv::normalize(forground3, forground3, 0, 1, cv::NORM_MINMAX, CV_8UC1);
cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 );
cvRectangle( img,
cvPoint( maxloc.x, maxloc.y ),
cvPoint( maxloc.x + tpl_width, maxloc.y + tpl_height ),
cvScalar( 0, 255, 0, 0 ), 1, 0, 0 );
/* display images */
self.imageView.image = [self UIImageFromIplImage:img];
cvReleaseImage(&img);
cvReleaseImage(&tpl);
cvReleaseImage(&res);
}
请告诉我我做错了什么。请帮助我。
提前致谢
最佳答案
我强烈建议您使用 C++ 接口(interface)和当前文档,您可以在此处找到:OpenCV v2.4.2 documentation
在此处获取适用于 iOS 的最新版本的 OpenCV:OpenCV for iOS并将其放入您的项目并将其包含在您的项目前缀中:
ExampleApp-Prefix.pch:
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
使用它来将 UIImages“转换”为 cv::Mats:
UIImageCVMatConverter.h:
//
// UIImageCVMatConverter.h
//
#import <Foundation/Foundation.h>
@interface UIImageCVMatConverter : NSObject {
}
+ (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat;
+ (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat withUIImage:(UIImage*)image;
+ (cv::Mat)cvMatFromUIImage:(UIImage *)image;
+ (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image;
+ (UIImage *)scaleAndRotateImageFrontCamera:(UIImage *)image;
+ (UIImage *)scaleAndRotateImageBackCamera:(UIImage *)image;
@end
UIImageCVMatConverter.mm:
//
// UIImageCVMatConverter.m
//
#import "UIImageCVMatConverter.h"
@implementation UIImageCVMatConverter
+ (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat withUIImage:(UIImage*)image;
{
CGColorSpaceRef colorSpace = CGImageGetColorSpace( image.CGImage );
CGFloat cols = image.size.width;
CGFloat rows = image.size.height;
CGFloat widthStep = image.size.width;
CGContextRef contextRef = CGBitmapContextCreate( NULL, cols, rows, 8, widthStep*4, colorSpace, kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault );
CGContextDrawImage( contextRef, CGRectMake(0, 0, cols, rows), image.CGImage );
CGContextSetRGBStrokeColor( contextRef, 1, 0, 0, 1 );
CGImageRef cgImage = CGBitmapContextCreateImage( contextRef );
UIImage* result = [UIImage imageWithCGImage:cgImage];
CGImageRelease( cgImage );
CGContextRelease( contextRef );
CGColorSpaceRelease( colorSpace );
return result;
}
+(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
{
NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
CGColorSpaceRef colorSpace;
if ( cvMat.elemSize() == 1 ) {
colorSpace = CGColorSpaceCreateDeviceGray();
}
else {
colorSpace = CGColorSpaceCreateDeviceRGB();
}
CGDataProviderRef provider = CGDataProviderCreateWithCFData( (__bridge CFDataRef)data );
CGImageRef imageRef = CGImageCreate( cvMat.cols, cvMat.rows, 8, 8 * cvMat.elemSize(), cvMat.step[0], colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault, provider, NULL, false, kCGRenderingIntentDefault );
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease( imageRef );
CGDataProviderRelease( provider );
CGColorSpaceRelease( colorSpace );
return finalImage;
}
+ (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 );
CGContextRef contextRef = CGBitmapContextCreate( cvMat.data, cols, rows, 8, cvMat.step[0], colorSpace, kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault );
CGContextDrawImage( contextRef, CGRectMake(0, 0, cols, rows), image.CGImage );
CGContextRelease( contextRef );
CGColorSpaceRelease( colorSpace );
return cvMat;
}
+ (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image
{
cv::Mat cvMat = [UIImageCVMatConverter cvMatFromUIImage:image];
cv::Mat grayMat;
if ( cvMat.channels() == 1 ) {
grayMat = cvMat;
}
else {
grayMat = cv :: Mat( cvMat.rows,cvMat.cols, CV_8UC1 );
cv::cvtColor( cvMat, grayMat, CV_BGR2GRAY );
}
return grayMat;
}
+ (UIImage *)scaleAndRotateImageBackCamera:(UIImage *)image
{
static int kMaxResolution = 640;
CGImageRef imgRef = image.CGImage;
CGFloat width = CGImageGetWidth( imgRef );
CGFloat height = CGImageGetHeight( imgRef );
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake( 0, 0, width, height );
if ( width > kMaxResolution || height > kMaxResolution ) {
CGFloat ratio = width/height;
if ( ratio > 1 ) {
bounds.size.width = kMaxResolution;
bounds.size.height = bounds.size.width / ratio;
}
else {
bounds.size.height = kMaxResolution;
bounds.size.width = bounds.size.height * ratio;
}
}
CGFloat scaleRatio = bounds.size.width / width;
CGSize imageSize = CGSizeMake( CGImageGetWidth(imgRef), CGImageGetHeight(imgRef) );
CGFloat boundHeight;
UIImageOrientation orient = image.imageOrientation;
switch( orient ) {
case UIImageOrientationUp:
transform = CGAffineTransformIdentity;
break;
case UIImageOrientationUpMirrored:
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
break;
case UIImageOrientationDown:
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationDownMirrored:
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
break;
case UIImageOrientationLeftMirrored:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationLeft:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationRightMirrored:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeScale(-1.0, 1.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
case UIImageOrientationRight:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
}
UIGraphicsBeginImageContext( bounds.size );
CGContextRef context = UIGraphicsGetCurrentContext();
if ( orient == UIImageOrientationRight || orient == UIImageOrientationLeft ) {
CGContextScaleCTM( context, -scaleRatio, scaleRatio );
CGContextTranslateCTM( context, -height, 0 );
}
else {
CGContextScaleCTM( context, scaleRatio, -scaleRatio );
CGContextTranslateCTM( context, 0, -height );
}
CGContextConcatCTM( context, transform );
CGContextDrawImage( UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef );
UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return returnImage;
}
+ (UIImage *)scaleAndRotateImageFrontCamera:(UIImage *)image
{
static int kMaxResolution = 640;
CGImageRef imgRef = image.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake( 0, 0, width, height);
if (width > kMaxResolution || height > kMaxResolution) {
CGFloat ratio = width/height;
if (ratio > 1) {
bounds.size.width = kMaxResolution;
bounds.size.height = bounds.size.width / ratio;
} else {
bounds.size.height = kMaxResolution;
bounds.size.width = bounds.size.height * ratio;
}
}
CGFloat scaleRatio = bounds.size.width / width;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
CGFloat boundHeight;
UIImageOrientation orient = image.imageOrientation;
switch(orient) {
case UIImageOrientationUp:
transform = CGAffineTransformIdentity;
break;
case UIImageOrientationUpMirrored:
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
break;
case UIImageOrientationDown:
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationDownMirrored:
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
break;
case UIImageOrientationLeftMirrored:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationLeft:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeScale(-1.0, 1.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
}
UIGraphicsBeginImageContext( bounds.size );
CGContextRef context = UIGraphicsGetCurrentContext();
if ( orient == UIImageOrientationRight || orient == UIImageOrientationLeft ) {
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
CGContextTranslateCTM(context, -height, 0);
}
else {
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0, -height);
}
CGContextConcatCTM( context, transform );
CGContextDrawImage( UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef );
UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return returnImage;
}
@end
这是我用来在图像中找到多个标记并将它们的中心点插入 std::vector 的代码:
#import "UIImageCVMatConverter.h"
...
cv::Mat src_img;
cv::Mat result_mat;
cv::Mat debug_img;
cv::Mat template_img;
NSArray *markerImages = [NSArray arrayWithObjects:@"marker-1.png", nil];
std::vector<cv::Point> markerPoints;
// input image
src_img = [UIImageCVMatConverter cvMatFromUIImage:cameriaInputImage];
cv::cvtColor(src_img, debug_img, CV_GRAY2BGR);
for (NSString *marker in markerImages) {
template_img = [UIImageCVMatConverter cvMatFromUIImage:[UIImage imageNamed:marker]];
cv::cvtColor(template_img, template_img, CV_GRAY2BGR);
int match_method = CV_TM_CCORR_NORMED;
cv::matchTemplate(src_img, template_img, result_mat, match_method);
cv::normalize(result_mat, result_mat, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
double minVal;
double maxVal;
cv::Point minLoc, maxLoc, matchLoc;
cv::minMaxLoc(result_mat, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat() );
if ( match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED ) {
matchLoc = minLoc;
}
else {
matchLoc = maxLoc;
}
cv::Point top_left = matchLoc;
cv::Point bottom_right = cv::Point(matchLoc.x + template_img.cols , matchLoc.y + template_img.rows);
cv::Point center = cv::Point(0,0);
center.x = (bottom_right.x + top_left.x) / 2;
center.y = (bottom_right.y + top_left.y) / 2;
markerPoints.push_back(center);
}
希望对你有帮助......
关于iphone - matchTemplate opencv 无法正常工作,如 opencv 文档中所示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12049358/
如果我使用 NSUserDefaults 存储应用程序的设置,是否有任何方法可以使我的应用程序的设置“隐藏”,以免显示在 iPhone 上的常规设置应用程序中?我知道还有其他工具,例如 mySetti
我按照该网站的教程进行操作: http://theappleblog.com/2008/08/04/tutorial-build-a-simple-rss-reader-for-iphone/ 为了制
我通过 localisableString 和仅适用于 NSLocale 方法的不同 xib 更改 iPhone 的语言来完成本地化,但应用程序的要求是通过更改应用程序设置 View 中的语言来本地化
我试图弄清楚 iPhone 是否可以通过无线或蓝牙连接到另一台非 iPhone 设备,但发现了相互冲突的信息。我发现的大部分内容都是在 SDK 3.0 版本发布之前发现的,当时这肯定是不可能的。查看堆
This question already has answers here: Closed 7 years ago. Possible Duplicate: How to detect iPhone
当我在模拟器上运行我的应用程序时,每次都会生成白色的空白屏幕。但是当我在底部黑色 iPhone 按钮退出应用程序后,重新进入应用程序后 View 将可见。然后应用程序照常运行。 但是当我将它加载到我的
我的意思是两台 iPhone 设备应该通过蓝牙或 WiFi 连接,并且一台设备的 UI 应该扩展到另一台 iPhone 设备(不共享屏幕)。我们有办法在 iOS 中执行此操作吗? 最佳答案 Bump
如何通过宏检测设备型号?我使用过类似的东西,但模拟器上的结果总是 IS_IPHONE_5 #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInt
我目前有一个应用程序要求用户维护 VPN 隧道。加载时我检查 VPN 隧道是否可用。 我想知道是否有任何方法可以显示 UIAlertView,单击“确定”后,用户将进入 iPhone 主设置屏幕,以便
我正在开发一个 iPhone 客户端应用程序,它允许用户对各种服务进行评分。无需注册或登录。 要求是用户不能重复对服务进行评分(尽管可以更改其评分)。从目前的情况来看,该应用程序可以被删除、重新安装,
比如说,我点击一个 iphone 应用程序图标,启动时它将创建一个 .app 文件。那么是否可以从该应用程序调用另一个 iphone 应用程序。或者我们可以在该 .app 文件中执行一些操作,例如它将
真的有可能让iPhone静音模式独立于iPhone App吗? 这个应用程序"Talking Carl"让我很困惑。我的 iPhone 处于静音模式。每当我打开这个应用程序时。应用程序声音处于开启模式
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
如果我想编写一些自定义 iPhone 应用程序,但不一定通过 App Store 分发它们,是否可以在不加入 iPhone 开发者计划的情况下实现? 假设我只是想为自己编写一些小实用程序并将其放入我的
人们对 Unity 或 Torque Engine 等游戏引擎有何看法和/或体验?如果您是 iPhone 游戏开发新手,是否值得学习其中一种引擎?这些引擎生成的应用程序与使用 sdk 的 native
您能否在未安装 XCode 开发工具的计算机上分发 iPhone 应用程序以在 iPhone 模拟器中进行测试?可以直接在电脑上安装模拟器进行测试吗? 我有一组测试人员,他们不是开发人员,除了能够运行
我想在我的越狱设备上构建并安装我的应用程序,而无需支付 iPhone 开发者计划所需的 99 美元。我有 Rock 和 Cydia...最简单的方法是什么(如果可能的话)? 谢谢。 最佳答案 这是一个
我想测试一个网站,看看它如何与 iPhone 配合使用,但我没有 iPhone 或 iPod touch。有没有一种方法可以让我在不拥有网站的情况下测试网站的运行情况? 我真正想要的是修复 Stack
简单地说;我的 99 美元能给我带来什么我无法免费获得的东西? 好吧,好吧,听起来是个愚蠢的问题,但苹果网站对我来说并不清楚。 我的预感是,您可以在 99 岁时向应用程序商店提交应用程序,但您可以免费
我是一名注册的 iOS 开发人员。如何将我的 iPhone 应用程序转移到我的个人 iPhone? 最佳答案 用于测试?只需选择您的设备而不是模拟器即可。 关于iphone - 如何将我的 iPhon
我是一名优秀的程序员,十分优秀!