当前位置:文档之家› 摄像头编程并且对图像的处理以及直方图的算法

摄像头编程并且对图像的处理以及直方图的算法

题目:图像的基本处理班级:2011级软件2班姓名:刘磊磊时间:20130907摘要:随着数字化与多媒体时代的来临,数字图像处理已经成为必备的基础知识。

全国各大专院校的计算机、电子、通信、医学、光学及许多相关专业都开设了与数字图像预处理相关的课程。

数字图像二值化是图像预处理中的一项重要技术,其在模式识别、光学字符识别、医学成像等方面都有着重要应用。

本论文主要为大家介绍24位真彩图像的灰度、二值处理以及图像的一些简单的打开和保存和如何画直方图,还有一些通过这次小学期学到的一些知识。

关键字:灰度处理,二值化图像的打开void CText1Dlg::ShowPic(){if(m_path =="") //判断图片路径是否存在{return;}hwnd = GetDlgItem(IDC_pic);hDesDC = hwnd->GetDC()->m_hDC;hSrcDC = CreateCompatibleDC(hDesDC);hBitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(),m_path,IMAGE_BITMAP,0,0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);GetObject(hBitmap, sizeof(BITMAP), &bm);SelectObject(hSrcDC, hBitmap);hwnd->GetClientRect(&rect);::SetStretchBltMode(hDesDC,COLORONCOLOR);::StretchBlt(hDesDC, rect.left, rect.top, rect.right, rect.bottom, hSrcDC,0, 0, bm.bmWidth, bm.bmHeight,+SRCCOPY);UpdateData(false);}void CText1Dlg::Onopenpicture() //打开图像{// TODO: Add your control notification handler code hereinum = 1;CFileDialog dlg(TRUE,"bmp",".bmp",OFN_HIDEREADONL Y,"BMP Files(*.jpg)|*.bmp||");//设置所能打开图像的格式// CFileDialog dlg(TRUE,"jpg",".jpg",OFN_HIDEREADONL Y,"JPG Files(*.bmp)|*.jpg||");这是jpg图像的打开方式if(dlg.DoModal() != IDOK) // dlg_jpg.DoModal(){return;}m_path = dlg.GetPathName(); //获得图片路径UpdateData(false); //更新路径公共变量ShowPic(); //调用显示图片函数}图像的保存我的方法是先选择需要保存的图片并且这张图片是已经命名的,然后选择路径,最后用CopyFile(“原始图像全名包括扩展名”,“保存的全名包括路径+新的命名+扩展名”,false)就OK了。

下面是代码:void CForm::Onsaveroud(){// TODO: Add your control notification handler code hereCString strFolderPath="";TCHAR szPath[_MAX_PA TH];BROWSEINFO bi;bi.hwndOwner = GetSafeHwnd();bi.pidlRoot = NULL;bi.lpszTitle = _T("选择文件夹");bi.pszDisplayName = szPath;bi.ulFlags = BIF_RETURNONL YFSDIRS;bi.lpfn = NULL;bi.lParam = NULL;LPITEMIDLIST pItemIDList = SHBrowseForFolder(&bi);if(pItemIDList){if(SHGetPathFromIDList(pItemIDList,szPath)){strFolderPath = szPath;}// 防止内存泄露,要使用IMalloc接口IMalloc* pMalloc;if( SHGetMalloc(&pMalloc) != NOERROR ){TRACE(_T("无法取得外壳程序的IMalloc接口\n"));}pMalloc->Free(pItemIDList);if(pMalloc)pMalloc->Release();}ScanDiskFile(strFolderPath);}void CForm::ScanDiskFile(CString& strPath){CFileFind find;CString strTemp = strPath;CString strDirectory = strPath + _T("\\") + _T("\\*.*");CString strFile;BOOL IsFind = find.FindFile(strDirectory);IsFind=find.FindNextFile();// 如果是"." 则不扫描if(find.IsDots()){}// 如果是是目录,继续扫描此目录elseif(find.IsDirectory()){strFile = find.GetFileName();strTemp = strTemp ;//+ _T("\\") + strFile;//ScanDiskFile(strTemp);}m_path=strTemp;//m_path就是所要保存图片的路径UpdateData(false);find.Close();}彩色图像的灰度处理:图像灰度处理就是把各个像素点的R,G,B值设置为同一个值,当然要根据图像来取值。

void CText1Dlg::Oncolortogray() //彩色转换灰度{// TODO: Add your control notification handler code here// m_dwOperation = IMAGE_Text1_color_grayed_out;UpdateData();CString str;if(inum == 1)//判别是否是从其他路径读取{m_sourcefile = m_path;//m_sourcefile 是所要处理的彩色图片的路径及全名m_targetfile = m_path+"gray.bmp";//m_targetfile是处理过的图片的路径及全名str = m_targetfile;}m_sourcefile = CStimes;m_targetfile = "gray.bmp";str = m_targetfile;if(m_sourcefile == "" || m_targetfile == "")return;FILE *sourcefile,*targetfile;//位图文件头和信息头BITMAPFILEHEADER sourcefileheader,targetfileheader;BITMAPINFOHEADER sourceinfoheader,targetinfoheader;memset(&targetfileheader,0,sizeof(BITMAPFILEHEADER));memset(&targetinfoheader,0,sizeof(BITMAPINFOHEADER));sourcefile=fopen(m_sourcefile,"rb");fread((void*)&sourcefileheader,1,sizeof(BITMAPFILEHEADER),sourcefile);//提取原图文件头if(sourcefileheader.bfType!=0x4d42){fclose(sourcefile);MessageBox("原图象不为BMP图象!");return;}fread((void*)&sourceinfoheader,1,sizeof(BITMAPINFOHEADER),sourcefile);//提取文件信息头if(sourceinfoheader.biBitCount!=24){fclose(sourcefile);MessageBox("原图象不为24位真彩色!");return;}if(sourceinfoheader.biCompression!=BI_RGB){fclose(sourcefile);MessageBox("原图象为压缩后的图象,本程序不处理压缩过的图象!");return;}//构造灰度图的文件头targetfileheader.bfOffBits=54+sizeof(RGBQUAD)*256;targetfileheader.bfSize=targetfileheader.bfOffBits+sourceinfoheader.biSizeImage/3;targetfileheader.bfReserved1=0;targetfileheader.bfReserved2=0;targetfileheader.bfType=0x4d42;//构造灰度图的信息头,这些都是固定值targetinfoheader.biBitCount=8;targetinfoheader.biSize=40;targetinfoheader.biHeight=sourceinfoheader.biHeight;targetinfoheader.biWidth=sourceinfoheader.biWidth;targetinfoheader.biPlanes=1;targetinfoheader.biCompression=BI_RGB;targetinfoheader.biSizeImage=sourceinfoheader.biSizeImage/3;targetinfoheader.biXPelsPerMeter=sourceinfoheader.biXPelsPerMeter;targetinfoheader.biYPelsPerMeter=sourceinfoheader.biYPelsPerMeter;targetinfoheader.biClrImportant=0;targetinfoheader.biClrUsed=256;//构造灰度图的调色版RGBQUAD rgbquad[256];// color=(RGBMixPlate *)malloc(sizeof(RGBMixPlate)*256);// index=(BYTE *)malloc(sizeof(BYTE)*infohead.readSize);int i,j,m,n,k;for(i=0;i<256;i++){rgbquad[i].rgbBlue=i;rgbquad[i].rgbGreen=i;rgbquad[i].rgbRed=i;rgbquad[i].rgbReserved=0;}targetfile=fopen(m_targetfile,"wb");//写入灰度图的文件头,信息头和调色板信息fwrite((void*)&targetfileheader,1,sizeof(BITMAPFILEHEADER),targetfile);fwrite((void*)&targetinfoheader,1,sizeof(BITMAPINFOHEADER),targetfile);fwrite((void*)&rgbquad,1,sizeof(RGBQUAD)*256,targetfile);BYTE* sourcebuf;BYTE* targetbuf;//这里是因为BMP规定保存时长度和宽度必须是4的整数倍,如果不是则要补足m=(targetinfoheader.biWidth/4)*4;if(m<targetinfoheader.biWidth)m=m+4;n=(targetinfoheader.biHeight/4)*4;if(n<targetinfoheader.biHeight)n=n+4;sourcebuf=(BYTE*)malloc(m*n*3);//读取原图的颜色矩阵信息fread(sourcebuf,1,m*n*3,sourcefile);fclose(sourcefile);targetbuf=(BYTE*)malloc(m*n);BYTE BYcolor[3];// 通过原图颜色矩阵信息得到灰度图的矩阵信息for(i=0;i<n;i++){for(j=0;j<m;j++){for(k=0; k<3; k++)BYcolor[k]=sourcebuf[(i*m+j)*3+k];targetbuf[(i*m)+j]=BYcolor[0]*0.114+BYcolor[1]*0.587+BYcolor[2]*0.299;if(targetbuf[(i*m)+j]>255)targetbuf[(i*m)+j]=255;}}fwrite((void*)targetbuf,1,m*n+1,targetfile);fwrite(&targetinfoheader,1,12,targetfile);fwrite(&targetinfoheader,1,40,targetfile);fclose(targetfile);////////////////////////////////////////////////////preview(IDC_pic,str);//灰度预览}void CText1Dlg::preview(int control, CString CStimes)//预览图片{//conture是所要显示图片的picture控件的ID,CStimes是所要预览的图片的全名(包括扩展名)CString filepath;filepath = CStimes;IStream* stream;IPicture* picture;OLE_XSIZE_HIMETRIC m_width;OLE_YSIZE_HIMETRIC m_height;HGLOBAL hmem;CFile file;file.Open(filepath,CFile::modeReadWrite);DWORD len=file.GetLength();hmem=GlobalAlloc(GMEM_MOVEABLE,len);LPVOID pData=NULL;pData=GlobalLock(hmem);file.ReadHuge(pData,len);file.Close();GlobalUnlock(hmem);CreateStreamOnHGlobal(hmem,TRUE,&stream);OleLoadPicture(stream,len,TRUE,IID_IPicture,(LPVOID*)&picture);picture->get_Height(&m_height);picture->get_Width(&m_width);int width=(int)(240);int height=(int)(225);// int width=(int)(m_width/26.45);// int height=(int)(m_height/26.45);CRect rect1,rect2;GetClientRect(rect1);GetWindowRect(rect2);int x,y;x=rect1.Width()>(width+10)?0:(width+10-rect1.Width());y=rect1.Height()>(height+10)?0:(height+10-rect1.Height());rect2.InflateRect(x/2,y/2);MoveWindow(rect2);CenterWindow();//CDC* pDC=GetDC();CWnd *pi=GetDlgItem(control);//---注意:GetDlgItem是Cwnd类中函数,在此格式是继承;CDC *dc=pi->GetDC(); //picture->Render(dc->m_hDC,0,0,width,height,0,m_height,m_width,-m_height,NULL);pi->ReleaseDC(dc);////}彩色图像的二值化:图像的二值化就是将图像上的每个像素点的灰度值设置为0或255,也就是将整个图变为明显的黑白效果。

相关主题