#include <stdio.h>
#include <windows.h>
#include <cmath>
void InitImage(BYTE *image[2000], int w, int h)
{
for(int i=0;i<h;i++)
image[i]=(BYTE *)new BYTE[w];
for(i=0;i<h;i++)
for(int j=0;j<w;j++)
image[i][j]=0;
}
void ReadRawFile(BYTE *image[2000], int w, int h, char filepath[200])
{
FILE *fp=fopen(filepath,"rb");
if(fp==NULL) {printf("error open %s\n",filepath);exit(0);}
for(int i=0;i<h;i++)
fread(image[i],1,w,fp);
fclose(fp);
}
void FanDa(BYTE *image[2000],int w,int h,BYTE *image1[2000],int w1,int h1)
{
for(int i=0;i<h1;i++)
for(int j=0;j<w1;j++)
{
image1[i][j]=image[(int)(i/(h1*1.0/h))][(int)(j/(w1*1.0/w))];
}
}
void SaveRawFile(BYTE *image[2000], int w, int h, char filepath[200])
{
FILE *fp=fopen(filepath,"wb");
if(fp==NULL) {printf("error open %s to write\n",filepath);exit(0);}
for(int i=0;i<h;i++)
fwrite(image[i],1,w,fp);
fclose(fp);
}
#define PI 3.14
#define D 5
#define HTHRESH 1000
int houghbianhuan(BYTE *image[2000], double xmax, double ymax, int &num, int *hough[180])
{
int i;
int j;
int x;
int y;
/* int Hthresh =100;
int d =3;*/
double rmin;
double rmax;
int jmax;
double t;
double r;
double cost;
double sint;
rmin = -xmax;
rmax = sqrt(xmax*xmax+ymax*ymax);
jmax = (int)floor((rmax-rmin)/D);
for(i=0;i<180;i++)
{
t=i*PI/180;
for(x=0;x<xmax;x++)
{
for(y=0;y<ymax;y++)
{
if(image[y][x] == 255)
{
cost=cos(t);
sint=sin(t);
r=x*cost+y*sint;
j=(int)floor((r-rmin)/D+D/2);
/*if((r-rmin)/d-j<d/2)
{
*/ hough[i][j]++;
/*}
else
{
hough[i][j+1]++;
}*/
}
}
}
}
for(i=0;i<180;i++)
{
for(j=0;j<jmax;j++)
{
if(hough[i][j]>HTHRESH)
{
num++;
}
}
}
return num;
}
void makeline(BYTE *image[2000], double xmax, double ymax,int *hough[180])
{
int i;
int j;
int x;
int y;
/* int Hthresh =100;
int d =3;*/
double rmin;
double rmax;
int jmax;
double t;
double r;
double cost;
double sint;
rmin = -xmax;
rmax = sqrt(xmax*xmax+ymax*ymax);
jmax = (int)floor((rmax-rmin)/D);
for(i=0;i<180;i++)
{
for(j=0;j<jmax;j++)
{
if(hough[i][j]>HTHRESH)
{
t=i*PI/180;
r=rmin+j*D;
cost=cos(t);
sint=sin(t);
for(x=0;x<xmax;x++)
{
for(y=0;y<ymax;y++)
{
if(fabs((x*cost+y*sint)-r)<.5)
{
image[y][x]=126;
}
}
}
}
}
}
}
void fangshuo(BYTE *image[2000],int w,int h,BYTE *image1[2000],int w1,int h1)
{
int i;
int j; /*目标图像像素坐标*/
int x;
int y; /*源图像像素坐标*/
int kx;
int ky; /*双线性插值函数相关系数*/
int pa;
int pb;
int pc;
int pd; /*源图像新坐标的邻近四点*/
for(i=0;i<h1;i++) /*目标图像行扫描*/
{
/*y=(int)floor(i*h/(double)h1); */
y=i*h/h1; /*对源图像新坐标的y轴值做舍小数取整*/
ky=h1-i*h%h1; /*双线性插值函数y轴相关系数*/
for(j=0;j<w1;j++) /*目标图像列扫描*/
{
/*x=(int)floor(j*w/(double)w1); */
x=j*w/w1; /*对源图像新坐标的x轴值做舍小数取整*/
kx=w1-j*w%w1; /*双线性插值函数x轴相关系数*/
pa=image[y][x]; /*确定邻近四点,左上*/
pb=image[y][x+1]; /*右上*/
pc=image[y+1][x]; /*左下*/
pd=image[y+1][x+1]; /*右下*/
image1[i][j]=(kx*ky*(pa-pb-pc+pd)+w1*ky*pb+h1*kx*pc+(w1*h1-h1*kx-w1*ky)*pd+w1*h1/2)/(w1*h1);
/*整数优化后的双线性插值公式*/
}
}
}