1.读取图片 imread
2.显示图片 imshow
3.创建窗口 namedWindow
4.颜色转换
5.同一窗口 两张图片
6.在图片上画线
7.图片轮廓
8.腐蚀与膨胀
//一、题目要求:
//用VS新建OpenCV工程项目,所有操作都在项目中进行。(除特殊注明外,每项5分)。
#include
#include
using namespace std;
using namespace cv;
//1.图像输入输出操作:
//1.1.新建子目录images,把附件lena.jpg放入子目录images中;
Mat img11;
Point p1;
void hui(int event, int x, int y, int flags, void*)
{
if (event == 1)
{
p1 = Point(x, y);
}
else if (event == 0 && flags == 1)
{
Point p(x, y);
line(img11, p1, p, Scalar(255,0,0), 5);
p1 = p;
imshow("写字", img11);
}
}
void tong(Mat &img1, Mat &img2, Mat img3)
{
int cols = img1.cols + 5 + img2.cols;
int rows = img1.rows;
img3.create(rows, cols, img1.type());
img1.copyTo(img3(Rect(0, 0, img1.cols, img1.rows)));
img2.copyTo(img3(Rect(img1.cols, 0, img2.cols, img2.rows)));
imshow("同时显示", img3);
}
int main()
{
Mat img1, img2;
//1.2.以彩色方式打开子目录images中的lena.jpg并显示;
img1 = imread("lena.jpg", 1);
imshow("彩色", img1);
waitKey(0);
//1.3.以灰度方式打开子目录images中的lena.jpg并显示
img2 = imread("lena.jpg", 0);
imshow("灰度", img2);
waitKey(0);
//2.窗口与图像创建:
//2.1.新建自适应图像大小窗口“显示lena.jpg”,并显示10秒后退出;
namedWindow("显示lena.jpg");
imshow("显示lena.jpg", img1);
waitKey(10000);
//2.2.存储1.3步的灰度图像,命名为“lena_gray.jpg”存入子目录images中;
imwrite("lena_gray.jpg", img2);
//2.3.在单窗口中同时显示lena.jpg和lena_gray.jpg;
Mat img3, da;
img3 = imread("lena_gray.jpg", 1);
tong(img1, img3, da);
waitKey(0);
//2.4.销毁名为“显示lena.jpg”的窗口;
destroyWindow("显示lena.jpg");
//2.5.用Mat(Size size, int type)创建行为200像素,列256像素,type为CV_8UC1的图像;
Mat q = Mat::Mat(Size(256, 200), CV_8UC1);
//2.6.利用遍历方法把2.5矩阵的第1列赋值为0,第2列赋值为1,......,第256列赋值为255,并显示;
for (int i = 0; i < q.rows; i++)
{
for (int j = 0; j < q.cols; j++)
{
q.col(j) = j;
}
}
cout << q << endl;
//2.7.生成一个大小为200 * 200像素的全0矩阵并显示;
Mat w = Mat::ones(200, 200, CV_8UC1);
cout << w;
cout << endl;
//2.8.定义MAT矩阵B,用clone()复制lena_gray.jpg到B。
Mat B;
B = img3.clone();
//3.关于图像的操作:
//3.1.输出图像lena.jpg的行数;
cout << "行数" << img1.rows << endl;
//3.2.输出图像lena.jpg的列数;
cout << "列数" << img1.cols << endl;
//3.3.输出图像lena.jpg的通道数;
cout << "通道数" << img1.channels() << endl;
//3.4.利用函数cvtColor()把lena.jpg转换为灰度图像;
cvtColor(img1, img1, COLOR_RGB2GRAY);
//3.5.在图像lena.jpg上画一个矩形;
rectangle(img1, Rect(20, 20, 100, 100), Scalar(0, 0, 255), 5);
imshow("矩形", img1);
waitKey(0);
//3.6.自定义回调函数,用鼠标在lena.jpg添加自勉口号“为升班加油”;
img11 = imread("lena.jpg");
imshow("写字", img11);
setMouseCallback("写字", hui);
waitKey(0);
//3.7.利用函数findContours()查找图像lena.jpg的轮廓;
Mat img9, img0;
img9 = imread("lena.jpg",0);
GaussianBlur(img9, img0, Size(3, 3), 0);
Canny(img0, img0, 100, 200);
vector<vector<Point>> contours;
vector<Vec4i> h;
findContours(img0, contours, h, RETR_LIST, CHAIN_APPROX_SIMPLE);
Mat lun = Mat(img9.size(), img9.type());
for (int i = 0; i < contours.size(); i++)
{
for (int j = 0; j < contours[i].size(); j++)
{
Point p =Point(contours[i][j].x, contours[i][j].y);
lun.at<uchar>(p) = 255;
}
}
drawContours(lun, contours, -1, Scalar(0, 0, 255));
imshow("轮廓", lun);
waitKey(0);
//3.8.创建矩形对象rect(100, 50, 50, 100),计算它的面积并打印。//9.27
Rect ss(100, 50, 50, 100);
cout << "面积" << ss.area() << endl;
}