mac 安裝opencv homebrew安裝

努力的草根發表於2014-09-07

0 Homebrew是啥?

“Homebrew installs the stuff you need that Apple didn’t.——Homebrew 使 OS X 更完整”

Homebrew的官網[1](多語言版本)簡單明瞭地介紹瞭如何安裝和使用這個工具,並提供了自己的Wiki。


1 安裝Homebrew 

brew的安裝很簡單,使用一條ruby命令即可,Mac系統上已經預設安裝了ruby。

gerryyang@mba:bin$ruby --version
ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]

關於安裝brew的詳細說明可以參考其github上的wiki說明[2]

"
Installation

The suggested and easiest way to install Homebrew is on the homepage. We don’t duplicate it here because it is asecurity risk to list it on a user-editable wiki.

The standard script installs Homebrew to /usr/local so that you don’t need sudo when youbrew install. It is acareful script, it can be run even if you have stuff installed to/usr/local already. It tells you exactly what it will do before itdoes it too. And you have to confirm everything it will do before itstarts.

There are other ways to install Homebrew which provide you with moreflexibility. They are listed below the requirements.

"
  1. gerryyang@mba:~$ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"  
  2. It appears Homebrew is already installed. If your intent is to reinstall you  
  3. should do the following before running this installer again:  
  4.   
  5.     rm -rf /usr/local/Cellar /usr/local/.git && brew cleanup  

由於我的系統上已經安裝過brew,因此再次執行安裝命令會提示我brew已經存在。



2 Homebrew的用法 

檢視brew的具體用法:

  1. gerryyang@mba:~$brew  
  2. Example usage:  
  3.   brew [info | home | options ] [FORMULA...]  
  4.   brew install FORMULA...  
  5.   brew uninstall FORMULA...  
  6.   brew search [foo]  
  7.   brew list [FORMULA...]  
  8.   brew update  
  9.   brew upgrade [FORMULA...]  
  10.   brew pin/unpin [FORMULA...]  
  11.   
  12. Troubleshooting:  
  13.   brew doctor  
  14.   brew install -vd FORMULA  
  15.   brew [--env | --config]  
  16.   
  17. Brewing:  
  18.   brew create [URL [--no-fetch]]  
  19.   brew edit [FORMULA...]  
  20.   open https://github.com/Homebrew/homebrew/wiki/Formula-Cookbook  
  21.   
  22. Further help:  
  23.   man brew  
  24.   brew home  
  25.   
  26. gerryyang@mba:~$  


在安裝好brew後,只需要一條命令就可以安裝OpenCV了:

1
brew install opencv

通常情況下這樣做就應該會安裝成功,但我在公司和家裡面的電腦嘗試的時候,brew都會報一些錯誤,我遇到的都是一些小問題,按照brew的提示資訊,解決掉相應的問題即可。

安裝成功後,你應該可以在“/usr/local/include”目錄下找到名為opencv和opencv2的目錄,這裡面是OpenCV相關的標頭檔案。你也可以在“/usr/local/lib”目錄下找到許多以libopencv_開頭的.dylib檔案,這些是OpenCV的連結庫檔案。

這裡有一個技巧,因為 /usr 目錄在對話方塊中預設不是可見的,可以按快捷鍵 command + shift + G,在彈出的“前往資料夾”對話方塊中輸入 /usr/local/lib ,即可跳轉到目標資料夾。如下圖所示:

下一步是我自己試出來的,對於Lion作業系統,你需要在Build Settings中,將“C++ Language Dialect”設定成C++11,將“C++ Standard Library”設定成libstdc++ ,如下圖所示。個人感覺是由於XCode預設設定的GNU++11、libc++與OpenCV庫有一些相容性問題,我在更改該設定前老是出現編譯錯誤。後續版本在Montain Lion系統中解決了這個問題,不用進行這一步了。

把上面的設定都做好後,就可以在需要的使用OpenCV庫的地方,加上opencv的標頭檔案引用即可:

