OpenCV学习笔记(一)
- 显示图片
- 创建Mat类
- mat类常用函数
- 灰度,滤波
- 图像翻转
- 平移
- 缩放
显示图片
#include "pch.h"
#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
Mat img = imread("D:\\test.jpg");
namedWindow("测试opencv");
imshow("测试opencv", img);
cvWaitKey(6000000);
}
创建Mat类
#include "pch.h"
#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image1;
Mat image2(6, 6, CV_8UC1);
Mat image3(Size(7,7),CV_8UC3);
Mat image4(8, 8, CV_32FC2, Scalar(1, 3));
Mat image5(Size(9,9),CV_8UC3,Scalar(1,2,3));
Mat image6(image2);
cout << image1 << endl;
cout << image2 << endl;
cout << image3 << endl;
cout << image4 << endl;
cout << image5 << endl;
cout << image6 << endl;
return 0;
}
mat类常用函数
#include "pch.h"
#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image1(10,8,CV_8UC1,Scalar(5));
cout << "image1 row" << image1.rows<< endl;
cout << "image1 col" << image1.cols << endl;
cout << image1.rowRange(1, 3) << endl;
cout << image1.colRange(2, 4) << endl;
Mat image2(8, 8, CV_32FC2, Scalar(1, 5));
image2.create(10, 10, CV_8UC(3));
cout << image2.channels() << endl;
image2.convertTo(image2, CV_32F);
cout << image2.depth() << endl;
Mat image3 = Mat::zeros(image2.rows, image2.cols, CV_8UC1);
image1.row(4) = image1.row(5) * 2;
cout << image1 << endl;
Mat image4 = image1.col(4);
cout << image4 << endl;
image1.col(1).copyTo(image4);
cout << image4 << endl;
return 0;
}
灰度,滤波
#include "pch.h"
#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("d:\\test1.jpg");
if (image.empty())
return -1;
Mat gray;
cvtColor(image, gray, CV_RGB2GRAY);
imshow("gray",gray);
Mat blurdstimage;
blur(gray, blurdstimage, Size(5, 5),Point(-1,-1));
imshow("blurdstimage", blurdstimage);
imwrite("d:\\blurdstimage.png", blurdstimage);
waitKey(0);
return 0;
}
图像翻转
#include "pch.h"
#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("d:\\test1.jpg");
Mat resultimage (image.size(),image.type());
Mat xmapimage(image.size(), CV_32FC1);
Mat ymapimage(image.size(), CV_32FC1);
int rows = image.rows;
int cols = image.cols;
for (int j = 0; j < rows; j++)
for (int i = 0; i < cols; i++)
{
xmapimage.at<float>(j, i) = cols - i;
ymapimage.at<float>(j, i) = rows - j;
}
remap(image, resultimage, xmapimage, ymapimage, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
imshow("image", image);
imshow("sultimage", resultimage);
waitKey(0);
return 0;
}
平移
#include "pch.h"
#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
Mat imagetranslations1(Mat & image, int xoffset, int yoffset)
{
int nrows = image.rows;
int ncols = image.cols;
Mat resultimage(image.size(), image.type());
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
int x = j - xoffset;
int y = i - yoffset;
if (x >= 0 && y >= 0 && x < ncols && y < nrows)
resultimage.at<Vec3b>(i, j) = image.ptr<Vec3b>(y)[x];
}
}
return resultimage;
}
Mat imagetranslations2(Mat & image, int xoffset, int yoffset)
{
int nrows = image.rows + abs(yoffset);
int ncols = image.cols + abs(xoffset);
Mat resultimage(nrows, ncols, image.type());
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
int x = j - xoffset;
int y = i - yoffset;
if (x >= 0 && y >= 0 && x < ncols && y < nrows)
resultimage.at<Vec3b>(i, j) = image.ptr<Vec3b>(y)[x];
}
}
return resultimage;
}
int main()
{
Mat image = imread("d://test1.jpg");
imshow("image", image);
int xoffset = 50;
int yoffset = 50;
Mat reimage1 = imagetranslations1(image, xoffset, yoffset);
imshow("reimage1", reimage1);
Mat reimage2 = imagetranslations2(image, xoffset, yoffset);
imshow("reimage2", reimage2);
xoffset = -50;
yoffset = -50;
Mat reimage3 = imagetranslations1(image, xoffset, yoffset);
imshow("reimage3", reimage3);
waitKey(0);
return 0;
}
缩放
#include "pch.h"
#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
Mat imagereduction1(Mat &image, double kx, double ky)
{
int nrows = cvRound(image.rows*kx);
int ncols = cvRound(image.cols*ky);
Mat resultimage(nrows, ncols, image.type());
for (int i = 0; i < nrows; ++i)
{
for (int j = 0; j < ncols; ++j)
{
int x = static_cast<int>((i + 1) / kx + 0.5) - 1;
int y = static_cast<int>((j + 1) / ky + 0.5) - 1;
resultimage.at<Vec3b>(i, j) = image.at<Vec3b>(x, y);
}
}
return resultimage;
}
Vec3b areaaverage(const Mat &image, Point_<int> leftpoint, Point_<int> rightpoint)
{
int temp1 = 0, temp2 = 0, temp3 = 0;
int npix = (rightpoint.x - leftpoint.x + 1)*(rightpoint.y - leftpoint.y + 1);
for (int i = leftpoint.x; i <= rightpoint.x; i++)
{
for (int j = leftpoint.y; j <= rightpoint.y; j++)
{
temp1 += image.at<Vec3b>(i, j)[0];
temp2 += image.at<Vec3b>(i, j)[1];
temp3 += image.at<Vec3b>(i, j)[2];
}
}
Vec3b vectemp;
vectemp[0] = temp1 / npix;
vectemp[1] = temp2 / npix;
vectemp[2] = temp3 / npix;
return vectemp;
}
Mat imagereduction2(const cv::Mat &image, double kx, double ky)
{
int nrows = cvRound(image.rows*kx);
int ncols = cvRound(image.cols*ky);
Mat resultimage(nrows, ncols, image.type());
int leftrowcoordinate = 0;
int leftcolcoordinate = 0;
for (int i = 0; i < nrows; ++i)
{
int x = static_cast<int>((i + 1) / kx + 0.5) - 1;
for (int j = 0; j < ncols; ++j)
{
int y = static_cast<int>((j + 1)/ky + 0.5) - 1;
resultimage.at<Vec3b>(i, j) = areaaverage(image, Point_<int>(leftrowcoordinate, leftcolcoordinate), Point_<int>(x, y));
leftcolcoordinate = y + 1;
}
leftcolcoordinate = 0;
leftrowcoordinate = x + 1;
}
return resultimage;
}
int main()
{
Mat image = imread("d://test1.jpg");
imshow("image", image);
Mat resultimage1 = imagereduction1(image, 0.5, 0.5);
imshow("res1", resultimage1);
Mat resultimage2 = imagereduction2(image, 0.5, 0.5);
imshow("res2", resultimage2);
waitKey(0);
return 0;
}