程序总体很简单 提取两幅图像的像素 做减法 求视差看看
但是 编译 通过
运行 报错:我的意图是左击鼠标 显示视差图 。在左击鼠标之前,能正常显示图,一左击 就弹出:f:\dd\vctools\crt_bld\self_x86\crt\srt\write.c
line:69
expression:_osfile(fh)&FOPEN
什么意思呢?????????????#include<windows.h>
#include<string.h>
#include<tchar.h>
#include"resource.h"
#include"fstream"
#define Row 640
#define Column 480
using namespace std;
//--------------------------------------------函数声明
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);//windows消息处理函数 O(∩_∩)O~
bool readBmp(char *,char*,int** pixel);
bool shicha(int **pixelL,int **pixelR);
ATOM MyRegisterClass(HINSTANCE);
//-------------------------------------------全局变量
//int pixelL[Row][Column];
//int pixelR[Row][Column];
FILE *fp;
HDC hdc;
HDC hdcmem;
HBITMAP hBm;//位图句柄
HBITMAP hBm1;//位图句柄BITMAP bm;
BITMAP bm1;HINSTANCE hInst;
WNDCLASSEX wndclass;
WNDCLASSEX ChildWndclass;
HWND childhwnd;
PAINTSTRUCT pt;
 FILE*fp1=fopen("33.bmp","wb");
//------------------------------------------windows程序入口函数
int WINAPI WinMain(
   HINSTANCE hInstance,
   HINSTANCE hPrevInst,
   LPSTR lpszCmdLine,
   int nCmdShow
   
   )
{HWND hwnd;
MSG Msg;
hInst=hInstance;
  wchar_t lpszTitle[]=_T("视差");
  MyRegisterClass(hInstance);
  //MyRegisterChildClass(hInstance);
  hwnd=CreateWindow(_T("盛安"),
               _T("视差"),
   WS_OVERLAPPEDWINDOW,
   0,
   0,
                   400,
   400,
   NULL,
   NULL,
   hInstance,
   NULL
   );
  hBm=LoadBitmap(hInstance,(LPCWSTR)IDB_BITMAP4);
  hBm1=LoadBitmap(hInstance,(LPCWSTR)IDB_BITMAP3);
  GetObject(hBm,sizeof(BITMAP),(LPVOID)&bm);
  GetObject(hBm1,sizeof(BITMAP),(LPVOID)&bm1);  ShowWindow(hwnd,nCmdShow);
  UpdateWindow(hwnd);
  while(GetMessage(&Msg,NULL,0,0))
  {
  TranslateMessage(&Msg);
  DispatchMessage(&Msg);
  
  }
  return Msg.wParam;}