1
#import "opencv2/opencv.hpp"

注意,如果你的原始檔副檔名是.m的,你還需要改成.mm,這樣編譯器才知道你將會在該檔案混合使用C++語言和Objective-C語言。

OpenCV處理圖象需要的格式是cv::Mat類,而MacOS的圖象格式預設是NSImage,所以你需要知道如何在cv::Mat與NSImage之前相互轉換。如下是一個NSImage的Addition,你肯定會需要它的。該程式碼來自stackoverflow上的這個貼子

NSImage+OpenCV.h 檔案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
//  NSImage+OpenCV.h
//
//  Created by TangQiao on 12-10-26.
//

#import <Foundation/Foundation.h>
#import "opencv2/opencv.hpp"

@interface NSImage (OpenCV)

+(NSImage*)imageWithCVMat:(const cv::Mat&)cvMat;
-(id)initWithCVMat:(const cv::Mat&)cvMat;

@property(nonatomic, readonly) cv::Mat CVMat;
@property(nonatomic, readonly) cv::Mat CVGrayscaleMat;

@end

NSImage+OpenCV.mm檔案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
//  NSImage+OpenCV.mm
//
//  Created by TangQiao on 12-10-26.
//

#import "NSImage+OpenCV.h"

static void ProviderReleaseDataNOP(void *info, const void *data, size_t size)
{
    return;
}


@implementation NSImage (OpenCV)

-(CGImageRef)CGImage
{
    CGContextRef bitmapCtx = CGBitmapContextCreate(NULL/*data - pass NULL to let CG allocate the memory*/,
                                                   [self size].width,
                                                   [self size].height,
                                                   8 /*bitsPerComponent*/,
                                                   0 /*bytesPerRow - CG will calculate it for you if it's allocating the data.  This might get padded out a bit for better alignment*/,
                                                   [[NSColorSpace genericRGBColorSpace] CGColorSpace],
                                                   kCGBitmapByteOrder32Host|kCGImageAlphaPremultipliedFirst);

    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:bitmapCtx flipped:NO]];
    [self drawInRect:NSMakeRect(0,0, [self size].width, [self size].height) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
    [NSGraphicsContext restoreGraphicsState];

    CGImageRef cgImage = CGBitmapContextCreateImage(bitmapCtx);
    CGContextRelease(bitmapCtx);

    return cgImage;
}


-(cv::Mat)CVMat
{
    CGImageRef imageRef = [self CGImage];
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
    CGFloat cols = self.size.width;
    CGFloat rows = self.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), imageRef);
    CGContextRelease(contextRef);
    CGImageRelease(imageRef);
    return cvMat;
}

-(cv::Mat)CVGrayscaleMat
{
    CGImageRef imageRef = [self CGImage];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGFloat cols = self.size.width;
    CGFloat rows = self.size.height;
    cv::Mat cvMat = cv::Mat(rows, cols, CV_8UC1); // 8 bits per component, 1 channel
    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
                                                    kCGImageAlphaNone |
                                                    kCGBitmapByteOrderDefault); // Bitmap info flags

    CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), imageRef);
    CGContextRelease(contextRef);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);
    return cvMat;
}

+ (NSImage *)imageWithCVMat:(const cv::Mat&)cvMat
{
    return [[[NSImage alloc] initWithCVMat:cvMat] autorelease];
}

- (id)initWithCVMat:(const 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((CFDataRef)data);

    CGImageRef imageRef = CGImageCreate(cvMat.cols,                                     // Width
                                        cvMat.rows,                                     // Height
                                        8,                                              // Bits per component
                                        8 * cvMat.elemSize(),                           // Bits per pixel
                                        cvMat.step[0],                                  // Bytes per row
                                        colorSpace,                                     // Colorspace
                                        kCGImageAlphaNone | kCGBitmapByteOrderDefault,  // Bitmap info flags
                                        provider,                                       // CGDataProviderRef
                                        NULL,                                           // Decode
                                        false,                                          // Should interpolate
                                        kCGRenderingIntentDefault);                     // Intent


    NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:imageRef];
    NSImage *image = [[NSImage alloc] init];
    [image addRepresentation:bitmapRep];

    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);

    return image;
}

