目标:
- 在windows、ubuntu、树莓派系统中安装opencv2.4.x 或者opencv3.4.x,
- 在windows、ubuntu、树莓派3个系统下,依次编写一个图片特性处理代码程序。(要求同时显示原始图片和特效图片,并保存特效图片到文件中。)
源码
#include
#include
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;
}
2.1 官网下载Sources版本(下载很慢,重复下载很多次才成功,需要耐心)
2.2 将下载文件复制到home目录下,进行解压配置:
进入命令行模式:
unzip opencv-3.4.1.zip
cd opencv-3.4.1
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
mkdir my_build_dir
cd my_build_dir
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make
sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf
执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加
/usr/local/lib
sudo ldconfig
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
至此所有的配置都已经完成
2.3 Linux下编程:
cd opencv-3.4.1
mkdir mytest
touch test.cpp
sudo gedit /test.cpp
#sudo vim /test.cpp
#根据自己配置编辑器进行编辑
编辑下面代码:
注意补全头文件;
其中图片路径直接放在home目录下;
源码:
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
CvPoint center;
double scale = -3;
IplImage* image = cvLoadImage("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;
}
gcc test.cpp -o test `pkg-config --cflags --libs opencv`
gcc编译器:gcc +文件名+ -o+输出文件流名称 +` 支持包
./test
sudo apt-get update
sudo apt-get upgrade
sudo rpi-update
sudo apt-get install build-essential cmake git pkg-config
sudo apt-get install libjpeg8-dev
sudo apt-get install libtiff5-dev
sudo apt-get install libjasper-dev
sudo apt-get install libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install libatlas-base-dev gfortran
wget -O opencv-3.4.1.zip https://github.com/Itseez/opencv/archive/3.4.1.zip
unzip opencv-3.4.1.zip
wget -O opencv_contrib-3.4.1.zip https://github.com/Itseez/opencv_contrib/archive/3.4.1.zip
unzip opencv_contrib-3.4.1.zip
cd opencv-3.4.1
mkdir release
cd release
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 make
(4个小时/单线程;网上有多线程的方法安装,自行了解!不过树莓派3b+运行能力不如我们笔记本,多线程也容易崩,不推荐多线程吧~)
sudo make install
sudo ldconfig
这里opencv就编译安装完成了!
3.3编写程序:(与linux一致要注意头文件opencv2/opencv)
3.3.1基于 C++ 编程(编写步骤与上述2.3类似)
创建opencv_test.cpp文件
编译:(lena.jpg与opencv_test.cpp处于同一目录下)
g++ opencv_test1.cpp -o opencv_test1 -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -loencv_imgcodecs
./opencv_test
一定加上“.py”系统会默认为python文件
源码
import cv2 as cv
import math
src=cv.imread('lena.jpg')
cv.imshow('lena', src)
scale=-3
cv.namedWindow('lena_huidu', cv.WINDOW_AUTOSIZE)
s=src.shape
for i in range(s[1]):
for j in range(s[0]):
dx=(j-s[0]/2)/(s[0]/2)
dy=(i-s[1]/2)/(s[1]/2)
weight=math.exp((dx*dx+dy*dy)*scale)
ptr = src[j,i]
ptr[0] =ptr[0]*weight
ptr[1] = ptr[1]*weight
ptr[2] = ptr[2]*weight
cv.imshow('lena_huidu', src)
cv.waitKey(0)
cv.destroyAllWindows()
大功告成!