//--------------------------------------------父窗口消息处理函数
LRESULT CALLBACK WndProc(HWND hwnd,
 UINT message,
 WPARAM wParam,
 LPARAM lParam
 )
{//动态分配两个二维数组,因为栈的大小有限
int **pixelL=new int*[Row];
for(int i=0;i<Row;i++)
pixelL[i]=new int[Column];int **pixelR=new int*[Row];
for(int i=0;i<Row;i++)
pixelR[i]=new int[Column];switch(message)
  {
case WM_CREATE:
hdc=GetDC(hwnd);
hdcmem=CreateCompatibleDC(hdc);//获取内存设备环境句柄
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
    hdc=BeginPaint(hwnd,&pt);
SelectObject(hdcmem,hBm);//把位图选入内存设备环境句柄
BitBlt(hdc,0,0,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);//把位图从内存设备环境句柄拷贝到设备环境句柄中,实现位图输出

SelectObject(hdcmem,hBm1);
break;
case WM_DESTROY:
DeleteObject(hBm);
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
readBmp("L.bmp","pixelL.txt",pixelL);
readBmp("R.bmp","pixelR.txt",pixelR);
shicha(pixelL,pixelR);
    
//SelectObject(hdcmem,hBm1);
BitBlt(hdc,640,0,bm1.bmWidth,bm1.bmHeight,hdcmem,0,0,SRCCOPY);
    //hdc=BeginPaint(hwnd,&pt);
//SetTextColor(hdc,RGB(0,255,0));
   // TextOut(hdc,bm.bmWidth,bm.bmHeight,_T("SSSS"),3);
EndPaint(hwnd,&pt);
break;

default:
return DefWindowProc(hwnd,message,wParam,lParam);
  }
 //释放动态申请的内存
for(int   i=0;   i <Row;   i++) 
    delete   []   pixelL[i]; 
delete   []   pixelL; for(int   i=0;   i <Row;   i++) 
    delete   []   pixelR[i]; 
delete   []   pixelR; 
 
 
 
return 0;}
//-----------------------------------------注册父窗口类
ATOM MyRegisterClass(HINSTANCE hInstance)
{
wndclass.cbSize = sizeof(WNDCLASSEX);  
wndclass.style=0;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.hIconSm=NULL;
wndclass.lpszClassName=_T("盛安");
return  RegisterClassEx(&wndclass);
}//读左右两幅图片的像素进内存PBmpBufferL和PBmpBufferR里,并且把像素值保存于pixelL[Row][Column]
//和pixelR[Row][Column]中,写入pixelL.txt和pixelR.txt中
bool readBmp(char *bmpname,char *txtname,int **pixel)
{
/*pixel=new int*[Row];
for(int i=0;i<Row;i++)
pixel[i]=new int[Column];*/
int bitBitCount;//定义变量,用于存放图片每行像素所占的字节数
int n=0;
 FILE *fp=fopen(bmpname,"rb");//打开要读的图片
 if(fp==0)
 return 0;
  ofstream outfile(txtname,ios::in|ios::trunc);//打开pixel.txt,用于存放像素值
  if(!outfile)
     exit(1);
 fseek(fp,sizeof(BITMAPFILEHEADER),0);
 BITMAPINFOHEADER head;
 fread(&head,sizeof(BITMAPINFOHEADER),1,fp);
 bitBitCount=head.biBitCount;//由于要用到的图像为位深为24的,简单起见,其他位深的本程序没有考虑O(∩_∩)O~
 int lineByte=(head.biWidth*bitBitCount/8+3)/4*4;
 unsigned char* pBmpBuffer=new unsigned char[lineByte *head.biHeight];
 fread(pBmpBuffer,3,lineByte*head.biHeight/3,fp);//读入像素进内存
 for(int i= 0;i<=head.biHeight-1;i++)
    {
for(int j=0;j<head.biWidth;j++)
 {
 pixel[i][j]=*(pBmpBuffer+i*lineByte+j);         outfile<<pixel[i][j]<<" ";
         n++;
 if(n%480==0)
 outfile<<endl;
 }
    } BITMAPFILEHEADER fileHead;
 fileHead.bfType=0x4D42;
 fileHead.bfSize= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)  + lineByte*head.biHeight;
    fileHead.bfReserved1 = 0;
    fileHead.bfReserved2 = 0;
    //bfOffBits是图像文件前3个部分所需空间之和
    fileHead.bfOffBits=54;
    //写文件头进文件
    fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp1);
    //申请位图信息头结构变量,填写信息头信息
    BITMAPINFOHEADER _head; 
    _head.biBitCount=bitBitCount;
    _head.biClrImportant=0;
    _head.biClrUsed=0;
    _head.biCompression=0;
_head.biHeight=head.biHeight;
    _head.biPlanes=1;
    _head.biSize=40;
_head.biSizeImage=lineByte*head.biHeight;
_head.biWidth=head.biWidth;
_head.biXPelsPerMeter=head.biXPelsPerMeter;
_head.biYPelsPerMeter=head.biYPelsPerMeter;
   //写位图信息头进内存
    fwrite(&_head, sizeof(BITMAPINFOHEADER),1, fp1);
    fwrite(pBmpBuffer ,_head.biHeight*lineByte, 1, fp1);    delete pBmpBuffer;
  /* for(int   i=0;   i <Row;   i++) 
    delete   []   pixel[i]; 
    delete   []   pixel; */    fclose(fp);
    fclose(fp1); 
return 1;
}
//------------------------------------------------------------
bool shicha(int **pixelL,int** pixelR){ 
/*pixelL=new int*[Row];
for(int i=0;i<Row;i++)
pixelL[i]=new int[Column]; pixelR=new int*[Row];
for(int i=0;i<Row;i++)
pixelR[i]=new int[Column];*/
int ** pixel=new int*[Row];
for(int i=0;i<Row;i++)
pixel[i]=new int[Column];
unsigned char* pBmpBuffer=new unsigned char[Row*Column];
// int pixel[Row][Column];
 for(int i=0;i<Row;i++)
  for(int j=0;j<Column;j++)
  {
 pixel[i][j]=pixelL[i][j]-pixelR[i][j];
  *(pBmpBuffer+i*Column+j)= pixel[i][j];   }
    fwrite(pBmpBuffer ,Row*Column, 1, fp1);
    delete pBmpBuffer;
   for(int   i=0;   i <Row;   i++) 
    delete   []   pixelL[i]; 
/*delete   []   pixelL; for(int   i=0;   i <Row;   i++) 
    delete   []   pixelR[i]; 
delete   []   pixelR; */
   //delete   []   pixelL; for(int   i=0;   i <Row;   i++) 
    delete   []   pixel[i]; 
delete   []   pixel;
 
  
return 1;}