一、Windows10安装opencv3.14
1.在官网下载windows版本的opencv
2.配置环境变量(配置后要重启电脑)
3.在vs2017下写一个opencv调用灰度图片的图片
1)添加C++空项目Project1
2)配置相应的环境
-
debug调为x64
-
添加包含目录、库目录、附加依赖项(打开工程属性alt+enter)
包含目录:找到安装目录下的include,includeopencv ,includeopencv2,将三个路径建议都添加进目录
库目录:将F:opencvopencvuildx64vc15lib也就是bin目录添加进去
附加依赖项:链接器->输入->附加依赖项 手动敲入 opencv_world341d.lib
4.编写测试程序
源码
#include <highgui.hpp>
#include <opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
CvPoint center;
double scale = -3;
IplImage* image = cvLoadImage("d://myworkspace//VisualStudioProjects//lena.jpg");//引入图片位置
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;
}
结果展示
二、ubuntu16.04安装opencv3.14
1.下载sources版本并将其解压在某一目录下
2.进入到目录opencv-3.142下打开terminal随后安装依赖库和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之后执行命令 ,创建编译文件夹
mkdir my_bulid_dir
进入文件夹进行配置
cd my_bulid_dir
执行命令,漫长的编译过程
sudo make
执行命令
sudo make install
sudo make install 执行完毕后OpenCV编译过程就结束了,接下来就需要配置一些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
Linux下opencv编程
创建一个文件夹并创建一个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("me.jpg");
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("me.png", src);
cvNamedWindow("me",1); imshow("me1.png", src);
cvWaitKey();
return 0;
}
保存并编译
g++ test.cpp -o test `pkg-config --cflags --libs opencv`
运行
./test
结果展示
三、树莓派同linux
四、摄像头显示处理视频
源码
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
VideoCapture capture(0);
while(1)
{
Mat frame;
capture >> frame;
imshow("读取视频帧",frame);
waitKey(30);
}
system("pause");
return 0;
}
结果