0
点赞
收藏
分享

微信扫一扫

opencv图像加载保存

sin信仰 2022-03-11 阅读 73
c++opencv

用opencv打开一张图片

在opencv-4.1.2下新建文件夹mytest

cd opencv-4.1.2
mkdir mytest

创建并进入test.cpp编程

touch test.cpp
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("q.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;
}

编译并运行


g++ cv.cpp -std=c++11 -o test `pkg-config --cflags --libs opencv4`
./test

常见问题: fatal error :opencv/opencv.hpp:没有那个文件和目录

请用代码

sudo apt-get install libcanberra-gtk-module

以及万能的重启大法

举报

相关推荐

0 条评论