实验环境
OpenCV3.2.0
源码
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("image0.jpg", IMREAD_COLOR);
cvtColor(src, src, COLOR_BGR2GRAY);
//if (src.depth() == CV_8UC1)//这是成立的
//{
// cout << src.depth() << endl;
//}
//IplImage *src = cvLoadImage("MatImage.bmp", 1);
if (src.empty() /*|| src == NULL*/)
{
cout << "读入的图像为空error" << endl;
return -1;
}
imshow("原图灰度化", src);
Mat res(src.size(), src.depth(), Scalar(0));
//IplImage *res2 = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
//imshow("111", res);
//cvShowImage("222", res2);
//实现水平镜像功能
for (int i = 0; i<src.rows; i++)
{
for (int j = 0; j<src.cols; j++)
{
res.at<uchar>(i, src.cols - j - 1) = src.at<uchar>(i, j);
}
}
imshow("水平镜像", res);
//if (res == NULL) {
// return NULL;
//}
//int N = src->width;
//for (int i = 0; i<src->height; i++) {
// for (int j = 0; j<src->width; j++) {
// CvScalar color = cvGet2D(src, i, j);
// cvSet2D(res, i, N - j - 1, color);//注意这里
// }
//}
Mat res2(src.size(), src.depth(), Scalar(0));
//实现竖直镜像功能
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
res2.at<uchar>(src.rows - i - 1, j) = src.at<uchar>(i, j);
}
}
imshow("竖直镜像", res2);
Mat res3(src.size(), src.depth(), Scalar(0));
//对角镜像
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
res3.at<uchar>(src.rows - i - 1, src.cols - j - 1) = src.at<uchar>(i, j);
}
}
imshow("对角镜像", res3);
waitKey(0);
return 0;
}
结果显示