一、Windows
- 安装opencv3.4.1
- 配置环境变量
- visual studio配置
(1)新建空项目
(2)添加新建项
(3)打开属性管理器:选择Debug x64右键“属性”
(4)添加包含目录
(5)添加库目录
(6)添加附加依赖项
- 测试效果
- 编写代码
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
CvPoint center;
double scale = -3;
IplImage* image = cvLoadImage("d://dasan//qianrushi//Project1//timg.png");
argc == 2 ? cvLoadImage(argv[1]) : 0;
cvShowImage("Image", image);
if (!image) return -1; center = cvPoint(image->width / 2, image->height / 2);
for (int i = 0; i < image->height; i++)
for (int j = 0; j < image->width; j++) {
double dx = (double)(j - center.x) / center.x;
double dy = (double)(i - center.y) / center.y;
double weight = exp((dx*dx + dy * dy)*scale);
uchar* ptr = &CV_IMAGE_ELEM(image, uchar, i, j * 3);
ptr[0] = cvRound(ptr[0] * weight);
ptr[1] = cvRound(ptr[1] * weight);
ptr[2] = cvRound(ptr[2] * weight);
}
Mat src; Mat dst;
src = cvarrToMat(image);
cv::imwrite("test.png", src);
cvNamedWindow("test", 1); imshow("test", src);
cvWaitKey();
return 0;
}
二、Ubuntu
-
官网下载opencv3.4.1 , 选择sources版本
-
解压下载下来的zip包
unzip opencv-3.4.1.zip
-
进入到解压后的文件包中
-
安装依赖库和cmake ,如果提醒需要apt-get update,那就先sudo su进入root权限,再sudo apt-get update,然后在执行下面命令
sudo apt-get install cmake
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff5.dev libswscale-dev libjasper-dev
-
安装完cmake之后执行命令 ,创建编译文件夹
In-source builds are not allowed.
mkdir my_build_dir
cd my_build_dir
-
cmake一下
cmake -D CMAKE_BUILD_TYPE=Release-D
CMAKE_INSTALL_PREFIX=/usr/local …
-
执行命令,漫长的编译过程
sudo make
-
执行命令
sudo make install
-
配置OpenCV的编译环境
首先将OpenCV的库添加到路径,从而可以让系统找到sudo gedit /etc/ld.so.conf.d/opencv.conf
在打开的空白的文件末尾添加
/usr/local/lib
-
执行如下命令使得刚才的配置路径生效
sudo ldconfig
-
配置bash
sudo gedit /etc/bash.bashrc
最末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
保存,执行如下命令使得配置生效source /etc/bash.bashrc
更新
sudo updatedb
-
所有的配置都已经完成,用一个自带的程序测试一下
cd ~/dir/opencv-3.4.1/samples/cpp/example_cmake
按顺序执行
cmake .
make
./opencv_example
成功打开了摄像头,左上角有一个hello opencv
配置成功
-
在Linux下编程
依次执行cd opencv-3.4.1
mkdir mytest
cd mytest
touch test.cpp
sudo gedit /test.cpp
代码:
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
CvPoint center;
double scale = -3;
IplImage* image = cvLoadImage("timg.jpeg");
argc == 2? cvLoadImage(argv[1]) : 0;
cvShowImage("Image", image);
if (!image) return -1; center = cvPoint(image->width / 2, image->height / 2);
for (int i = 0;i<image->height;i++)
for (int j = 0;j<image->width;j++) {
double dx = (double)(j - center.x) / center.x;
double dy = (double)(i - center.y) / center.y;
double weight = exp((dx*dx + dy*dy)*scale);
uchar* ptr = &CV_IMAGE_ELEM(image, uchar, i, j * 3);
ptr[0] = cvRound(ptr[0] * weight);
ptr[1] = cvRound(ptr[1] * weight);
ptr[2] = cvRound(ptr[2] * weight);
}
Mat src;Mat dst;
src = cvarrToMat(image);
cv::imwrite("test.png", src);
cvNamedWindow("test",1); imshow("test", src);
cvWaitKey();
return 0;
}
g++ test.cpp -o test ‘pkg-config --cflags –libsopencv’
./test
把test.cpp拷贝到home目录下
三、树莓派
- 安装opencv
安装opencv的各种工具和图像处理包
软件源更新
sudo apt-get update
所有软件更新
sudo apt-get upgrade
树莓派固件更新
sudo rpi-update
安装构建opencv的相关工具
sudo apt-get install build-essential cmake git pkg-config
安装 jpeg 格式图像工具包
sudo apt-get install libjpeg8-dev
安装 tif 格式图像工具包
sudo apt-get install libtiff5-dev
安装 jasper 图像工具包
sudo apt-get install libjasper-dev
安装 png 图像工具包
sudo apt-get install libpng12-dev
安装视频 I/O 包
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
安装gtk2.0
sudo apt-get install libgtk2.0-dev
优化函数包
sudo apt-get install libatlas-base-dev gfortran
下载opencv源码
wget -O opencv-3.4.1.zip https://github.com/Itseez/opencv/archive/3.4.1.zip
解压opencv源码
unzip opencv-3.4.1.zip
下载opencv_contrib源码
wget -O opencv_contrib-3.4.1.zip https://github.com/Itseez/opencv_contrib/archive/3.4.1.zip
解压opencv_contrib源码
unzip opencv_contrib-3.4.1.zip
找到下载的文件夹,可以右键选择终端打开,也可以直接在终端下输入命令
cd opencv-3.4.1
新建一个名为release文件夹
mkdir release
进入此文件夹
cd release
设置cmake编译参数
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=/usr/local
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.1/modules
-D INSTALL_PYTHON_EXAMPLES=ON
-D BUILD_EXAMPLES=ON …
正式编译
sudo mkde
编译安装
sudo make install
更新动态链接库
sudo ldconfig
opencv编译安装完成.
编写程序
代码:
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
CvPoint center;
double scale = -3;
IplImage* image = cvLoadImage("d://dasan//qianrushi//Project1//timg.png");
argc == 2 ? cvLoadImage(argv[1]) : 0;
cvShowImage("Image", image);
if (!image) return -1; center = cvPoint(image->width / 2, image->height / 2);
for (int i = 0; i < image->height; i++)
for (int j = 0; j < image->width; j++) {
double dx = (double)(j - center.x) / center.x;
double dy = (double)(i - center.y) / center.y;
double weight = exp((dx*dx + dy * dy)*scale);
uchar* ptr = &CV_IMAGE_ELEM(image, uchar, i, j * 3);
ptr[0] = cvRound(ptr[0] * weight);
ptr[1] = cvRound(ptr[1] * weight);
ptr[2] = cvRound(ptr[2] * weight);
}
Mat src; Mat dst;
src = cvarrToMat(image);
cv::imwrite("test.png", src);
cvNamedWindow("test", 1); imshow("test", src);
cvWaitKey();
return 0;
}
另:
用Ubuntu安装opencv时,占用空间较大,导致安装到百分之92时home目录已满安装失败,当初装双系统时分配给Ubuntu的空间不够用了,搜索解决方案: 回到Windows划分一个新加卷,挂载到home目录下。
在重启电脑时黑屏显示error:unknown filesystem ,搜索解决方案: 用ls显示分区信息,由于不知道Ubuntu所在分区,用set root=hd0,msdos1 ; set prefix=(hd0,msdos1) ; insmod normal 的语句一直从msdos0一直试到8,终于不显示unknow filesystem,输入nomal后成功进入开机选择系统的界面。