@end

完成以上步驟後,恭喜你,你可以在原始碼中自由地呼叫OpenCV的函式了。



3 Homebrew的有趣問題

下面關於Homebrew的一些常見問題[3]

(1) 在OS X中找不到想要的軟體,可以使用brew安裝;

  1. gerryyang@mba:~$brew install wget  
  2. ==> Downloading http://ftpmirror.gnu.org/wget/wget-1.14.tar.gz  
  3. ######################################################################## 100.0%  
  4. ==> ./configure --prefix=/usr/local/Cellar/wget/1.14 --sysconfdir=/usr/local/etc  
  5. ==> make install  
  6. Warning: Could not link wget. Unlinking...  
  7. Error: The `brew link` step did not complete successfully  
  8. The formula built, but is not symlinked into /usr/local  
  9. You can try again using `brew link wget'  
  10.   
  11. Possible conflicting files are:  
  12. /usr/local/bin/wget  
  13. /usr/local/share/man/man1/wget.1  
  14. ==> Summary  
  15. <span style="font: 13.0px Monaco">/usr/local/Cellar/wget/1.14: 8 files, 688K, built in 2.3 minutes</span>   

由於之前使用原始碼方式安裝過wget,因此再用brew安裝時會提示衝突的錯誤,因為/usr/locat/bin/wget已經存在。實際上,Homebrew 將軟體包分裝到單獨的目錄(/usr/local/Cellar),然後 symlink 到 /usr/local 中。 

(2) 如何更新本地已安裝的package呢?

第一步:先更新brew:

brew update

  1. gerryyang@mba:bin$brew update  
  2. Already up-to-date.  

第二步:更新所有package:
brew upgarde

  1. gerryyang@mba:bin$brew upgrade  
  2. ==> Upgrading 1 outdated package, with result:  
  3. wget 1.15  
  4. ==> Upgrading wget  
  5. ==> Installing wget dependency: openssl  
  6. ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/openssl-1.0  
  7. ######################################################################## 100.0%  
  8. ==> Pouring openssl-1.0.1f.mavericks.bottle.tar.gz  
  9. ==> Caveats  
  10. This formula is keg-only, so it was not symlinked into /usr/local.  
  11.   
  12. Mac OS X already provides this software and installing another version in  
  13. parallel can cause all kinds of trouble.  
  14.   
  15. The OpenSSL provided by OS X is too old for some software.  
  16.   
  17. Generally there are no consequences of this for you. If you build your  
  18. own software and it requires this formula, you'll need to add to your  
  19. build variables:  
  20.   
  21.     LDFLAGS:  -L/usr/local/opt/openssl/lib  
  22.     CPPFLAGS: -I/usr/local/opt/openssl/include  
  23.   
  24. ==> security find-certificate -a -p /Library/Keychains/System.keychain > '/usr/l  
  25. ==> security find-certificate -a -p /System/Library/Keychains/SystemRootCertific  
  26. ==> mv -f /usr/local/etc/openssl/osx_cert.pem.tmp /usr/local/etc/openssl/osx_cer  
  27. ==> Summary  
  28. /usr/local/Cellar/openssl/1.0.1f: 429 files, 15M  
  29. ==> Installing wget  
  30. ==> Downloading http://ftpmirror.gnu.org/wget/wget-1.15.tar.gz  
  31. ######################################################################## 100.0%  
  32. ==> ./configure --prefix=/usr/local/Cellar/wget/1.15 --sysconfdir=/usr/local/etc --with-ssl=openssl --with-libssl-prefix=/usr/local/opt/openssl --disable-iri  
  33. ==> make install  
  34. Warning: Could not link wget. Unlinking...  
  35. Error: The `brew link` step did not complete successfully  
  36. The formula built, but is not symlinked into /usr/local  
  37. You can try again using `brew link wget'  
  38.   
  39. Possible conflicting files are:  
  40. /usr/local/bin/wget  
  41. /usr/local/share/man/man1/wget.1  
  42. ==> Summary  
  43. /usr/local/Cellar/wget/1.15: 8 files, 700K, built in 73 seconds  
  44. gerryyang@mba:bin$  


