#include <QCoreApplication>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
vector<Mat> imagevec;
double Tenrgrad(Mat *src){
int row = src->rows;
int col = src->cols;
int number = (row-2)*(col-2);
double S = 0;
for(int i = 1 ;i < row-1 ; i++)
{
int Sx,Sy,Sxy;
uchar* predata= src->ptr<uchar>(i-1);
uchar* curdata= src->ptr<uchar>(i);
uchar* nexdata= src->ptr<uchar>(i+1);
for(int j = 1 ;j < col-1 ;j++)
{
//*****************************
// i-1,j-1 i-1,j i-1,j+1
// i,j-1 i,j i,j+1
// i+1,j-1 i+1,j i+1,j+1
//Gx =-1,0,1 Gy =1, 2, 1
// -2,0,2 0, 0, 0
// -1,0,1 -1,-2,-1
//*******************************
Sx=nexdata[j+1]+2*curdata[j+1]+predata[j+1]-nexdata[j-1]-2*curdata[j-1]-predata[j-1];
Sy=predata[j+1]+2*predata[j+1]+predata[j+1]-nexdata[j-1]-2*nexdata[j-1]-nexdata[j-1];
Sxy = sqrt(Sx)+sqrt(Sy);
S += Sxy*Sxy;
}
}
return S/number;
}
void puttext(Mat *Img,double vaule,string str)
{
stringstream ValueStream;
string ValueString;
cout<<vaule<<endl;
ValueStream << vaule; //将int类型的值放入输入流中
ValueStream >> ValueString; //从sstream中抽取前面插入的int类型的值,赋给string类型
ValueString = str + ValueString;
putText(*Img, ValueString, Point(20, 50), FONT_HERSHEY_COMPLEX, 0.8, Scalar(255, 20, 245), 2);
//putText(dst,str,Point(30,30),FONT_HERSHEY_COMPLEX);
}
Mat MultImgShow(vector<Mat> imagevec)
{
int col = imagevec[0].cols;
int row = imagevec[0].rows;
int imgNum = imagevec.size();
int width = col*2+1;
int height = row*2+1;
Mat finalImg(height,width,imagevec[0].type());
Point2i location(0, 0); //坐标点(从0,0开始)
//构建窗口大小 通道与imageVector[0]的通道一样
Size imgStdSize(row,col);
for (int i=0; i<imgNum; i++)
{
location.x = (i%2)*col;
location.y = (i/2)*row;
cout<<location.x<<location.y<<imgStdSize<<finalImg.rows<<finalImg.cols<<i<<endl;
imagevec[i].copyTo(finalImg( Rect(location, Size(col,row)) ) );
}
return finalImg;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Mat src = imread("D:/pictures/3.jpg");
Mat dst,blurImg,gaussImg,medianImag;
resize(src,dst,Size(540,360));
blur(dst,blurImg,Size(5,5));
GaussianBlur(dst,gaussImg,Size(5,5),0);
medianBlur(dst,medianImag,5);
Mat graydst,grayblImg,grayGauImg,grayMedImg;
cvtColor(dst,graydst,COLOR_BGR2GRAY);
cvtColor(blurImg,grayblImg,COLOR_BGR2GRAY);
cvtColor(gaussImg,grayGauImg,COLOR_BGR2GRAY);
cvtColor(medianImag,grayMedImg,COLOR_BGR2GRAY);
puttext(&dst,Tenrgrad(&graydst),"src vaule is:");
puttext(&blurImg,Tenrgrad(&grayblImg),"blurImg vaule is:");
puttext(&gaussImg,Tenrgrad(&grayGauImg),"gaussImg vaule is:");
puttext(&medianImag,Tenrgrad(&grayMedImg),"medianImag vaule is:");
imagevec.push_back(dst);
imagevec.push_back(blurImg);
imagevec.push_back(gaussImg);
imagevec.push_back(medianImag);
Mat finalimg = MultImgShow(imagevec);
namedWindow("final res",WINDOW_AUTOSIZE);
imshow("final res",finalimg);
//cvtColor(dst,dst,COLOR_BGR2GRAY);
// imshow("dst",dst);
// imshow("src",src);
// imshow("blurImg",blurImg);
// imshow("gaussImg",gaussImg);
// imshow("medianImag",medianImag);
cout<<"dst tenrgrad vaule is : "<<Tenrgrad(&graydst)<<endl;
cout<<"blurImg tenrgrad vaule is : "<<Tenrgrad(&grayblImg)<<endl;
cout<<"gaussImg tenrgrad vaule is : "<<Tenrgrad(&grayGauImg)<<endl;
cout<<"medianImag tenrgrad vaule is : "<<Tenrgrad(&grayMedImg)<<endl;
return a.exec();
}
- 1
- 2
前往页