完整版图像处理实验.docx

上传人:b****1 文档编号:11098789 上传时间:2023-05-29 格式:DOCX 页数:28 大小:667.23KB
下载 相关 举报
完整版图像处理实验.docx_第1页
第1页 / 共28页
完整版图像处理实验.docx_第2页
第2页 / 共28页
完整版图像处理实验.docx_第3页
第3页 / 共28页
完整版图像处理实验.docx_第4页
第4页 / 共28页
完整版图像处理实验.docx_第5页
第5页 / 共28页
完整版图像处理实验.docx_第6页
第6页 / 共28页
完整版图像处理实验.docx_第7页
第7页 / 共28页
完整版图像处理实验.docx_第8页
第8页 / 共28页
完整版图像处理实验.docx_第9页
第9页 / 共28页
完整版图像处理实验.docx_第10页
第10页 / 共28页
完整版图像处理实验.docx_第11页
第11页 / 共28页
完整版图像处理实验.docx_第12页
第12页 / 共28页
完整版图像处理实验.docx_第13页
第13页 / 共28页
完整版图像处理实验.docx_第14页
第14页 / 共28页
完整版图像处理实验.docx_第15页
第15页 / 共28页
完整版图像处理实验.docx_第16页
第16页 / 共28页
完整版图像处理实验.docx_第17页
第17页 / 共28页
完整版图像处理实验.docx_第18页
第18页 / 共28页
完整版图像处理实验.docx_第19页
第19页 / 共28页
完整版图像处理实验.docx_第20页
第20页 / 共28页
亲,该文档总共28页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

完整版图像处理实验.docx

《完整版图像处理实验.docx》由会员分享,可在线阅读,更多相关《完整版图像处理实验.docx(28页珍藏版)》请在冰点文库上搜索。

完整版图像处理实验.docx

完整版图像处理实验

图像处理实验部分

打开图像

1.创建MFCApplication工程:

文件名称:

MyImage

2.添加cdib.h和cdib.cpp到工程项目中(也可以在工程项目中创建这两个文件,需要编写程序)

3.在MylmageDoc.h文件中增加如下语句

public:

CDib*m_pDib;(此类中增加成员变量表示Cdib对象的指针)

4.在MylmageDoc.cpp文件中的构造函数中添加:

m_pDib=newCDib;

在析构函数中添加

deletem_pDib;

5.在MylmageDoc.h文件中添加#include"cdib.h"

6.在Doc.cpp中CMylmageDoc:

:

Serialize(CArchive&ar)函数中添加:

m_pDib->Serialize(ar);

用CDib的串行化来实现CDisplayBmpImDoc文档的串行化。

7.在CMylmageView.h中添加变量声明

public:

intlWidth;CPointStartPoint;

intlHeight;CPointEndPoint;

longintlLineBytes;boolm_bTwoValue;

8.CMylmageView.cpp文件中的OnDraw()函数中添加代码

voidCMylmageView:

:

OnDraw(CDC*pDC)

{

CSizesize;

intlHeight,lWidth;

if(pDoc->m_pDib->m_lpBMlH!

=NULL)

{

lWidth=pDoc->m_pDib->m_lpBMlH->biWidth;lHeight=pDoc->m_pDib->m_lpBMlH->biHeight;size.cx=lWidth;

size.cy=lHeight;pDoc->m_pDib->Draw(pDC,CPoint(0,0),size);

}

}

运行,即可打开一幅BMP格式的图像

、灰度化与直方图

O144哺7旳84打阴旳.bmp傑Wort2

图1初始图片

图2灰度化图片(作为原图片使用)

图3灰度化图片直方图

 

灰度化图片直方图生成流程图

、图像滤波

(一)均值滤波(3*3模板)

基本思想:

基本原理是用均值代替原图像中的各个像素值,即对待处理的当前像素

