【OpenCV笔记一】Mac安装OpenCV环境及demo

1.安装cmake
# brew install cmake

2.安装OpenCV
# brew tap homebrew/science
# brew install opencv
//opencv升级
# brew upgrade opencv

3.配置OpenCV环境: vi /etc/profile添加
#Opencv
export OpenCV_DIR=/usr/local/Cellar/opencv/3.3.1
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/Cellar/opencv/3.3.1_1/lib/pkgconfig
export PKG_CONFIG_PATH
export LD_LIBRARY_PATH=/usr/local/Cellar/opencv/3.3.1_1/bin:SLD_LIBRARY_PATH
export PATH=${PATH}:/usr/local/Cellar/opencv/3.3.1_1/lib

执行:source /etc/profile
重启电脑

4.测试demo
<1>.test.cpp
   #include 
#include 

using namespace cv;

int main(int argc, char** argv)
{   
  //加载图片路径
  Mat img = imread("moon.jpeg", CV_LOAD_IMAGE_UNCHANGED);

  if(img.empty())
  {
    fprintf(stderr, "请输入正确的图片路径\n");
    return -1;
  }

  Mat gray;
  cvtColor(img,gray,CV_BGR2GRAY);
  
  //jpeg格式转换为png格式图片
  if( ! imwrite("image_copy.png", img) )
  {
    fprintf(stderr, "转换写入图片失败.\n");
  }

  imshow("显示图片", img);
  waitKey(0);

  return 0;
}

<2>.Makefile 注意:第2行和第4行开头是 Tab键,不是空格键。
test:test.cpp
	g++ -o test test.cpp `pkg-config --libs opencv` `pkg-config --cflags opencv` 

clean:
	rm test

 

你可能感兴趣的:(OpenCV,学习笔记)