iOS:从背景图像中检索矩形图像

2024-04-11

我正在开发一个实现,其中我在大背景图像中有一个矩形图像。我正在尝试以编程方式从大图像中检索矩形图像,并从该特定矩形图像中检索文本信息。我正在尝试使用 Open-CV 第三方框架,但无法从大背景图像中检索矩形图像。有人可以指导我,我怎样才能实现这一目标?

UPDATED:

我找到了Link https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/c/squares.c?rev=27使用 OpenCV 找出正方形。我可以修改它来查找矩形形状吗?有人可以指导我吗?

最新更新:

我终于得到了代码,如下。

    - (cv::Mat)cvMatWithImage:(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 backing 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 = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
    CGColorSpaceRef colorSpace;
    if ( cvMat.elemSize() == 1 ) {
        colorSpace = CGColorSpaceCreateDeviceGray();
    }
    else {
        colorSpace = CGColorSpaceCreateDeviceRGB();
    }

    //CFDataRef data;
    CGDataProviderRef provider = CGDataProviderCreateWithCFData( (CFDataRef) data ); // It SHOULD BE (__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;
}
-(void)forOpenCV
{
    imageView = [UIImage imageNamed:@"myimage.jpg"];
    if( imageView != nil )
    {
        cv::Mat tempMat = [imageView CVMat];

        cv::Mat greyMat = [self cvMatWithImage:imageView];
        cv::vector<cv::vector<cv::Point> > squares;

        cv::Mat img= [self debugSquares: squares: greyMat];

        imageView = [self UIImageFromCVMat: img];

        self.imageView.image = imageView;
    }
}

double angle( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) {
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}