点(x,y),选择一个模板,该模板由其近邻的若干像素组成,求模板中所有像素的均值,再把该均值赋予当前像素点(x,y),作为处理后图像在该

点上的灰度值g(x,y)。

步骤如下:

1.在资源菜单menu中HistModify中添加菜单按钮MeanFilter

2.ID名称可以随意修改,也可以默认。

3.中CMyImage点击右键,选择属性

在下拉菜单中找到ID」MGSMOOTHMEANFILTER,添加代码

4.在MylmageView.cpp中出现

voidCMylmageView:

:

OnlmgsmoothMeanfilter()函数

5.在其中添加代码

voidCMyImageView:

:

OnlmgsmoothMeanfilter(){

CMyImageDoc*pDoc=GetDocument();

intIHeight=pDoc->m_pDib->mi_lpBMIH->biHeight;

intIWidth=pDoc->m_pDib->m」pBMIH->biWidth;

intILineBytes=(IWidth+3)/4*4;

unsignedchar*IpSrc;

unsignedchar*IpDst;

unsignedchar*lpNewDib;

lpNewDib=newunsignedchar[lLineBytes*lHeight];

memcpy(lpNewDib,pDoc->m_pDib->m_lpImage,lLineBytes*lHeight);

for(inti=0;i

{

lpSrc=pDoc->m_pDib->m_lpImage+lLineBytes*(lHeight-1-i);

lpDst=lpNewDib+lLineBytes*(lHeight-1-i);

for(intj=0;j

{

if(i>0&&i0&&j

{

*lpDst=(*(lpSrc)+*(lpSrc+1)+*(lpSrc-1)+*(lpSrc+lLineBytes)+*(lpSrc-lLineBytes)+*(lpSrc+lLineBytes+1)+*(lpSrc-lLineBytes+1)+*(lpSrc+lLineBytes-1)+*(lpSrc-lLineBytes-1))/9;

}

lpSrc++;lpDst++;

}

}

memcpy(pDoc->m_pDib->m_lpImage,lpNewDib,lLineBytes*lHeight);

Invalidate(true);

MessageBox(L"均值滤波");

}

(2)中值滤波(3*3模板)

基本思想:

中值滤波的基本原理是把数字图像或数字序列中一点的值用该点的一个邻域中各点值的中值代替,让周围的像素值接近的真实值,从而消除孤立的噪声点。

方法是去某种结构的二维滑动模板,将板内像素按照像素值的大小进行排序,生成单调上升的二维数据序列,并取出序列中位于中间位置的灰度作为中心像素的灰度。

步骤如下:

1.在资源菜单menu中HistModify中添加菜单按钮MedianFilter

2.中CMyImage点击右键,选择属性

在下拉菜单中找到ID_IMGSMOOTHMEDIANFILTER,添加代码

3.在MyImageView.cpp中出现

voidCMyImageView:

:

OnImgsmoothMedianfilter()函数

4.在其中添加代码

voidCMyImageView:

:

OnImgsmoothMedianfilter()

{

CMyImageDoc*pDoc=GetDocument();

intlHeight=pDoc->m_pDib->m_lpBMIH->biHeight;

intlWidth=pDoc->m_pDib->m_lpBMIH->biWidth;

intlLineBytes=(lWidth+3)/4*4;

unsignedchar*lpSrc;

unsignedchar*lpDst;

unsignedchar*lpNewDib;lpNewDib=newunsignedchar[lLineBytes*lHeight];memcpy(lpNewDib,pDoc->m_pDib->m_lpImage,lLineBytes*lHeight);

for(inti=0;i

{

lpSrc=pDoc->m_pDib->m_lpImage+lLineBytes*(lHeight-1-i);

lpDst=lpNewDib+lLineBytes*(lHeight-1-i);

for(intj=0;j

{

if(i>0&&i0&&j

{

inta[9]={0};intm,n,temp;a[0]=*(lpSrc);a[1]=*(lpSrc+1);a[2]=*(lpSrc+lLineBytes);

a[3]=*(lpSrc-lLineBytes);a[4]=*(lpSrc+lLineBytes+1);a[5]=*(lpSrc-lLineBytes+1);

a[6]=*(lpSrc-1);a[7]=*(lpSrc+lLineBytes-1);a[8]=*(lpSrc-lLineBytes-1);

for(n=0;n<9;n++)

{

for(m=n+1;m<9;m++)

if(a[n]

{

temp=a[n];a[n]=a[m];a[m]=temp;

}

}

*lpDst=a[4];

}

lpSrc++;lpDst++;

}

}

memcpy(pDoc->m_pDib->m_lpImage,lpNewDib,lLineBytes*lHeight);

Invalidate(true);MessageBox(L"中值滤波");

}

(3)均值滤波(5*5模板)

1.在资源菜单menu中HistModify中添加菜单按钮MedianFilter52.中CMyImage点击右键,选择属性

在下拉菜单中找到ID_IMGSMOOTHMEDIANFILTER5,添加代码

3.在MyImageView.cpp中出现

voidCMyImageView:

:

OnImgsmoothMedianfilter5()函数

4.在其中添加代码

voidCMyImageView:

:

OnImgsmoothMeanfilter5()

{

for(inti=0;i

{

lpSrc=pDoc->m_pDib->m_lpImage+lLineBytes*(lHeight-1-i);

lpDst=lpNewDib+lLineBytes*(lHeight-1-i);

for(intj=0;j

{

if(i>0&&i0&&j

{

*lpDst=(*(lpSrc)+*(lpSrc+1)+*(lpSrc-1)+

*(lpSrc+lLineBytes)+*(lpSrc-lLineBytes)+

*(lpSrc+lLineBytes+1)+*(lpSrc-lLineBytes+1)+

*(lpSrc+lLineBytes-1)+*(lpSrc-lLineBytes-1)+

*(lpSrc-2*lLineBytes)+*(lpSrc+2*lLineBytes)+

*(lpSrc-2*lLineBytes-1)+

*(lpSrc-2*lLineBytes-2)+*(lpSrc-2*lLineBytes+1)+

*(lpSrc-2*lLineBytes+2)+

*(lpSrc+2*lLineBytes-1)+*(lpSrc+2*lLineBytes-2)+

*(lpSrc+2*lLineBytes+1)+

*(lpSrc+2*lLineBytes+2)+*(lpSrc+2)+*(lpSrc-2)+

*(lpSrc+lLineBytes+2)+*(lpSrc-lLineBytes+2)+

*(lpSrc+lLineBytes-2)+*(lpSrc-lLineBytes-2))/15;

}

lpSrc++;lpDst++;

}

}

memcpy(pDoc->m_pDib->m_lpImage,lpNewDib,lLineBytes*lHeight);

Invalidate(true);

MessageBox(L"均值滤波5*5");

}

(4)中值滤波(5*5模板)

1.在资源菜单menu中HistModify中添加菜单按钮MedianFilter5

2.中CMyImage点击右键,选择属性

在下拉菜单中找到ID_IMGSMOOTHMEDIANFILTER5,添加代码

3.在MyImageView.cpp中出现

voidCMyImageView:

:

OnImgsmoothMedianfilter()函数

4.在其中添加代码

voidCMyImageView:

:

OnImgsmoothMedianfilter5()

{

for(inti=0;i

{

lpSrc=pDoc->m_pDib->m_lpImage+lLineBytes*(lHeight-1-i);

lpDst=lpNewDib+lLineBytes*(lHeight-1-i);

for(intj=0;j

{

if(i>0&&i0&&j

{

inta[25]={0};intm,n,temp;

a[0]=*(lpSrc);a[1]=*(lpSrc+1);a[2]=*(lpSrc+lLineBytes);

a[3]=*(lpSrc-lLineBytes);a[4]=*(lpSrc+lLineBytes+1);a[5]=*(lpSrc-lLineBytes+1);a[6]=*(lpSrc-1);a[7]=*(lpSrc+lLineBytes-1);a[8]=*(lpSrc-lLineBytes-1);

a[9]=*(lpSrc-2*lLineBytes);a[10]=*(lpSrc+2*lLineBytes);

a[11]=*(lpSrc-2*lLineBytes-1);a[12]=*(lpSrc-2*lLineBytes-2);

a[13]=*(lpSrc-2*lLineBytes+1);a[14]=*(lpSrc-2*lLineBytes+2);a[15]=*(lpSrc+2*lLineBytes-1);a[16]=*(lpSrc+2*lLineBytes-2);

a[17]=*(lpSrc+2*lLineBytes+1);a[18]=*(lpSrc+2*lLineBytes+2);

a[19]=*(lpSrc+2);a[20]=*(lpSrc-2);a[21]=*(lpSrc+lLineBytes+2);a[22]=*(lpSrc-lLineBytes+2);a[23]=*(lpSrc+lLineBytes-2);a[24]=*(lpSrc-lLineBytes-2);

for(n=0;n<25;n++)

{

for(m=n+1;m<25;m++)

if(a[n]

{

temp=a[n];

a[n]=a[m];

a[m]=temp;

}

}

*lpDst=a[12];

}

lpSrc++;lpDst++;

}

}

memcpy(pDoc->m_pDib->m_lpImage,lpNewDib,lLineBytes*lHeight);

Invalidate(true);

MessageBox(L"中值滤波");

}

(5)运行结果比较和分析

结果分析:

这个取均值的模板其实是一个低通滤波器。

因图像细节信息主要分布在高频区域,因此均值滤波的过程会导致图像变模糊。

如果模板选取过大,则这种模糊会加剧;模板选择越小,去噪能力会下降。

即3*3模板去噪能力没有5*5模板强,但5*5模板的处理室图像更模糊,如图1,2,3,4,5所示。

用3*3的模板和5*5处理图像,结果不一样。

5*5的模板产生的图形边缘更模糊,亮点更少。

同时使用中值滤波算法,可以在保护图像边缘同时去除处噪声,而均值滤波不能对图像边缘保护,如图1,2,3,4,5所示。

对于椒盐噪声,中值滤波能在去除噪声的同时较好的保持图像边缘,而均值滤波效果不佳。

如图6,7,8所示。

原图像均值与中值滤波流程图

第一疫曲

倉,対■素;K會!

图23*3均值滤波

图33*3中值滤波

1

fl

1

图1原图像

图45*5均值滤波

图55*5中值滤波

 

 

二、边缘检测

(1)梯度算子(以Priwitt为例)

基本思想:

利用梯度幅值在边缘处达到极值检测边缘。

该法不受施加运算方向限制,同

时能获得边缘方向信息,定位精度高,但对噪声较为敏感。

主要代码:

voidCMylmageView:

:

OnEdgePriwitt()

{

CMylmageDoc*pDoc=GetDocument();

intIHeight=pDoc->m_pDib->m_lpBMIH->biHeight;

intIWidth=pDoc->m_pDib->m_lpBMIH->biWidth;

intILineBytes=(IWidth+3)/4*4;

unsignedchar*lpSrc;unsignedchar*lpDst;

unsignedchar*lpNewDib;

lpNewDib=newunsignedchar[ILineBytes*IHeight];

memcpy(lpNewDib,pDoc->m_pDib->m_lplmage,ILineBytes*IHeight);

for(inti=0;i

{

IpSrc=pDoc->m_pDib->m_lplmage+ILineBytes*(IHeight-1-i);

IpDst=IpNewDib+ILineBytes*(IHeight-1-i);

for(intj=0;j

{

if(i>0&&i0&&j

{

inttmp1=*(lpSrc+ILineBytes-1)+1**(lpSrc-1)+*(lpSrc-ILineBytes-1)-*(lpSrc+ILineBytes+1)-1**(lpSrc+1)-*(lpSrc-ILineBytes+1);

inttmp2=*(lpSrc+ILineBytes-1)+1**(lpSrc+ILineBytes)+*(lpSrc+ILineBytes+1)-*(lpSrc-ILineBytes-1)-1**(lpSrc-ILineBytes)-*(lpSrc-ILineBytes+1);

*IpDst=min((*IpSrc+(unsigned

char)(min(sqrt(float(tmp1*tmp1+tmp2*tmp2)),255))/2),255);

}lpSrc++;lpDst++;

}

}memcpy(pDoc->m_pDib->m_lpImage,lpNewDib,lLineBytes*lHeight);

Invalidate(true);

MessageBox(L"Prewitt边缘提取");

}

(2)Sobel算子

基本思想:

Sobel算子根据像素点上下、左右邻点灰度加权差,在边缘处达到极值这一现象检测边缘。

对噪声具有平滑作用,提供较为精确的边缘方向信息,边缘定位精度不够高。

当对精度要求不是很高时,是一种较为常用的边缘检测方法。

主要代码:

voidCMyImageView:

:

OnEdgeSobel()

{

CMyImageDoc*pDoc=GetDocument();unsignedchar*lpSrc;unsignedchar*lpDst;

unsignedchar*lpNewDIBBits;lpNewDIBBits=newunsignedchar[lLineBytes*lHeight];

if(lpNewDIBBits==NULL)

{

return;

}

BeginWaitCursor();

lpSrc=(unsignedchar*)pDoc->m_pDib->m_lpImage;

memcpy(lpNewDIBBits,lpSrc,lLineBytes*lHeight);

intpixel[8];

for(inti=0;i<=lHeight;i++)

{

lpDst=(unsignedchar*)pDoc->m_pDib->m_lpImage+lLineBytes*(lHeight-1-i)-1;

lpSrc=lpNewDIBBits+lLineBytes*(lHeight-1-i)-1;

for(intj=0;j

{

lpSrc++;lpDst++;

if(i>0&&i0&&j

pixel[0]=(

int

)*(lpSrc+lLineBytes-1);

pixel[1]=(

int

)*(lpSrc+lLineBytes);

pixel[2]=(

int

)*(lpSrc+lLineBytes+1);

pixel[3]=(

int

)*(lpSrc-1);

pixel[4]=(

int

)*(lpSrc+1);

pixel[5]=(int)*(lpSrc-lLineBytes-1);pixel[6]=(int)*(lpSrc-lLineBytes);

pixel[7]=(int)*(lpSrc-lLineBytes+1);int

tmp1=pixel[0]+2*pixel[1]+pixel[2]-pixel[5]-2*pixel[6]-pixel[7];

inttmp2=pixel[2]+2*pixel[4]+pixel[7]-pixel[0]-2*pixel[3]-pixel[5];

intresult

=(int)(sqrt(double(tmp1*tmp1+tmp2*tmp2))/2+0.5);//>>255

if(result>255)result=255;

*lpDst=(unsignedchar)result;*lpDst&=0xfc;

if(tmp2==0)tmp2=1;doubletha=atan((double(tmp1))/tmp2);

if(abs(tha)

else

if(tha>Pi/8&&tha

*lpDst=*lpDst+1;

else

if(tha<-Pi/8&&tha>-Pi*3/8)*lpDst=*lpDst+3;

else*lpDst=*lpDst+2;

}else*lpDst=0;

}

}

CStringstr=pDoc->GetTitle();pDoc->SetTitle(str+L"--Sobel");

InvalidateRect(CRect(0,0,lHeight,lWidth),true);EndWaitCursor();

delete[]lpNewDIBBits;

Messag

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2