注:
利用opencv建立的坐标轴,如何自定义创建,比如在图像的中心位置为坐标轴原点?
答:自己建立一个坐标转换关系,opencv的原点是(0,0),要把原点设置为(x0,x0),
Point PointMy(x,y)
{
return Point(x0+x,y0+y)
}
正文:
#include<iostream>
#include<core/core.hpp>
#include<highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image(Size(500,500),CV_8UC3);
image.at<Vec3b>(100,250)[0]=0;
image.at<Vec3b>(100,250)[1]=0;
image.at<Vec3b>(100,250)[2]=255;
putText(image,"at(100,250) is Here!",Point(250,100),0,0.7,Scalar(255,0,0));
image.at<Vec3b>(Point(100,250))[0]=0;
image.at<Vec3b>(Point(100,250))[1]=0;
image.at<Vec3b>(Point(100,250))[2]=255;
putText(image,"at(Point(100,250)) is Here!",Point(100,250),0,0.7,Scalar(255,0,0));
imshow("Test Function at",image);
waitKey();
return 0;
}
#include<iostream>
#include<core/core.hpp>
#include<highgui/highgui.hpp>
using namespace cv;
using namespace std;
//At方法
double CopyImageByAt(Mat originalImage, Mat &targetImage);
//行指针方法
double CopyImageByRowPtr(Mat originalImage, Mat &targetImage);
//指针方法
double CopyImageByPtr(Mat originalImage, Mat &targetImage);
//迭代方法
double CopyImageByIterator(Mat originalImage, Mat &targetImage);
//Opencv方法
double CopyFun(Mat originalImage, Mat &targetImage);
int main()
{
//读入图片,注意图片路径
Mat image=imread("D:\\Picture\\lena.jpg",1);
//图片读入成功与否判定
if(!image.data)
{
cout<<"you idiot!where did you hide lena!"<<endl;
//等待按键
system("pause");
return -1;
}
imshow("原始图像",image);
//输出图像
Mat targetImage(image.size(),image.type());
cout<<endl<<"At方法耗时:"<<CopyImageByAt(image,targetImage)<<endl;
imshow("At方法",targetImage);
cout<<endl<<"行指针方法耗时:"<<CopyImageByRowPtr(image,targetImage)<<endl;
imshow("行指针方法",targetImage);
cout<<endl<<"指针方法耗时:"<<CopyImageByPtr(image,targetImage)<<endl;
imshow("指针方法",targetImage);
cout<<endl<<"迭代方法耗时:"<<CopyImageByIterator(image,targetImage)<<endl;
imshow("迭代方法",targetImage);
cout<<endl<<"OpenCV Copy方法耗时:"<<CopyFun(image,targetImage)<<endl;
imshow("Copy方法",targetImage);
waitKey();
return 0;
}
//使用at方法实现逐个像素复制
double CopyImageByAt(Mat originalImage, Mat &targetImage)
{
double now=getTickCount();
//行
int rows=originalImage.rows;
//列
int cols=originalImage.cols;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
//若是灰度图像应使用如下表示:
//targetImage.at<uchar>(i,j)=originalImage.at<Vec3b>(i,j);
targetImage.at<Vec3b>(i,j)[0]=originalImage.at<Vec3b>(i,j)[0];
targetImage.at<Vec3b>(i,j)[1]=originalImage.at<Vec3b>(i,j)[1];
targetImage.at<Vec3b>(i,j)[2]=originalImage.at<Vec3b>(i,j)[2];
}
}
double end=getTickCount();
//返回方法耗时
return (end-now)/getTickFrequency();
}
//使用访问每行首指针方法实现像素复制
double CopyImageByRowPtr(Mat originalImage, Mat &targetImage)
{
double now=getTickCount();
//行
int rows=targetImage.rows;
//每行总元素数量,此处图像为3通道
int totalNum=targetImage.cols*targetImage.channels();
for(int i=0;i<rows;i++)
{
//data1指向目标图像第i行的首元素
uchar *data1=targetImage.ptr<uchar>(i);
data2指向原始图像第i行的首元素
uchar *data2=originalImage.ptr<uchar>(i);
for(int j=0;j<totalNum;j++)
{
//遍历每行所有元素
data1[j]=data2[j];
}
}
double end=getTickCount();
//返回方法耗时
return (end-now)/getTickFrequency();
}
//无扩充的图像,采用指针方法逐个像素复制
double CopyImageByPtr(Mat originalImage, Mat &targetImage)
{
double now=getTickCount();
//行
int rows=targetImage.rows;
//每行总元素数量,此处图像为3通道
int totalNum=targetImage.cols*targetImage.channels();
//判断图像数据是否连续
if(originalImage.isContinuous())
{
totalNum*=rows;
rows=1;
}
//外层循环只执行一次
for(int i=0;i<rows;i++)
{
uchar *data1=targetImage.ptr<uchar>(i);
uchar *data2=originalImage.ptr<uchar>(i);
for(int j=0;j<totalNum;j++)
{
data1[j]=data2[j];
}
}
double end=getTickCount();
//返回方法耗时
return (end-now)/getTickFrequency();
}
//使用迭代器遍历逐个像素复制
double CopyImageByIterator(Mat originalImage, Mat &targetImage)
{
double now=getTickCount();
//获取起始位置迭代器
Mat_<Vec3b>::iterator itBegin1=targetImage.begin<Vec3b>();
Mat_<Vec3b>::iterator itBegin2=originalImage.begin<Vec3b>();
//获取终止位置迭代器
Mat_<Vec3b>::iterator itEnd1=targetImage.end<Vec3b>();
Mat_<Vec3b>::iterator itEnd2=originalImage.end<Vec3b>();
for(;itBegin1!=itEnd1;++itBegin1)
{
(*itBegin1)[0]=(*itBegin2)[0];
(*itBegin1)[1]=(*itBegin2)[1];
(*itBegin1)[2]=(*itBegin2)[2];
++itBegin2;
}
double end=getTickCount();
//返回方法耗时
return (end-now)/getTickFrequency();
}
//OpenCV Copy方法实现图像复制
double CopyFun(Mat originalImage, Mat &targetImage)
{
double now=getTickCount();
originalImage.copyTo(targetImage);
double end=getTickCount();
//返回方法耗时
return (end-now)/getTickFrequency();
}