#include
#include
using namespace std;
using namespace cv;
int main()
{
Mat img1;
img1 = imread("build.jpg");
imshow("原图", img1);
Mat img2;
img2.create(img1.size(), 0);
for (int i = 0; i < img1.rows; i++)
{
for (int j = 0; j < img1.cols; j++)
{
img2.at<uchar>(i, j) = (img1.at<Vec3b>(i, j)[0] + img1.at<Vec3b>(i, j)[1] + img1.at<Vec3b>(i, j)[2]) / 3;
}
}
imshow("灰度图", img2);
imwrite("build_gray.jpg", img2);
Mat img3;
img3 = imread("build_gray.jpg",0);
Mat img4;
img4.create(img3.size(), 0);
for (int i = 0; i < img3.rows; i++)
{
for (int j = 0; j < img3.cols; j++)
{
img4.at<uchar>(i, j) = 1.1*img3.at<uchar>(i, j) + 60;
}
}
imshow("线性灰度变换", img4);
Mat img5;
img5.create(img3.size(), 0);
for (int i = 0; i < img3.rows; i++)
{
for (int j = 0; j < img3.cols; j++)
{
img5.at<uchar>(i, j) = 1.2*log(1 + img3.at<uchar>(i, j));
}
}
normalize(img5, img5, 0, 255, NORM_MINMAX);
imshow("非线性灰度变换", img5);
int sum = 0;
for (int i = 0; i < img3.rows; i++)
{
for (int j = 0; j < img3.cols; j++)
{
if (img3.at<uchar>(i, j) == 100);
{
sum++;
}
}
}
cout << "灰度值等于100的像素值个数" << sum << endl;
int bins = 256;
Mat hist;
int hist_size[] = { bins };
float range[] = { 0,256 };
const float* ranges[] = { range };
calcHist(&img3, 1, 0, Mat(), hist,1, hist_size, ranges);
double max_l;
minMaxLoc(hist, 0, &max_l);
int s = 2;
int gt = 256;
Mat z = Mat::zeros(gt, s*bins, CV_8UC3);
for (int i = 0; i < bins; i++)
{
float bin_val = hist.at<float>(i);
int zg = cvRound(bin_val*gt / max_l);
rectangle(z, Point(i*s, gt - 1), Point((i + 1)*s - 1, gt - zg), Scalar(255, 255, 255));
}
imshow("直方图", z);
Mat j(img3.size(), 0);
equalizeHist(img3, j);
imshow("直方图均衡化", j);
Mat q;
blur(img1, q, Size(3, 3));
imshow("blur", q);
Mat g;
GaussianBlur(img1, g, Size(3, 3), 3, 3);
imshow("高斯滤波", g);
Mat m;
medianBlur(img1, m, 3);
imshow("中值滤波", m);
Mat pp;
pp = imread("build.jpg");
int x0 = 100;
int y0 = 200;
int r = pp.rows + y0;
int c = pp.cols + x0;
Mat p(r,c, pp.type());
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
int x = j - x0;
int y = i - y0;
if (x >= 0 && y >= 0 && x < c&&y < r)
{
p.at<Vec3b> (i,j)= pp.ptr<Vec3b>(y)[x];
}
}
}
imshow("平移", p);
Point center(img1.cols / 2, img1.rows / 2);
Mat o = getRotationMatrix2D(center, 30, 1);
cout << "旋转矩阵" <<o<< endl;
Mat x;
warpAffine(img1, x, o, x.size());
imshow("旋转矩阵", x);
Mat v(img1.size(), CV_8UC3, Scalar(100, 0, 150));
imshow("创建", v);
Point2f src[3];
Point2f dst[3];
src[0] = Point2f(0, 0);
src[1] = Point2f(120, 200);
src[2] = Point2f(130, 400);
dst[0] = Point2f(0, 0);
dst[1] = Point2f(220, 300);
dst[2] = Point2f(230, 500);
Mat r1 = getAffineTransform(src, dst);
warpAffine(img1, v, r1, v.size());
imshow("仿射变换", v);
imwrite("build.jpg仿射变换.jpg", v);
Mat jj;
jj = imread("build.jpg仿射变换.jpg");
Mat bk;
copyMakeBorder(jj, bk, 10, 10, 10, 10, 0, Scalar(255, 0, 0));
imshow("边框", bk);
Mat sx;
resize(img1, sx, Size(img1.cols / 2, img1.rows / 2));
imshow("缩小", sx);
waitKey(0);
}