(3) 如何刪除Homebrew?

If you installed to /usr/local then you can use the script in thisgist to uninstall — it will onlyremove Homebrew and the stuff Homebrew installed leaving anything elsein/usr/local alone.

Provided you haven’t put anything else in Homebrew’s prefix(brew --prefix), you can generally justrm -rf that directory. Thisis because Homebrew won’t touch files outside its prefix.


(4) Homebrew下載的package存放的路徑在哪裡?

  1. gerryyang@mba:bin$brew --cache  
  2. /Library/Caches/Homebrew  
  3. gerryyang@mba:bin$ls /Library/Caches/Homebrew/  
  4. Formula/                                wget-1.14.tar.gz  
  5. coreutils-8.22.tar.xz                  wget-1.15.tar.gz  
  6. openssl-1.0.1f.mavericks.bottle.tar.gz  xz-5.0.5.mavericks.bottle.2.tar.gz  
  7. gerryyang@mba:bin$ls /Library/Caches/Homebrew/Formula/  
  8. coreutils.brewing  openssl.brewing  wget.brewing  xz.brewing  
  9.   
  10. gerryyang@mba:bin$  

(5) 我可以安裝自己的stuff在/usr/local嗎?

Yes, brew is designed to not get in your way so you can use it how youlike.

Install your own stuff, but be aware that if you install commonlibraries, like libexpat yourself, it may cause trouble when trying tobuild certain Homebrew formula. As a resultbrew doctor will warn youabout this.

Thus it’s probably better to install your own stuff to the Cellar andthen brew link it. Like so:

$ cd foo-0.1
$ brew diy
./configure —prefix=/usr/local/Cellar/foo/0.1
$ ./configure —prefix=/usr/local/Cellar/foo/0.1
[snip]
$ make && make install
$ brew link foo
Linking /usr/local/Cellar/foo/0.1… 17 symlinks created

