Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用于XCode的Objective-C中的openCV_C++_Ios_Objective C_Xcode_Opencv - Fatal编程技术网

C++ 用于XCode的Objective-C中的openCV

C++ 用于XCode的Objective-C中的openCV,c++,ios,objective-c,xcode,opencv,C++,Ios,Objective C,Xcode,Opencv,我想在我的项目中使用openCV。我遵循的步骤是: 1) 从此处下载openCV iOS Framework 2.4.11: 2) 从下载的zip文件中提取openCV2框架后,将其拖放 3) 在XCode中打开了Objective-CIOS应用程序项目,我在其中创建了一个PrefixHeader.pch文件。在构建设置中,我粘贴.pch的位置,并将预编译前缀标题保留为“否” 我在.pch文件中编写以下代码: #ifdef __cplusplus #import <opencv2/open

我想在我的项目中使用openCV。我遵循的步骤是:

1) 从此处下载openCV iOS Framework 2.4.11:

2) 从下载的zip文件中提取openCV2框架后,将其拖放

3) 在XCode中打开了Objective-CIOS应用程序项目,我在其中创建了一个PrefixHeader.pch文件。在构建设置中,我粘贴.pch的位置,并将预编译前缀标题保留为“否”

我在.pch文件中编写以下代码:

#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif

5)我将VIEW Controller,m重命名为.mm,使其能够与OpenCV的C++兼容。然后,我将我的openCV代码放在如下位置:

UIImage* image = [UIImage imageNamed:@"lena.png"];

// Convert UIImage* to cv::Mat
UIImageToMat(image, cvImage);

if (0)
{

    NSString* filePath = [[NSBundle mainBundle]
                          pathForResource:@"lena" ofType:@"png"];
    // Create file handle
    NSFileHandle* handle =
    [NSFileHandle fileHandleForReadingAtPath:filePath];
    // Read content of the file
    NSData* data = [handle readDataToEndOfFile];
    // Decode image from the data buffer
    cvImage = cv::imdecode(cv::Mat(1, [data length], CV_8UC1,
                                   (void*)data.bytes),
                           CV_LOAD_IMAGE_UNCHANGED);
}

if (0)
{
    NSData* data = UIImagePNGRepresentation(image);
    // Decode image from the data buffer
    cvImage = cv::imdecode(cv::Mat(1, [data length], CV_8UC1,
                                   (void*)data.bytes),
                           CV_LOAD_IMAGE_UNCHANGED);
}

if (!cvImage.empty())
{
    cv::Mat gray;
    // Convert the image to grayscale
    cv::cvtColor(cvImage, gray, CV_RGBA2GRAY);
    // Apply Gaussian filter to remove small edges
    cv::GaussianBlur(gray, gray,
                     cv::Size(5, 5), 1.2, 1.2);
    // Calculate edges with Canny
    cv::Mat edges;
    cv::Canny(gray, edges, 0, 50);
    // Fill image with white color
    cvImage.setTo(cv::Scalar::all(255));
    // Change color on edges
    cvImage.setTo(cv::Scalar(0, 128, 255, 255), edges);
    // Convert cv::Mat to UIImage* and show the resulting image
    imageView.image = MatToUIImage(cvImage);

当我构建项目时,它会给我一个构建未正确链接的错误,以及使用未声明的标识符“cvImage”。有人能给出一个运行opencv2框架iOS应用程序的分步程序吗

您的构建参数是什么?(和链接器标志?)实际上我解决了这个问题。这是我的ViewController.h中丢失的一个链接。但是,我所面临的一个错误是它不能读取JPEG文件,并且一些PNG文件未被检测到。您当前正在编译Objc还是C++?另外,您用于opencv的图像加载后端是什么?(ffmpeg?)我正在openCV中使用UIImage到Mat的转换。无效UIImageToMat(常量UIImage*图像,cv::Mat&m,布尔字母EXIST=false);这不起作用
UIImage* image = [UIImage imageNamed:@"lena.png"];

// Convert UIImage* to cv::Mat
UIImageToMat(image, cvImage);

if (0)
{

    NSString* filePath = [[NSBundle mainBundle]
                          pathForResource:@"lena" ofType:@"png"];
    // Create file handle
    NSFileHandle* handle =
    [NSFileHandle fileHandleForReadingAtPath:filePath];
    // Read content of the file
    NSData* data = [handle readDataToEndOfFile];
    // Decode image from the data buffer
    cvImage = cv::imdecode(cv::Mat(1, [data length], CV_8UC1,
                                   (void*)data.bytes),
                           CV_LOAD_IMAGE_UNCHANGED);
}

if (0)
{
    NSData* data = UIImagePNGRepresentation(image);
    // Decode image from the data buffer
    cvImage = cv::imdecode(cv::Mat(1, [data length], CV_8UC1,
                                   (void*)data.bytes),
                           CV_LOAD_IMAGE_UNCHANGED);
}

if (!cvImage.empty())
{
    cv::Mat gray;
    // Convert the image to grayscale
    cv::cvtColor(cvImage, gray, CV_RGBA2GRAY);
    // Apply Gaussian filter to remove small edges
    cv::GaussianBlur(gray, gray,
                     cv::Size(5, 5), 1.2, 1.2);
    // Calculate edges with Canny
    cv::Mat edges;
    cv::Canny(gray, edges, 0, 50);
    // Fill image with white color
    cvImage.setTo(cv::Scalar::all(255));
    // Change color on edges
    cvImage.setTo(cv::Scalar(0, 128, 255, 255), edges);
    // Convert cv::Mat to UIImage* and show the resulting image
    imageView.image = MatToUIImage(cvImage);