版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
题目:图像的基本处理班级:2011级软件2班姓名:刘磊磊时间:20130907摘要:随着数字化与多媒体时代的来临,数字图像处理已经成为必备的基础知识。全国各大专院校的计算机、电子、通信、医学、光学及许多相关专业都开设了与数字图像预处理相关的课程。数字图像二值化是图像预处理中的一项重要技术,其在模式识别、光学voidCText1Dlg::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);}voidCText1Dlg::Onopenpicture()//打开图像{ //TODO:Addyourcontrolnotificationhandlercodehere inum=1; CFileDialogdlg(TRUE,"bmp",".bmp",OFN_HIDEREADONLY,"BMPFiles(*.jpg)|*.bmp||");//设置所能打开图像的格式// CFileDialogdlg(TRUE,"jpg",".jpg",OFN_HIDEREADONLY,"JPGFiles(*.bmp)|*.jpg||");这是jpg图像的打开方式 if(dlg.DoModal()!=IDOK)//dlg_jpg.DoModal() { return; } m_path=dlg.GetPathName();//获得图片路径 UpdateData(false);//更新路径公共变量 ShowPic();//调用显示图片函数 }图像的保存我的方法是先选择需要保存的图片并且这张图片是已经命名的,然后选择路径,最后用CopyFile(“原始图像全名包括扩展名”,“保存的全名包括路径+新的命名+扩展名”,false)就OK了。下面是代码:voidCForm::Onsaveroud(){ //TODO:Addyourcontrolnotificationhandlercodehere CStringstrFolderPath="";TCHARszPath[_MAX_PATH];BROWSEINFObi;bi.hwndOwner=GetSafeHwnd();bi.pidlRoot=NULL;bi.lpszTitle=_T("选择文件夹");bi.pszDisplayName=szPath;bi.ulFlags=BIF_RETURNONLYFSDIRS;bi.lpfn=NULL;bi.lParam=NULL;LPITEMIDLISTpItemIDList=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); }voidCForm::ScanDiskFile(CString&strPath){ CFileFindfind;CStringstrTemp=strPath;CStringstrDirectory=strPath+_T("\\")+_T("\\*.*");CStringstrFile;BOOLIsFind=find.FindFile(strDirectory);IsFind=find.FindNextFile(); //如果是"."则不扫描if(find.IsDots()) {}//如果是是目录,继续扫描此目录else if(find.IsDirectory()){strFile=find.GetFileName();strTemp=strTemp;//+_T("\\")+strFile;//ScanDiskFile(strTemp);} m_path=strTemp;//m_path就是所要保存图片的路径UpdateData(false);find.Close();}彩色图像的灰度处理:图像灰度处理就是把各个像素点的R,G,B值设置为同一个值,当然要根据图像来取值。voidCText1Dlg::Oncolortogray()//彩色转换灰度{ //TODO:Addyourcontrolnotificationhandlercodehere// m_dwOperation=IMAGE_Text1_color_grayed_out; UpdateData(); CStringstr; 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; //位图文件头和信息头 BITMAPFILEHEADERsourcefileheader,targetfileheader; BITMAPINFOHEADERsourceinfoheader,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; //构造灰度图的调色版 RGBQUADrgbquad[256];// color=(RGBMixPlate*)malloc(sizeof(RGBMixPlate)*256);// index=(BYTE*)malloc(sizeof(BYTE)*infohead.readSize); inti,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); BYTEBYcolor[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);//灰度预览}voidCText1Dlg::preview(intcontrol,CStringCStimes)//预览图片{//conture是所要显示图片的picture控件的ID,CStimes是所要预览的图片的全名(包括扩展名) CStringfilepath; filepath=CStimes; IStream*stream; IPicture*picture; OLE_XSIZE_HIMETRICm_width; OLE_YSIZE_HIMETRICm_height; HGLOBALhmem; CFilefile; file.Open(filepath,CFile::modeReadWrite); DWORDlen=file.GetLength(); hmem=GlobalAlloc(GMEM_MOVEABLE,len); LPVOIDpData=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); intwidth=(int)(240); intheight=(int)(225);//intwidth=(int)(m_width/26.45);// intheight=(int)(m_height/26.45); CRectrect1,rect2; GetClientRect(rect1); GetWindowRect(rect2); intx,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,也就是将整个图变为明显的黑白效果。将256个亮度等级的灰度图像通过适当的阀值选取而获得仍然可以反映图像整体和局部特征的二值化图像。所有灰度大于或者等于阀值的像素被判定为属于特定物体,其灰度值为255,否则这些像素点被排除在物体区域外,灰度值为0,拜师背景或者例外的物体区域。下面是代码:voidCText1Dlg::Onwhiteandblack()//彩色图片二值化{ //TODO:Addyourcontrolnotificationhandlercodehere UpdateData(); if(inum==1) { m_sourcefile=CStimes;//m_sourcefile是所要处理的彩色图片的路径及全名 m_targetfile="white.bmp";//m_targetfile是处理过的图片的路径及全名 } m_sourcefile=CStimes; m_targetfile="white.bmp"; if(m_sourcefile==""||m_targetfile=="") return; FILE*sourcefile,*targetfile; //位图文件头和信息头 BITMAPFILEHEADERsourcefileheader,targetfileheader; BITMAPINFOHEADERsourceinfoheader,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; //构造灰度图的调色版 RGBQUADrgbquad[256];// color=(RGBMixPlate*)malloc(sizeof(RGBMixPlate)*256);// index=(BYTE*)malloc(sizeof(BYTE)*infohead.readSize); inti,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); BYTEBYcolor[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; if( targetbuf[(i*m)+j]>100)///阀值m_scrollbar.GetScrollPos() targetbuf[(i*m)+j]=255; else targetbuf[(i*m)+j]=0; } } fwrite((void*)targetbuf,1,m*n+1,targetfile); fclose(targetfile); preview(IDC_pic,"white.bmp");//二值预览 }直方图:直方图(Histogram)又称柱状图、质量分布图。是一种统计报告图,由一系列高度不等的纵向条纹或线段表示数据分布的情况。一般用横轴表示数据类型,纵轴表示分布情况。下面介绍直方图的简单绘制代码。voidCHistogram::OnRed(){ //TODO:Addyourcontrolnotificationhandlercodehere GetDlgItem(IDC_STATIC1)->ShowWindow(SW_SHOW); GetDlgItem(IDC_STATIC2)->ShowWindow(SW_HIDE); GetDlgItem(IDC_STATIC3)->ShowWindow(SW_HIDE); GetDlgItem(IDC_STATIC4)->ShowWindow(SW_HIDE); GetDlgItem(IDC_STATIC5)->ShowWindow(SW_HIDE); }//我在对话框上添加了5个piture框架类控件,ID分别是GetDlgItem()里面的。我只写一个,其他的差不多,改一下所要显示的控件ID就成了。voidCHistogram::coordinate_axis()//画坐标轴{ CWnd*pi=GetDlgItem(IDC_STATIC6);//---注意:GetDlgItem是Cwnd类中函数,在此格式是继承; CDC*dc=pi->GetDC(); dc->MoveTo(0,255); dc->LineTo(0,0);//画纵轴 dc->MoveTo(0,0); dc->LineTo(5,5); dc->MoveTo(0,0); dc->LineTo(-5,5);///纵轴箭头 dc->MoveTo(0,255); dc->LineTo(270,255);//横轴 dc->MoveTo(270,255); dc->LineTo(265,250); dc->MoveTo(270,255); dc->LineTo(265,260);//横轴箭头 inti,j; for(i=11;i<255;) { dc->MoveTo(i,255); dc->LineTo(i,258); i+=10; }//设置横轴步长,10 dc->MoveTo(256,255); dc->LineTo(256,258); for(i=245;i>0;) { dc->MoveTo(0,i); dc->LineTo(-3,i); i-=10; }//设置纵轴步长 pi->ReleaseDC(dc);}voidCHistogram::red()//红色直方图{ CWnd*pi=GetDlgItem(IDC_STATIC1);//---注意:GetDlgItem是Cwnd类中函数,在此格式是继承; CDC*dc=pi->GetDC(); CPenMyPen,*OldPen; MyPen.CreatePen(PS_SOLID,1,RGB(0,0,0)); OldPen=dc->SelectObject(&MyPen); inti,j; for(i=0,j=1;i<=255;) { dc->MoveTo(j,255); dc->
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026届小升初语文期末冲刺专项卷含答案
- 肺癌病人围手术期护理
- 氧化铝制取工岗前工作规范考核试卷含答案
- 排土犁司机岗前任职考核试卷含答案
- 2026年显微超声技术应用于现代根管治疗术的护理配合
- 梳理缝编非织造布制作工安全知识竞赛强化考核试卷含答案
- 畜禽屠宰无害化处理工岗位安全实操考核试卷含答案
- 海洋勘探震源操作工岗位价值创造考核试卷含答案
- 公关员操作规程知识考核试卷含答案
- 炼焦备煤工操作水平测试考核试卷含答案
- 江苏无锡市2026-2027学年高三上学期期中考试语文试题
- 高压配电柜安全操作规程
- 老年人抗菌药物的使用与注意事项
- 2026年护士条例学习解读课件(含案例分析)
- 2026年湖南普通高中学业水平选择性考试化学真题及答案
- 露天矿山开采施工方案
- 钠离子电池用硬炭负极材料技术要求
- 驾驶服务外包投标方案
- 2026年兰州市红古区事业单位招聘笔试参考题库及答案解析
- 钢结构冬季施工方案
- 一年级家长会课件2024-2025学年
评论
0/150
提交评论