在我的環境上檢測是這樣的:

  1. gerryyang@mba:bin$brew doctor  
  2. Warning: Unbrewed dylibs were found in /usr/local/lib.  
  3. If you didn't put them there on purpose they could cause problems when  
  4. building Homebrew formulae, and may need to be deleted.  
  5.   
  6. Unexpected dylibs:  
  7.     /usr/local/lib/libboost_atomic.dylib  
  8.     /usr/local/lib/libboost_chrono.dylib  
  9.     /usr/local/lib/libboost_context.dylib  
  10.     /usr/local/lib/libboost_date_time.dylib  
  11.     /usr/local/lib/libboost_filesystem.dylib  
  12.     /usr/local/lib/libboost_graph.dylib  
  13.     /usr/local/lib/libboost_iostreams.dylib  
  14.     /usr/local/lib/libboost_locale.dylib  
  15.     /usr/local/lib/libboost_log.dylib  
  16.     /usr/local/lib/libboost_math_c99.dylib  
  17.     /usr/local/lib/libboost_math_c99f.dylib  
  18.     /usr/local/lib/libboost_math_c99l.dylib  
  19.     /usr/local/lib/libboost_math_tr1.dylib  
  20.     /usr/local/lib/libboost_math_tr1f.dylib  
  21.     /usr/local/lib/libboost_math_tr1l.dylib  
  22.     /usr/local/lib/libboost_prg_exec_monitor.dylib  
  23.     /usr/local/lib/libboost_program_options.dylib  
  24.     /usr/local/lib/libboost_python.dylib  
  25.     /usr/local/lib/libboost_random.dylib  
  26.     /usr/local/lib/libboost_regex.dylib  
  27.     /usr/local/lib/libboost_serialization.dylib  
  28.     /usr/local/lib/libboost_signals.dylib  
  29.     /usr/local/lib/libboost_system.dylib  
  30.     /usr/local/lib/libboost_thread.dylib  
  31.     /usr/local/lib/libboost_timer.dylib  
  32.     /usr/local/lib/libboost_unit_test_framework.dylib  
  33.     /usr/local/lib/libboost_wave.dylib  
  34.     /usr/local/lib/libboost_wserialization.dylib  
  35.   
  36. Warning: Unbrewed static libraries were found in /usr/local/lib.  
  37. If you didn't put them there on purpose they could cause problems when  
  38. building Homebrew formulae, and may need to be deleted.  
  39.   
  40. Unexpected static libraries:  
  41.     /usr/local/lib/libboost_atomic.a  
  42.     /usr/local/lib/libboost_chrono.a  
  43.     /usr/local/lib/libboost_context.a  
  44.     /usr/local/lib/libboost_coroutine.a  
  45.     /usr/local/lib/libboost_date_time.a  
  46.     /usr/local/lib/libboost_exception.a  
  47.     /usr/local/lib/libboost_filesystem.a  
  48.     /usr/local/lib/libboost_graph.a  
  49.     /usr/local/lib/libboost_iostreams.a  
  50.     /usr/local/lib/libboost_locale.a  
  51.     /usr/local/lib/libboost_log.a  
  52.     /usr/local/lib/libboost_math_c99.a  
  53.     /usr/local/lib/libboost_math_c99f.a  
  54.     /usr/local/lib/libboost_math_c99l.a  
  55.     /usr/local/lib/libboost_math_tr1.a  
  56.     /usr/local/lib/libboost_math_tr1f.a  
  57.     /usr/local/lib/libboost_math_tr1l.a  
  58.     /usr/local/lib/libboost_prg_exec_monitor.a  
  59.     /usr/local/lib/libboost_program_options.a  
  60.     /usr/local/lib/libboost_python.a  
  61.     /usr/local/lib/libboost_random.a  
  62.     /usr/local/lib/libboost_regex.a  
  63.     /usr/local/lib/libboost_serialization.a  
  64.     /usr/local/lib/libboost_signals.a  
  65.     /usr/local/lib/libboost_system.a  
  66.     /usr/local/lib/libboost_test_exec_monitor.a  
  67.     /usr/local/lib/libboost_thread.a  
  68.     /usr/local/lib/libboost_timer.a  
  69.     /usr/local/lib/libboost_unit_test_framework.a  
  70.     /usr/local/lib/libboost_wave.a  
  71.     /usr/local/lib/libboost_wserialization.a  
  72.   
  73. Warning: You have unlinked kegs in your Cellar  
  74. Leaving kegs unlinked can lead to build-trouble and cause brews that depend on  
  75. those kegs to fail to run properly once built. Run `brew link` on these:  
  76.   
  77.     wget  
  78. gerryyang@mba:bin$  

(6) 為什麼選擇Homebrew這個名字?
mxcl was too concerned with the beer theme and didn’t consider that the project may actually prove popular. By the time he realized it was too late. However, today, the first google hit for “homebrew” is not beer related ;-)

看來作者是個酒鬼!

(7) 關於更多地QA可以參考Homebrew的Wiki[4]


(8) 一些常用安裝推薦

  1. gerryyang@mba:bin$brew list  
  2. coreutils  openssl  wget  xz  

Introduction to Coreutils 

The GNU Core Utilities are the basic file, shell and text manipulationutilities of the GNU operating system.
These are the core utilitieswhich are expected to exist on every operating system.


相關文章