- (cv::Mat) debugSquares: (std::vector<std::vector<cv::Point> >) squares : (cv::Mat &)image
{
    NSLog(@"%lu",squares.size());

    // blur will enhance edge detection

    //cv::Mat blurred(image);
    cv::Mat blurred = image.clone();
    medianBlur(image, blurred, 9);

    cv::Mat gray0(image.size(), CV_8U), gray;
    cv::vector<cv::vector<cv::Point> > contours;

    // find squares in every color plane of the image
    for (int c = 0; c < 3; c++)
    {
        int ch[] = {c, 0};
        mixChannels(&image, 1, &gray0, 1, ch, 1);

        // try several threshold levels
        const int threshold_level = 2;
        for (int l = 0; l < threshold_level; l++)
        {
            // Use Canny instead of zero threshold level!
            // Canny helps to catch squares with gradient shading
            if (l == 0)
            {
                Canny(gray0, gray, 10, 20, 3); //

                // Dilate helps to remove potential holes between edge segments
                dilate(gray, gray, cv::Mat(), cv::Point(-1,-1));
            }
            else
            {
                gray = gray0 >= (l+1) * 255 / threshold_level;
            }

            // Find contours and store them in a list
            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

            // Test contours
            cv::vector<cv::Point> approx;
            for (size_t i = 0; i < contours.size(); i++)
            {
                // approximate contour with accuracy proportional
                // to the contour perimeter
                approxPolyDP(cv::Mat(contours[i]), approx, arcLength(cv::Mat(contours[i]), true)*0.02, true);

                // Note: absolute value of an area is used because
                // area may be positive or negative - in accordance with the
                // contour orientation
                if (approx.size() == 4 &&
                    fabs(contourArea(cv::Mat(approx))) > 1000 &&
                    isContourConvex(cv::Mat(approx)))
                {
                    double maxCosine = 0;

                    for (int j = 2; j < 5; j++)
                    {
                        double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                        maxCosine = MAX(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3)
                        squares.push_back(approx);
                }
            }
        }
    }

    NSLog(@"squares.size(): %lu",squares.size());


    for( size_t i = 0; i < squares.size(); i++ )
    {
        cv::Rect rectangle = boundingRect(cv::Mat(squares[i]));
        NSLog(@"rectangle.x: %d", rectangle.x);
        NSLog(@"rectangle.y: %d", rectangle.y);

        if(i==squares.size()-1)////Detecting Rectangle here
        {
            const cv::Point* p = &squares[i][0];

            int n = (int)squares[i].size();

            NSLog(@"%d",n);

            line(image, cv::Point(507,418), cv::Point(507+1776,418+1372), cv::Scalar(255,0,0),2,8);

            polylines(image, &p, &n, 1, true, cv::Scalar(255,255,0), 5, CV_AA);

            int fx1=rectangle.x;
                NSLog(@"X: %d", fx1);
            int fy1=rectangle.y;
                NSLog(@"Y: %d", fy1);
            int fx2=rectangle.x+rectangle.width;
                NSLog(@"Width: %d", fx2);
            int fy2=rectangle.y+rectangle.height;
                NSLog(@"Height: %d", fy2);

            line(image, cv::Point(fx1,fy1), cv::Point(fx2,fy2), cv::Scalar(0,0,255),2,8);

        }

    }

    return image;
}

谢谢。


这是使用一个小包装类将 C++ 与 Objective-C 代码分开的完整答案。

我不得不在 stackoverflow 上提出另一个问题 https://stackoverflow.com/questions/14104510/objective-c-wrapper-invoking-c-static-member-functions来应对我糟糕的 C++ 知识 - 但我已经弄清楚了与 C++ 接口所需的一切cleanly与 Objective-C 代码,使用squares.cpp示例代码作为示例。目的是尽可能保持原始 C++ 代码原始,并将 openCV 的大部分工作保留在纯 C++ 文件中,以实现(不)可移植性。

我保留了原来的答案,因为这似乎超出了编辑范围。完整的demo项目在github上 https://github.com/foundry/OpenCVSquares

CVViewController.h / CVViewController.m

  • 纯 Objective-C

  • 通过 WRAPPER 与 openCV c++ 代码进行通信...它既不知道也不关心 c++ 是否正在包装器后面处理这些方法调用。

CVWrapper.h / CVWrapper.mm

  • 目标C++

尽可能少做,实际上只有两件事......

  • 调用 UIImage objC++ 类别以与 UIImage cv::Mat 相互转换
  • 在 CVViewController 的 obj-C 方法和 CVSquares c++(类)函数调用之间进行调解

CVSquares.h / CVSquares.cpp

  • pure C++
  • CVSquares.cpp在类定义中声明公共函数(在本例中为一个静态函数)。
    这取代了的工作main{}在原始文件中。
  • 我们努力保持CVSquares.cpp为了可移植性,尽可能接近 C++ 原始版本。

CVViewController.m

//remove 'magic numbers' from original C++ source so we can manipulate them from obj-C
#define TOLERANCE 0.01
#define THRESHOLD 50
#define LEVELS 9

UIImage* image =
        [CVSquaresWrapper detectedSquaresInImage:self.image
                                       tolerance:TOLERANCE
                                       threshold:THRESHOLD
                                          levels:LEVELS];

CVSquaresWrapper.h

//  CVSquaresWrapper.h

#import <Foundation/Foundation.h>

@interface CVSquaresWrapper : NSObject

+ (UIImage*) detectedSquaresInImage:(UIImage*)image
                          tolerance:(CGFloat)tolerance
                          threshold:(NSInteger)threshold
                             levels:(NSInteger)levels;

@end

CVSquaresWrapper.mm

//  CVSquaresWrapper.mm
//  wrapper that talks to c++ and to obj-c classes

#import "CVSquaresWrapper.h"
#import "CVSquares.h"
#import "UIImage+OpenCV.h"

@implementation CVSquaresWrapper

+ (UIImage*) detectedSquaresInImage:(UIImage*) image
                          tolerance:(CGFloat)tolerance
                          threshold:(NSInteger)threshold
                             levels:(NSInteger)levels
{
    UIImage* result = nil;

        //convert from UIImage to cv::Mat openCV image format
        //this is a category on UIImage
    cv::Mat matImage = [image CVMat]; 


        //call the c++ class static member function
        //we want this function signature to exactly 
        //mirror the form of the calling method 
    matImage = CVSquares::detectedSquaresInImage (matImage, tolerance, threshold, levels);


        //convert back from cv::Mat openCV image format
        //to UIImage image format (category on UIImage)
    result = [UIImage imageFromCVMat:matImage]; 

    return result;
}

@end

CVSquares.h

//  CVSquares.h

#ifndef __OpenCVClient__CVSquares__
#define __OpenCVClient__CVSquares__

    //class definition
    //in this example we do not need a class 
    //as we have no instance variables and just one static function. 
    //We could instead just declare the function but this form seems clearer

class CVSquares
{
public:
    static cv::Mat detectedSquaresInImage (cv::Mat image, float tol, int threshold, int levels);
};

#endif /* defined(__OpenCVClient__CVSquares__) */

CVSquares.cpp

//  CVSquares.cpp

#include "CVSquares.h"

using namespace std;
using namespace cv;

static int thresh = 50, N = 11;
static float tolerance = 0.01;

    //declarations added so that we can move our 
    //public function to the top of the file
static void findSquares(  const Mat& image,   vector<vector<Point> >& squares );
static void drawSquares( Mat& image, vector<vector<Point> >& squares );

    //this public function performs the role of 
    //main{} in the original file (main{} is deleted)
cv::Mat CVSquares::detectedSquaresInImage (cv::Mat image, float tol, int threshold, int levels)
{
    vector<vector<Point> > squares;

    if( image.empty() )
        {
        cout << "Couldn't load " << endl;
        }

    tolerance = tol;
    thresh = threshold;
    N = levels;
    findSquares(image, squares);
    drawSquares(image, squares);

    return image;
}


// the rest of this file is identical to the original squares.cpp except:
// main{} is removed
// this line is removed from drawSquares: 
// imshow(wndname, image); 
// (obj-c will do the drawing)

UIImage+OpenCV.h

UIImage 类别是一个 objC++ 文件,包含在 UIImage 和 cv::Mat 图像格式之间转换的代码。这是您移动两种方法的地方-(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat and - (cv::Mat)cvMatWithImage:(UIImage *)image

//UIImage+OpenCV.h

#import <UIKit/UIKit.h>

@interface UIImage (UIImage_OpenCV)

    //cv::Mat to UIImage
+ (UIImage *)imageFromCVMat:(cv::Mat&)cvMat;

    //UIImage to cv::Mat
- (cv::Mat)CVMat;


@end        

这里的方法实现与您的代码没有变化(尽管我们没有传递 UIImage 进行转换,而是引用self)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS:从背景图像中检索矩形图像 的相关文章

  • 两者都实现了类。将使用两者之一

    我有一个项目 它具有使用 SocketRocket 的依赖项 通过 CocoaPods 安装 并从 HeapAnalytics 导入了静态库 显然 HeapAnalytics 库已经使用了 SocketRocket 编译时没有出现错误 但在
  • 防止UIScrollView的UIPanGestureRecognizer遮挡UIScreenEdgePanGestureRecognizer

    我有一个UIScrollView它填满了我应用程序的一页上的屏幕 但我希望允许用户从屏幕边缘平移以显示其后面的视图 问题是 UIScrollView 窃取了我的触摸UIScreenEdgePanGestureRecognizer在屏幕边缘
  • iOS - NSNotificationCenter 多个UIKeyboard通知

    我有两个视图控制器 我们称它们为 A 和 B 1 在 A 中 我显示一个包含文本字段的 popOver 2 B中有一个UITextView用于简单的文本编辑 我必须管理 A 和 B 中的键盘才能滚动键盘隐藏的内容 我知道如何重新定位内容 我
  • 是什么导致了这个 iPhone 崩溃日志?

    我有点卡住了 需要解决这个问题 因为我的一个应用程序出现了随机崩溃 而这些崩溃并不总是能够重现 这是崩溃日志之一 Incident Identifier 59865612 9F00 44EA 9474 2BF607AD662E CrashR
  • OpenCV SIFT 描述符关键点半径

    我正在深入研究OpenCV的SIFT描述符提取的实现 https github com Itseez opencv blob master modules nonfree src sift cpp 我发现了一些令人费解的代码来获取兴趣点邻域
  • VideoCapture.read() 返回过去的图像

    我在跑python3 6 with openCV on the Raspberry pi OS is Raspbian 代码的大致结构如下 The image以时间间隔 3 5 分钟 捕获 被捕获image在函数中处理并返回度量 精度的种类
  • 如何从代码隐藏中设置 CarouselView 的项目?

    我有一个 CarouselView 它绑定到图像的 ItemsSource 但我想通过更改 CarouselView 的索引来更改当前显示的图像 我尝试使用 CarouselView Position 作为必须选择的元素的索引 但不幸的是这
  • 如何请求用户开启定位服务

    我需要我的应用程序来访问用户的当前位置 它在应用程序开始时检查用户是否已设置 如果没有 我需要应用程序显示提示以使其使用位置服务 就像警报视图一样 点击按钮 它应该会带您进入 iPhone 上的位置服务屏幕 您可以通过以下代码检查 loca
  • 使用 Google place API 从 lat long 获取附近的地点

    我正在使用 google place API 即 https maps googleapis com maps api place search json location 33 7167 73 0667 radius 500 type f
  • 无法使用 Xamarin 和 WCF 访问 Web 服务

    我想使用 Xamarin 和 WCF 来使用公共 Web 服务 对于这个演示 我将使用Xamarin iOS 这是我试图使用的 公共 网络服务 http www webservicex net globalweather asmx WSDL
  • 为什么我的 tableView 函数运行了 3 次?

    我有一个 UITableView 并且有执行它所需的方法之一 func tableView tableView UITableView numberOfRowsInSection section Int gt Int println sec
  • 在 Swift 中自动移动 UISlider

    我想在按下按钮时将 UISlider 从 minValue 循环移动到 maxValue 并在再次按下按钮时将其停止在当前位置 我想使用 Swift 我遇到的主要问题是函数 slider setValue 太快了 我希望动画更慢 IBAct
  • 如何使用 Swift 获取 YouTube 频道的所有播放列表?

    我的问题不是关于从一般频道检索视频 我只想获取该频道创建的所有 播放列表 并检索每个播放列表的缩略图 标题和视频数量 这是一个 YouTube 频道示例 正如您所看到的 有很多已创建的播放列表 截至目前 我只能获取某个频道最新上传的视频 在
  • 混合静态和动态 UITableViewController 内容会导致 NSRangeException

    我一直在寻找这个错误 并找到了一些具有类似行为的帖子 但没有解决问题的解决方案 我有一个 UITableViewController 在 SB 中声明为静态 它具有以下部分 第 0 部分 配方 是静态的 有 4 个单元格 第 1 部分 口味
  • 使用 UIImageJPEGRepresentation 时,compressionQuality 应该是多少?

    我想对用户库中的照片应用滤镜 然后将其写回磁盘 我在用着UIImageJPEGRepresentation 该函数需要一个UIImage and a compressionQuality值介于 0 0 和 1 0 之间 因为我想保留原始质量
  • 检查 touchend 是否在拖动后出现

    我有一些代码可以更改表的类 在手机上 有时表格对于屏幕来说太宽 用户将拖动 滚动来查看内容 但是 当他们触摸并拖动表格时 每次拖动都会触发 touchend 如何测试触摸端是否是触摸拖动的结果 我尝试跟踪dragstart和dragend
  • 我什么时候应该对 IBOutlet 使用弱或强限定符? [复制]

    这个问题在这里已经有答案了 可能的重复 ARC 下 IBOutlets 应该强还是弱 https stackoverflow com questions 7678469 should iboutlets be strong or weak
  • OpenCv读/写视频色差

    我试图简单地使用 openCV 打开视频 处理帧并将处理后的帧写入新的视频文件 我的问题是 即使我根本不处理帧 只是打开视频 使用 VideoCapture 读取帧并使用 VideoWriter 将它们写入新文件 输出文件看起来比输入更 绿
  • 如何更改已上传的 Firebase 存储图像文件名?

    我需要更改已上传到 firebase 存储中的文件名 因为 在 firebase 存储中上传图像后 我将 url 保存在 firebase 数据库中的特定子 文件夹 下 但是 当我将图像移动到另一个子 文件夹 时 我需要根据新名称更改存储中
  • Swift 中通过不同类调用委托方法

    我正在获取 JSON 菜单 一旦 JSON 返回 我想运行 menuReady 来更新表的内容在 SomeTableViewController 类中 但下面的代码似乎不起作用 AIM Run 菜单就绪 JSON 返回后更新内容 PROBL

随机推荐

  • MFC中如何将BYTE数组转换为CString?

    如何在 MFC 中将 BYTE 数组转换为 CString 试试这个 例如 如果 x 是你的字节数组 那么 BYTE x 5 x 0 A x 1 0 x 2 B x 3 C x 4 0 CString str LPCSTR x sizeof
  • 如何使用 ASP.NET 身份在 Web API 2 中实现两因素身份验证?

    我看过这个链接使用谷歌身份验证器的两因素身份验证 http bitoftech net 2014 10 15 two factor authentication asp net web api angularjs google authen
  • 我们如何在phantomjs中使用evaluateAsync

    有什么用evaluateAsync以及什么时候我们必须使用这个功能以及使用这个功能有什么好处 在下面我们看到一个可怜的文档 http phantomjs org api webpage method evaluate async html为
  • Android 连接 Twitter - 从 Twitter 获取空答案

    我在连接 Twitter 时遇到 5 问题 代码贴在下面 首先 我已经配置了所有内容 twitter 键 清单中的回调等 然后我调用 twitter 并打开浏览器 然后我登录 twitter 并接受应用程序 然后浏览器返回到应用程序并尝试从
  • kivy:可以使用缓冲区作为图像源吗?

    我有如下代码 它可以从一些现有图像中生成新图像 from PIL import Image as pyImage def create compound image back image path fore image path fore
  • 查找特定文件最频繁的提交者

    给定 git 存储库中的特定文件 我如何找到该文件中最频繁的提交者 您可以使用git 短日志 https git scm com docs git shortlog为了这 git shortlog sn path to file 这将打印出
  • 为什么WPML的“字符串翻译”中没有出现字符串?

    我的 php 文件中有这样的字符串 并且没有出现在 字符串翻译 中 我也购买并安装了这个主题 并且该主题中的字符串也不会出现在 WPML 的 字符串翻译 中 这是我安装时已经在主题中的字符串示例
  • 如何将 NSArray 转换为 NSMutableArray

    ABAddressBookRef addressBook ABAddressBookCreate CFArrayRef allPeople ABAddressBookCopyArrayOfAllPeople addressBook CFIn
  • 在 HTML 中使用 SVG 字形标签

    我有一个带有以下来源的 icon svg 文件
  • Lion + Xcode 3.2.3 启动问题

    我刚刚从图书馆得到一本基于 Xcode 3 x 的 iPhone 编程书籍 所以我想从它开始 幸运的是 我从 SnowLeopard 更新到 Lion 时已经安装了 Xcode 因此安装或运行它时没有遇到任何问题 因此 我通读了 Hello
  • RecyclerView 中检测到不一致,如何在滚动时更改 RecyclerView 的内容

    我在用着RecyclerView显示项目的名称 我的行包含单个TextView 项目名称存储在List
  • 列表视图项目内的 UWP 按钮

    I m working on my first UWP app and I want create a UI like this For each list item project there ll be a set of buttons
  • jQuery/Javascript - 使用附加的查询字符串重新加载当前页面?

    我的表单上有一个下拉菜单 当选择某些内容时 我需要重新加载当前页面 但带有附加的查询字符串 我该怎么做呢 这是一个老问题 但它首先出现在谷歌搜索结果中 我采用的解决方案与 jAndy 的类似 window location pathname
  • XCode 库搜索路径

    对于我的 iOS 项目 我有一个特定的文件夹结构 root src xcode MyProject
  • 提取两个给定字符串之间的文本

    希望有人可以帮助我 现在已经遍布谷歌了 我正在对文档进行一些区域 ocr 并且想使用正则表达式提取一些文本 总是这样 直到 姓名 姓名 org nr 12323123 我想提取名称部分 它可以是 1 4 个名称 但 Til 和 org nr
  • 大数组的堆栈溢出,但同样大的向量的堆栈溢出?

    今天我在处理大型数据结构时遇到了一个有趣的问题 我最初使用向量来存储超过 1000000 个整数 但后来决定我实际上并不需要向量的动态功能 无论如何 我在声明后就保留了 1000000 个位置 相反 这将是有益的 能够在数据结构中的任何位置
  • 如何禁用 PHP 中的线程安全?

    我正在使用一些需要禁用线程安全的软件 我正在 Windows 服务器上工作 根据我在其他地方读到的内容 我不能只在 ini 文件中配置它 这是真的 如果是这样 我将如何编译它以关闭线程安全 您必须在禁用 ZTS 的情况下编译 PHP 编译标
  • MATLAB 中的 .m 和 .mat 文件有什么区别

    当我跟踪我的参考 MATLAB 脚本时 我发现了带有以下内容的文件 mat扩大 我的问题是 有什么区别 mat and m files 如何使用打开文件 mat扩大 扩展名为 m 的文件包含 MATLAB 代码 其形式为script htt
  • 移动设备上的 jQuery 实时滚动事件(解决方法)

    老问题 当用户在移动网站或应用程序 Web 视图 上滚动元素时触发滚动事件 我所寻找的只是访问正确的scrollTop 当用户在移动设备上滚动我的页面时获取该值 而不是在用户停止时获取它 我确信在某个地方有一个解决方法 如果我是正确的话 这
  • iOS:从背景图像中检索矩形图像

    我正在开发一个实现 其中我在大背景图像中有一个矩形图像 我正在尝试以编程方式从大图像中检索矩形图像 并从该特定矩形图像中检索文本信息 我正在尝试使用 Open CV 第三方框架 但无法从大背景图像中检索矩形图像 有人可以指导我 我怎样才能实