1.镜像
//对图像进行水平镜像
void shuipingjingxiang(Mat src)
{
Mat res(src.size(), src.depth(), Scalar(0));
//实现水平镜像功能
Mat imgBGR[3];
split(src, imgBGR);
Size se = src.size();
int width = se.width;
int height = se.height;
for (int k = 0; k < 3; k++)
{
for (int i = 0; i<height; i++)
{
for (int j = 0; j<width; j++)
{
imgBGR[k].at<uchar>(i, width - j - 1) = src.at<Vec3b>(i, j)[k];
}
}
}
merge(imgBGR,3, res);
//imshow("shuiping", res);
}
//对图像进行竖直镜像
void shuzhijingxiang(Mat src)
{
Mat res(src.size(), src.depth(), Scalar(0));
Mat imgBGR[3];
split(src, imgBGR);
//实现竖直镜像功能
for (int k = 0; k < 3; k++)
{
for (int i = 0; i<src.rows; i++)
{
for (int j = 0; j<src.cols; j++)
{
imgBGR[k].at<uchar>(src.rows - i - 1, j) = src.at<Vec3b>(i, j)[k];
}
}
}
merge(imgBGR, 3, res);
//imshow("shuzhi", res);
}
//对图像进行对角镜像
void duijiaojingxiang(Mat src)
{
Mat res(src.size(), src.depth(), Scalar(0));
Mat imgBGR[3];
split(src, imgBGR);
//实现对角镜像功能
for (int k = 0; k < 3; k++)
{
for (int i = 0; i<src.rows; i++)
{
for (int j = 0; j<src.cols; j++)
{
imgBGR[k].at<uchar>(src.rows - i - 1, src.cols - j - 1) = src.at<Vec3b>(i, j)[k];
}
}
}
merge(imgBGR, 3, res);
//imshow("duijiao", res);
}
注意:多通道需要进行merge运算。
2.旋转
//按照原图尺寸的旋转(对于矩形图像最好进行180度的旋转)
void myRoation(Mat src, double db_degree)
{
Mat res(src.size(), src.depth(), Scalar(0));
Point2f center;
center.x = float(src.cols / 2.0 - 0.5);
center.y = float(src.rows / 2.0 - 0.5);
double degree = db_degree;//角度为+表示逆时针
Mat M = getRotationMatrix2D(center, degree, 1);
warpAffine(src, res, M, src.size());
//增加一个背景元素的填充
}
3.下采样与上采样
//尺度缩放并还原进行模糊运算(ratio默认取0)
void myResize(Mat src,double ratio)
{
Size tmpSize = src.size();
int width = tmpSize.width;
int height = tmpSize.height;
Mat resTmp(Size(width / 2, height / 2), src.depth(), Scalar(0));
Mat res(src.size(), src.depth(), Scalar(0));
resize(src, resTmp, resTmp.size(), ratio, ratio);
resize(resTmp, res, res.size(), ratio, ratio);
//imshow("111", res);
}
4.平移
//平移功能
void myTransfer(Mat src,int t_x, int t_y)
{
Mat tmpSrc(src.size(), src.depth(), Scalar(0));
Mat res(src.size(), src.depth(), Scalar(0));
Point2f srcPoints[3];
Point2f resPoints[3];
srcPoints[0] = Point2i(0, 0);
srcPoints[1] = Point2i(0, src.rows);
srcPoints[2] = Point2i(src.cols, 0);
resPoints[0] = Point2i(t_x, t_y);
resPoints[1] = Point2i(t_x, src.rows + t_y);
resPoints[2] = Point2i(src.cols + t_x, t_y);
Mat M = getAffineTransform(srcPoints, resPoints);
warpAffine(src, tmpSrc, M, tmpSrc.size());
Rect rec = Rect(t_x, t_y, src.cols - t_x, src.rows - t_y);
Mat tmpSrc2 = tmpSrc(rec).clone();
copyMakeBorder(tmpSrc2, res, t_y, 0, t_x, 0, BORDER_REPLICATE);
}
5.加噪声
void myGaussblur(Mat src,int kernal_size)
{
Mat res(src.size(), src.depth(), Scalar(0));
GaussianBlur(src, res, Size(kernal_size, kernal_size), 0.3);
}
6.对比度增强
//图像对比度增强
void myEnhancement(Mat src)
{
//直方图均衡化
Mat res(src.size(), src.depth(), Scalar(0));
Mat imgRGB[3];
split(src, imgRGB);
for (int i = 0; i < 3; i++)
{
equalizeHist(imgRGB[i], imgRGB[i]);
}
merge(imgRGB, 3, res);
}
7.固定图像尺寸
//判断图像尺寸进行剪裁或边缘填充
Mat resizeImage(Mat src)
{
Mat tmpSrc = src.clone();
Mat res(Size(800, 400), tmpSrc.depth(), Scalar(0));
Size tmpSize = tmpSrc.size();
int width = tmpSize.width;
int height = tmpSize.height;
if (width < 800 && height <= 400)
{
int top, buttom, left, right;
top = (400 - height) / 2;
buttom = 400 - top - height;
left = (800 - width) / 2;
right = 800 - left - width;
copyMakeBorder(tmpSrc, res, top, buttom, left, right, BORDER_REFLECT);//最近领域插值运算
}
else if (width >= 800 && height>400)
{
int top_left_x = (width - 800) / 2;
int top_left_y = (height - 400) / 2;
Rect rec = Rect(top_left_x, top_left_y, 800, 400);
res = tmpSrc(rec);
}
else if (width < 800 && height >= 400)
{
Mat tmpSrc2(Size(800, height), tmpSrc.depth(), Scalar(0));
int left, right;
left = (800 - width) / 2;
right = 800 - width - left;
copyMakeBorder(tmpSrc, tmpSrc2, 0, 0, left, right, BORDER_REFLECT);
int top_left_y = (height - 400) / 2;
Rect rec = Rect(0, top_left_y, 800, 400);
res = tmpSrc2(rec);
}
else if (width > 800 && height <= 400)
{
Mat tmpSrc2(Size(width, 400), tmpSrc.depth(), Scalar(0));
int top, buttom;
top = (400 - height) / 2;
buttom = 400 - width - top;
copyMakeBorder(tmpSrc, tmpSrc2, top, buttom, 0, 0, BORDER_REFLECT);
int top_left_x = (width - 800) / 2;
Rect rec = Rect(top_left_x, 0, 800, 400);
res = tmpSrc2(rec);
}
else
{
res = src.clone();
}
return res;
}