#include "cv.h"
#include "highgui.h"
#include "stdio.h"
#include "math.h"
#include "iostream"
using namespace std;
#define WIDTHBYTES(bits) (((bits)+31)/32*4)
int m_Kmax = 5;
typedef struct tagSIZE1{
LONG cx;
LONG cy;
} CSize;
void ComputeCoarseness(unsigned char **img, CSize szImg, int kMax, double &FCrs)
{
int nLineBytes = WIDTHBYTES(szImg.cx*8);
int i,j,k;
int m,n;
double*** A = new double**[kMax]; //平均强度值
double*** Eh = new double**[kMax]; //水平方向平均强度差
double*** Ev = new double**[kMax]; //竖直方向平均强度差
for (k=0; k<kMax; k++)
{
A[k] = new double*[szImg.cy];
Eh[k] = new double*[szImg.cy];
Ev[k] = new double*[szImg.cy];
for (j=0; j<szImg.cy; j++)
{
A[k][j] = new double[szImg.cx];
Eh[k][j] = new double[szImg.cx];
Ev[k][j] = new double[szImg.cx];
}
}
for (k=0; k<kMax; k++)
{
for (j=0; j<szImg.cy; j++)
{
for (i=0; i<szImg.cx; i++)
{
A[k][j][i] = 0.0;
Eh[k][j][i] = 0.0;
Ev[k][j][i] = 0.0;
}
}
}
for (k=1; k<=kMax; k++)
{
int radius = pow((double)2,(double) k-1);
int num = 4*radius*radius;
if (szImg.cx<radius*2 || szImg.cy<radius*2)
{
break;
}
for (j=radius; j<szImg.cy-radius+1; j++)
{
for (i=radius; i<szImg.cx-radius+1; i++)
{
//计算A
for (n=j-radius; n<j+radius; n++)
{
for (m=i-radius; m<i+radius; m++)
{
A[k-1][j][i] += (double)img[j][i];
}
}
A[k-1][j][i] /= (double)num;
}
}//end of A
//求Eh和Ev
for (j=2*radius; j<szImg.cy-2*radius+1; j++)
{
for (i=2*radius; i<szImg.cx-2*radius+1; i++)
{
Eh[k-1][j][i] = fabs(A[k-1][j][i+radius] - A[k-1][j][i-radius]);
Ev[k-1][j][i] = fabs(A[k-1][j+radius][i] - A[k-1][j-radius][i]);
}
}
}
//每个像素点的最佳尺寸
int** Sbest;
Sbest = new int*[szImg.cy];
for (j=0; j<szImg.cy; j++)
{
Sbest[j] = new int[szImg.cx];
}
for (j=0; j<szImg.cy; j++)
{
for (i=0; i<szImg.cx; i++)
{
Sbest[j][i] = 0;
}
}
for (j=0; j<szImg.cy; j++)
{
for (i=0; i<szImg.cx; i++)
{
double Sbesth = 0.0;
double Sbestv = 0.0;
int kh = 0, kv = 0;
for (k=1; k<=kMax; k++)
{
if (Eh[k-1][j][i] > Sbesth)
{
Sbesth = Eh[k-1][j][i];
kh = k;
}
if (Ev[k-1][j][i] > Sbestv)
{
Sbestv = Ev[k-1][j][i];
kv = k;
}
}
if (Sbesth > Sbestv)
{
Sbest[j][i] = kh;
}
else
{
Sbest[j][i] = kv;
}
}
}
int sum = 0;
int count = 0;
for (j=0; j<szImg.cy; j++)
{
for (i=0; i<szImg.cx; i++)
{
if (Sbest[j][i])
{
sum += Sbest[j][i];
count++;
}
}
}
int allsize=150000+szImg.cy*szImg.cx-count;
FCrs = (double)sum/(double)allsize;
}
int main()
{
IplImage *img00;
IplImage *img0;
IplImage *img1;
IplImage *img2;
IplImage *img3;
IplImage *img4;
IplImage *b;
IplImage *g;
IplImage *r;
BYTE *pixelData; //像素信息
int size;
// img00=cvLoadImage("C:\\Users\\lc\\Desktop\\新建文件夹1\\1.jpg");
img00=cvLoadImage("C:\\Users\\lc\\Desktop\\活\\海面粗糙度\\发\\DSCN1387.JPG");
img0=cvCreateImage(cvSize(img00->width/5,img00->height/5),img00->depth,3);
cvResize(img00,img0);
//img0=cvLoadImage("delta25.bmp",1);
img1=cvCreateImage(cvGetSize(img0),img0->depth,1);
b=cvCreateImage(cvGetSize(img0),img0->depth,1);
g=cvCreateImage(cvGetSize(img0),img0->depth,1);
r=cvCreateImage(cvGetSize(img0),img0->depth,1);
//img3=cvCreateImage(cvSize(40,40),img1->depth,1);
// cvCvtColor(img0,img1,CV_BGR2GRAY);
cvSplit(img0,b,g,r,NULL);
double result;
for (int ii=0;ii<3;ii++)
{
if (ii==0)
{
cvCopy(b,img1);
cvNamedWindow("b",0);
cvShowImage("b",b);
// unsigned char* pdata=(unsigned char*)calloc(b->width*b->height,1);
CSize szImg;
szImg.cx=b->width;
szImg.cy=b->height;
unsigned char* img=(unsigned char *)b->imageData;
unsigned char** pImg;
pImg = new unsigned char*[szImg.cy];
for (int j=0; j<szImg.cy; j++)
{
pImg[j] = new unsigned char[szImg.cx];
}
int nLineBytes = WIDTHBYTES(szImg.cx*8);
for (int j=0; j<szImg.cy; j++)
{
for (int i=0; i<szImg.cx; i++)
{
pImg[j][i] = img[j*nLineBytes+i];
}
}
ComputeCoarseness(pImg, szImg, m_Kmax, result);
cout<<"该图像b分量的粗糙度是:"<<result<<endl;
}
else if(ii==1)
{
cvCopy(g,img1);
cvNamedWindow("g",0);
cvShowImage("g",g);
CSize szImg;
szImg.cx=g->width;
szImg.cy=g->height;
unsigned char* img=(unsigned char *)g->imageData;
unsigned char** pImg;
pImg = new unsigned char*[szImg.cy];
for (int j=0; j<szImg.cy; j++)
{
pImg[j] = new unsigned char[szImg.cx];
}
int nLineBytes = WIDTHBYTES(szImg.cx*8);
for (int j=0; j<szImg.cy; j++)
{
for (int i=0; i<szImg.cx; i++)
{
pImg[j][i] = img[j*nLineBytes+i];
}
}
ComputeCoarseness(pImg, szImg, m_Kmax, result);
cout<<"该图像g分量的粗糙度是:"<<result<<endl;
}
else if(ii==2)
{
cvCopy(r,img1);
cvNamedWindow("r",0);
cvShowImage("r",r);
CSize szImg;
szImg.cx=r->width;
szImg.cy=r->height;
unsigned char* img=(unsigned char *)r->imageData;
unsigned char** pImg;
pImg = new unsigned char*[szImg.cy];
for (int j=0; j<szImg.cy; j++)
{
pImg[j] = new unsigned char[szImg.cx];
}
int nLineBytes = WIDTHBYTES(szImg.cx*8);
for (int j=0; j<szImg.cy; j++)
{
for (int i=0; i<szImg.cx; i++)
{
pImg[j][i] = img[j*nLineBytes+i];
}
}
ComputeCoarseness(pImg, szImg, m_Kmax, result);
cout<<"该图像r分量的粗糙度是:"<<result<<endl;
}
}
cvCvtColor(img0,img1,CV_BGR2GRAY);
CSize szImg;
szImg.cx=img1->width;
szImg.cy=img1->height;
unsigned char* img=(unsigned char *)img1->imageData;
unsigned char** pImg;
pImg = new unsigned char*[szImg.cy];
for (int j=0; j<szImg.cy; j++)
{
pImg[j] = new unsigned char[szImg.cx];
}
int nLineBytes = WIDTHBYTES(szImg.cx*8);
for (int j=0; j<szImg.cy; j++)
{
for (int i=0; i<szImg.cx; i++)
{
pImg[j][i] = img[j*nLineBytes+i];
}
}
ComputeCoarseness(pImg, szImg, m_Kmax, result);
cout<<"该图像灰度量的粗糙度是:"<<result<<endl;
#if 0
//初始图像数组
size=img1->height*img1->width;
pixelData = new BYTE[size];
int **map = new int* [img1->height];
for(int i=0; i<img1->height; i++)
map[i] = new int[img1->width];
//给图像数组赋值
int sizeNumber=0;
for(int m=0; m<img1->height; m++)
{
for(int n=0; n<img1->width; n++)
{
map[m][n]=(int)pixelData[sizeNumber++];
if(sizeNumber==size)
break;
}
}
float **A_k1;
float **A_k2;
float **A_k3;
float **A_k4;
float **A_k5;
//求解粗糙度均值数组
A_k1 = new float* [img1->height];
for(int i1=0; i1<img1->height; i1++)
{
A_k1[i1] = new float[img1->width];
memset(A_k1[i1], 0, sizeof(float)*img1->width);
}
A_k2 = new float* [img1->height];
for(int i2=0; i2<img1->height; i2++)
{
A_k2[i2] = new float[img1->width];
memset(A_k2[i2], 0, sizeof(float)*img1->width);
}
A_k3 = new float* [img1->height];
for(int i3=0; i3<img1->height; i3++)
{
A_k3[i3] = new float[img1->width];
memset(A_k3[i3], 0, sizeof(float)*img1->width);
}
A_k4 = new float* [img1->height];
for(int i4=0; i4<img1->height; i4++)
{
A_k4[i4] = new float[img1->width];
memset(A_k4[i4], 0, sizeof(float)*img1->width);
}
A_k5 = new float* [img1->height];
for(int i5=0; i5<img1->height; i5++)
{
A_k5[i5] = new float[img1->width];
memset(A_k5[i5], 0, sizeof(float)*img1->width);
}
//k=1
for(int x1=0; x1<img1->height; x1++)
{
for(int y1=0; y1<img1->width; y1++)
{
int count1=0;
for(int ii1=x1-1; ii1<=x1; ii1++)
{
for(int jj1=y1-1; jj1<=y1; jj1++)
{
if(ii1<0 || jj1<0 || ii1>=img1->height || jj1>=img1->width)
continue;
else
count1++;
A
评论0