19,472
社区成员




string image_name;
cout<<endl<<"输入图像名:";
cin>>image_name;
Mat image_dst;
if (!isRGB)
{
image_dst = imread(image_name,0);
}
else
{
image_dst = imread(image_name,1);
}
if(image_dst.empty())
{
return -1;
} //是否加载成功
imshow(image_name,image_dst);
width = image_dst.cols;
height = image_dst.rows;
int channel = image_dst.channels();
int step = (width * channel+3)/4*4;
//step = width*channel;
//step = step/4*4;
uchar* ps = NULL;
p1 = new BYTE[step*height];
//size_mem = sizeof(p1);
/*这部分逐个像素表示图像。像素是从下到上、从左到右保存的。每个像素使用一个或者多个字节表示。
如果一个图像水平线的字节数不是4的倍数,这行就使用空字节补齐,通常是ASCII码0。
范例: 有一张5*5的图片,应该会有25个pixels,但是因为5不是4的倍数所以会显示成:
xxxxx000 xxxxx000 xxxxx000 xxxxx000 xxxxx000
x代表调色盘的编号 0代表Null_character
*/
for (int i = 0; i < height; i++)
{
ps = image_dst.ptr<uchar>(i);
int bmp_i = height - i;//opencv mat需要反转一下从左下角开始赋值
for (int j = 0; j < width; j++)
{
if (1 == channel)
{
*(p1 + bmp_i*step + j) = ps[j];
}
else if (3 == channel)
{ //按照bgr三通道分别赋值
*(p1 + bmp_i*step +j) = ps[j*3];
*(p1 + bmp_i*step + j*3 + 1) = ps[j*3 + 1];
*(p1 + bmp_i*step + j*3 + 2) =ps[j*3 + 2] ;
}
}
}
BYTE* px;
if(!isRGB)
{
px = Read8BitBmpFile2Img(image_name.c_str(),&width,&height);
}
else
{
px = Read24BitBmpFile2Img(image_name.c_str(),&width,&height);
}
int len = width*height*channel;
int huanhang = width *channel;
FILE *pFile1 = fopen("out_opencv.txt", "w"); // 文件打开方式 如果原来有内容也会销毁
FILE *pFile2 = fopen("out_ori.txt", "w"); // 文件打开方式 如果原来有内容也会销毁
for (int i = 1;i <=len;i++)
{
//fwrite ((int)*(p1+i), 1,1, pFile);
//cout<<(int)*(p1+i)<<"\t";
fprintf(pFile1,"%d\t",(int)*(p1+i));
if (i % (huanhang)==0)
{
fprintf(pFile1,"\r\n");
//cout<<"kk"<<endl;
/*fwrite ("\n", 1,strlen("\n"), pFile);*/
}
}
cout<<"下面是正常的"<<endl;
//fwrite ("hello\n", 1,strlen("hello\n")+1, pFile );
for (int i = 1;i <=len;i++)
{
fprintf(pFile2,"%d\t",(int)*(px+i));
if (i % huanhang==0)
{
fprintf(pFile2,"\r\n");
/*fwrite ("\n", 1,strlen("\n"), pFile);*/
}
}
fclose(pFile1);
fclose(pFile2);
fflush(pFile1);
fflush(pFile2);
//////////////////////////
// Read a 24 bit bmp File
//////////////////////////
BYTE *Read24BitBmpFile2Img(const char * filename,int *width,int *height)
{
FILE * BinFile;
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER BmpHeader;
BYTE *img;
int size;
int Suc=1;
// Open File
*width=*height=0;
if((BinFile=fopen(filename,"rb"))==NULL) return NULL;
// Read Struct Info
if (fread((void *)&FileHeader,1,sizeof(FileHeader),BinFile)!=sizeof(FileHeader)) Suc=-1;
if (fread((void *)&BmpHeader,1,sizeof(BmpHeader),BinFile)!=sizeof(BmpHeader)) Suc=-1;
if (Suc==-1) { fclose(BinFile); return NULL; }
// Read Image Data
*width=(BmpHeader.biWidth+3)/4*4;
*height=BmpHeader.biHeight;
size=(*width)*(*height)*3;
fseek(BinFile,FileHeader.bfOffBits,SEEK_SET);
if ( (img=new BYTE[size+8])!=NULL)
{
//size_mem = sizeof(img);
if(fread(img+8-int(img)%8,sizeof(BYTE),size,BinFile)!=(unsigned int)size)
{
fclose(BinFile);
delete img;
img=NULL;
return NULL;
}
}
fclose(BinFile);
return img;
}
void HexDump(char *buf,int len,int addr) {
int i,j,k;
char binstr[80];
for (i=0;i<len;i++) {
if (0==(i%16)) {
sprintf(binstr,"%08x -",i+addr);
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
sprintf(binstr,"%s ",binstr);
for (j=i-15;j<=i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
} else {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
sprintf(binstr,"%s ",binstr);
k=16-k;
for (j=i-k;j<i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
}
}
// Retinex.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Retinex.h"
//////////////////////////
// main
//////////////////////////
int main()
{
BYTE *OrgImg,*ResImg,*p1,*p2;
int width,height,i,j,suc,area1,area2,Tsize;
bool isRGB,ret;
clock_t t1,t2;
double emee,ame;
char ch;
int *offsetdata=new int[2];
for(i=0;i<2;i++)
{
*(offsetdata+i)=0X80808080;
}
system( "cls" );
printf("******中心/环绕Retienx算法******************\n");
printf(" 1.处理灰度图像\n");
printf(" 2.处理彩色图像\n");
printf("*********************************************\n");
printf("请选择(1或2): ");
do
{
cin>>ch;
}while( ch != '1' && ch != '2');
//system ( "cls" );
if ( ch == '1')
isRGB=false;
else if ( ch == '2')
isRGB=true;
// open file
string image_name;
cout<<endl<<"输入图像名:";
cin>>image_name;
Mat image_dst;
if (!isRGB)
{
image_dst = imread(image_name,0);
}
else
{
image_dst = imread(image_name,1);
}
if(image_dst.empty())
{
return -1;
} //是否加载成功
imshow(image_name,image_dst);
width = image_dst.cols;
height = image_dst.rows;
int channel = image_dst.channels();
int step = width * channel* 1;
uchar* ps = NULL;
p1 = new BYTE[width*height*channel+8];
for (int i = 0; i < height; i++)
{
ps = image_dst.ptr<uchar>(i);
int bmp_i = height -1 -i;//图片上下倒置
for (int j = 0; j <width; j++)
{
if (1 == channel)
{
*(p1 + i*step + j) = ps[j];
}
else if (3 == channel)
{ //opencv的顺序是bgr,bmp本来是grb?
*(p1 + bmp_i*step + j*3) = ps[j*3 + 1]; //g
*(p1 + bmp_i*step + j*3 + 1) = ps[j*3 + 2]; //r
*(p1 + bmp_i*step + j*3 + 2) = ps[j*3]; //b
}
}
}
BYTE* px;
if(!isRGB)
px = Read8BitBmpFile2Img(image_name.c_str(),&width,&height);
else
px = Read24BitBmpFile2Img(image_name.c_str(),&width,&height);
int len = width*height*channel;
int huanhang = width *channel;
FILE *pFile1 = fopen("out_opencv.txt", "w"); // 文件打开方式 如果原来有内容也会销毁
FILE *pFile2 = fopen("out_ori.txt", "w"); // 文件打开方式 如果原来有内容也会销毁
for (int i = 1;i <=len;i++)
{
fprintf(pFile1,"%d\t",(int)*(p1+i));
if (i % (huanhang)==0)
{
fprintf(pFile1,"\n");
}
}
cout<<"下面是正常的"<<endl;
for (int i = 1;i <=len;i++)
{
fprintf(pFile2,"%d\t",(int)*(px+i));
if (i % huanhang==0)
{
fprintf(pFile2,"\n");
}
}
fclose(pFile1);
fclose(pFile2);
fflush(pFile1);
fflush(pFile2);
if (p1==NULL)
{
printf("*fopen err!\n");
delete p1;
return 0;
}
if(width%64!=0||height%64!=0)
{
cout<<"图像大小需是64的倍数"<<endl;
delete p1;
return 0;
}
area1=width*height;
if(!isRGB)
{
OrgImg=p1+8-int(p1)%8;
p2=new BYTE[width*height+8];
ResImg=p2+8-int(p2)%8;
t2=clock();
GrayImageProcess(OrgImg,width,height,ResImg);
t1=clock();
printf("*****灰度图像处理结果******************\n");
cout<<"运行时间:"<<t1-t2<<"ms"<<endl;
Measure(ResImg,width,height,emee,ame);
cout<<"EMEE:"<<emee<<endl;
suc=Write8BitImg2BmpFile(ResImg,width,height,"result_1.bmp");
}
else
{
OrgImg=p1+8-int(p1)%8;
p2=new BYTE[width*height*3+8];
ResImg=p2+8-int(p2)%8;
t2=clock();
ColorImageProcess(OrgImg,width,height,ResImg);
t1=clock();
cout<<"运行时间:"<<t1-t2<<"ms"<<endl;
t2=clock();
suc=Write24BitImg2BmpFile(ResImg,width,height,"result_1.bmp");
ColorImageProcess2(OrgImg,width,height,ResImg);
t1=clock();
cout<<"运行时间:"<<t1-t2<<"ms"<<endl;
suc=Write24BitImg2BmpFile(ResImg,width,height,"result_2.bmp");
}
if(suc==-1)
{
printf("*fwrite err!\n");
}
Mat result1 = imread("result_1.bmp",1);
Mat result2 = imread("result_2.bmp",1);
imshow("result1",result1);
imshow("result2",result2);
//release mem
delete p1;
delete p2;
waitKey(0);
return 0;
}