您的位置:首页 > 运维架构

【VC图像处理】 图像水平镜像,垂直镜像,图像转置

2016-05-19 20:22 453 查看
#include "opencv2/core/core.hpp"
#include"opencv2/highgui/highgui.hpp"
#include<opencv2/opencv.hpp>
#include "iostream"
using namespace std;
using namespace cv;

void OnMirror_X(Mat img,Mat &OutImg)
{
int Width=img.cols;
int Height=img.rows;
OutImg.create(Height,Width,img.type());

for(int i=0;i<Height;i++)
for(int j=0;j<Width/2;j++)
{
if(img.channels()==1)
{

OutImg.at<uchar>(i,j)=img.at<uchar>(i,Width-j-1);
OutImg.at<uchar>(i,(Width-j-1))=img.at<uchar>(i,j);
}
else if(img.channels()==3)
{
OutImg.at<Vec3b>(i,j)=img.at<Vec3b>(i,Width-j-1);
OutImg.at<Vec3b>(i,Width-j-1)=img.at<Vec3b>(i,j);
}
}
}

void OnMirror_Y(Mat img,Mat &OutImg)
{

int Width=img.cols;
int Height=img.rows;
OutImg.create(Height,Width,img.type());
for(int i=0;i<Height/2;i++)
for(int j=0;j<Width;j++)
{
if(img.channels()==1)
{
OutImg.at<uchar>(i,j)=img.at<uchar>(Height-i-1,j);  //一开始没加-1  内存老报错!!!~~~  细心啊
OutImg.at<uchar>(Height-i-1,j)=img.at<uchar>(i,j);

}
else if(img.channels()==3)
{
OutImg.at<Vec3b>(i,j)=img.at<Vec3b>(Height-i-1,j);
OutImg.at<Vec3b>(Height-i-1,j)=img.at<Vec3b>(i,j);
}
}}
<pre name="code" class="cpp">void Transpose(Mat img,Mat &OutImg) // 图像转置
{
int Width=img.cols;
int Height=img.rows;

OutImg.create(Width,Height,img.type());//注意这里的长、宽

for(int i=0;i<Width;i++)  //注意这里的顺序
for(int j=0;j<Height;j++)
{
if(img.channels()==1)
{
OutImg.at<uchar>(i,j)=img.at<uchar>(j,i);
}
else if(img.channels()==3)
{
OutImg.at<Vec3b>(i,j)=img.at<Vec3b>(j,i);
}
}
}



void main()
{
Mat SrcImg=imread("C:\\Users\\Administrator\\Desktop\\工作\\testp\\01.jpg");
if(!SrcImg.data)
cout<<"读取图片错误\n";

Mat ResultImg1,ResultImg2;
OnMirror_X(SrcImg,ResultImg1);
OnMirror_Y(SrcImg,ResultImg2);

imshow("X",ResultImg1);
imshow("Y",ResultImg2);
imshow("src",SrcImg);
waitKey(0);

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opencv 图像处理