概述
//1.包含必要的头文件;
#include<opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
#if 1
void tong(Mat &img1, Mat &img2, Mat &img3)
{
int r = img1.rows;
int c = img1.cols + 5 + img2.cols;
img3.create(r, c, 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 img;
img = imread("automobile.jpg",0);
//2.输出图像automobile.jpg的宽度和高度;
cout << "宽度" << img.cols << endl;
cout << "高度" << img.rows << endl;
//3.按照幂律变换公式s = crv,取c = 1.3,v = 2.5对图像automobile.jpg进行灰度变换; 8:39
Mat m(img.size(), img.type());
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
m.at<uchar>(i, j) = saturate_cast<uchar>(1.3*(pow(img.at<uchar>(i, j), 2.5)));
}
}
imshow("幂律变换", m);
// 4.利用equalizeHist()图像automobile.jpg进行直方图均衡化,并存储输出结果图像; 8:46 修改9:26
int bins = 256;
Mat hist;
int hist_size[] = { bins };
float range[] = { 0,256 };
const float *ranges[] = { range };
calcHist(&img, 1, 0, Mat(), hist, 1, hist_size, ranges);
double max_l;
minMaxLoc(hist, 0, &max_l);
int sc = 2;
int gg = 256;
Mat z = Mat::zeros(gg, sc*bins, img.type());
for (int i = 0; i < bins; i++)
{
float bin_v = hist.at<float>(i);
int zg = cvRound(bin_v*gg / max_l);
rectangle(z, Point(i*sc, gg - 1), Point((i + 1)*sc-1 , gg - zg), Scalar(255,255,255));
}
imshow("直方图", z);
Mat j;
equalizeHist(z, j);
imshow("直方图均衡化", j);
//5.对automobile.jpg进行灰度处理,存储为automobile_gray.jpg; 8:48
Mat img2;
Mat img1 = imread("automobile.jpg");
cvtColor(img1, img2, COLOR_BGR2GRAY);
imshow("灰度图", img2);
imwrite("automobile_gray.jpg", img2);
//6.计算灰度图automobile_gray.jpg像素值为90的像素点数,并输出结果; 8:49
int sum = 0;
for (int i = 0; i < img2.rows; i++)
{
for (int j = 0; j < img2.cols; j++)
{
if (img2.at<uchar>(i, j) == 90)
{
sum++;
}
}
}
cout << "像素值为90的像素点数" << sum << endl;
//7.在图像automobile.jpg四周添加10个像素宽的蓝色边框并显示; 8:51
Mat bk;
copyMakeBorder(img1, bk, 10, 10, 10, 10, 0, Scalar(255, 0, 0));
imshow("边框", bk);
//8.检测图像automobile.jpg的边缘并显示; 8:57 修改 9:21
Mat g1;
GaussianBlur(img, g1, Size(3, 3), 3);
Canny(g1, g1, 10, 10);
imshow("边缘检测", g1);
//9.在单窗口中同时展示图像automobile.jpg和automobile_gray.jpg; 9:00
Mat d1 = imread("automobile.jpg");
Mat d2 = imread("automobile_gray.jpg");
Mat d3;
tong(d1, d2, d3);
//10.采用适当函数对automobile.jpg进行图像分割,并显示分割后效果; 9:02
Mat fg;
threshold(img, fg, 0, 255, THRESH_OTSU);
imshow("分割", fg);
//11.构造宽度为400,高度为300的全0矩阵B;9:03
Mat B = Mat::zeros(300, 400, d1.type());
//12.在automobile.jpg中截取(50, 200)、(450, 200)、(450, 500)、(50, 500)所围区域,存储为“automobile_part.jpg”的新图像; 9:11
Rect rect(50,200 , 400, 300);
cout << rect.tl() << endl;
Mat x = d1(rect);
imshow("截取", x);
imwrite("automobile_part.jpg", x);
//13.用automobile_part.jpg像素值替换矩阵B的对应像素值;9:13
B = x.clone();
imshow("替换后",B);
//14.计算automobile_part.jpg中心点旋转(旋转角度15°)的旋转矩阵并打印; 9:14
Point center(B.cols / 2, B.rows / 2);
Mat xz = getRotationMatrix2D(center, 15, 1);
cout << xz << endl;
//15.显示automobile_part.jpg中心点旋转(旋转角度15°)的旋转效果;9:16
Mat xz1;
warpAffine(B, xz1, xz, xz1.size());
imshow("旋转效果", xz1);
//16.对图像automobile.jpg在x轴方向缩小1 / 2,在y轴方向上都缩小1 / 3,显示缩小后效果;9:17
Mat xs;
resize(d1, xs, Size(d1.cols / 2, d1.rows / 3));
imshow("缩小", xs);
//17.用适当方法对图像automobile.jpg进行图像分割,显示图像分割效果; 9:18
Mat fg1;
threshold(img, fg1, 0, 255, THRESH_OTSU);
imshow("分割2", fg1);
18.用高斯滤波器对automobile.jpg进行平滑处理; 9:19
Mat G;
GaussianBlur(img, G, Size(7, 7), 7);
imshow("高斯滤波", G);
//19.提取automobile.jpg的轮廓并显示; 9:21
vector<vector<Point>> m1;
Mat g;
GaussianBlur(img, g, Size(3, 3), 3);
Canny(g, g, 10, 10);
Mat lun = Mat::zeros(img.size(), 0);
findContours(g, m1, RETR_LIST, CHAIN_APPROX_SIMPLE, Point());
drawContours(lun, m1, -1, Scalar(255));
imshow("轮廓", lun);
waitKey(0);
}
#endif // 0
最后
以上就是聪明万宝路为你收集整理的opencv 学习笔记(十五) 第三周总结的全部内容,希望文章能够帮你解决opencv 学习笔记(十五) 第三周总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复