Sobel算子边缘检测(vc实现)

来源:互联网 发布:新浪邮箱端口号 编辑:程序博客网 时间:2024/05/23 00:07


void CISLSView::OnSobel(){//程序编制:李立宗  lilizong@gmail.com//2012-8-12if(myImage1.IsNull())OnOpenResourceFile();if(!myImage2.IsNull())myImage2.Destroy();if(myImage2.IsNull()){myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);}float tempArray[9]={-1,-2,-1,0,0,0,1,2,1};DoTemplate(&myImage1,&myImage2,3,3,1, 1, tempArray, 3);float tempArray1[9]={1,0,-1,2,0,-2,1,0,-1};DoTemplate(&myImage1,&myImage3,3,3,1, 1, tempArray1, 3);getMaxValue(&myImage2,&myImage3);Invalidate();}void CISLSView::DoTemplate(CImage *myImage1,CImage *myImage2,int tempX, int tempY, int tempCenterX, int tempCenterY, float *tempArray, float tempCoef){/*CString str;str.Format(TEXT("%f"),tempArray[2]);MessageBox(str);*/if((*myImage1).IsNull())OnOpenResourceFile();if(!(*myImage2).IsNull())(*myImage2).Destroy();if((*myImage2).IsNull()){(*myImage2).Create((*myImage1).GetWidth(),(*myImage1).GetHeight(),24,0);}//COLORREF pixel; int maxY = (*myImage1).GetHeight();int maxX=(*myImage1).GetWidth();byte* pRealData;byte* pRealData2;pRealData=(byte*)(*myImage1).GetBits();pRealData2=(byte*)(*myImage2).GetBits();int pit=(*myImage1).GetPitch();int pit2=(*myImage2).GetPitch();//需要注意,pit和pit2的值并不一样,所以如果使用一个值,会导致不同的结果出现//CString str;//str.Format(TEXT("%d"),pit);//MessageBox(str);//str.Format(TEXT("%d"),pit2);//MessageBox(str);int bitCount=(*myImage1).GetBPP()/8;int bitCount2=(*myImage2).GetBPP()/8;int tempR=255,tempG=255,tempB=0;float temp;//tempR=tempG=tempG=0;//说明:将生产的图像作为24位图处理。for (int y=tempY; y<maxY-tempY; y++) {for (int x=tempX; x<maxX-tempX; x++) {temp=0;for(int i=0;i<tempX;i++)for(int j=0;j<tempY;j++)temp=temp+(*(pRealData+pit*(y-tempCenterY+i)+(x-tempCenterX+j)*bitCount))*tempArray[i*tempX+j];temp=tempCoef*temp;temp=fabs(temp);if(temp>255)tempR=255;elsetempR=(int)(temp+0.5);if(bitCount==1){tempG=tempR;tempB=tempR;}else{temp=0;for(int i=0;i<tempX;i++)for(int j=0;j<tempY;j++)temp=temp+(*(pRealData+pit*(y-tempCenterY+i)+(x-tempCenterX+j)*bitCount+1))*tempArray[i*tempX+j];temp=tempCoef*temp;temp=fabs(temp);if(temp>255)tempG=255;elsetempG=(int)(temp+0.5);temp=0;for(int i=0;i<tempX;i++)for(int j=0;j<tempY;j++)temp=temp+(*(pRealData+pit*(y-tempCenterY+i)+(x-tempCenterX+j)*bitCount+2))*tempArray[i*tempX+j];temp=tempCoef*temp;temp=fabs(temp);if(temp>255)tempB=255;elsetempB=(int)(temp+0.5);}*(pRealData2+pit2*y+x*bitCount2)=tempR;*(pRealData2+pit2*y+x*bitCount2+1)=tempG;*(pRealData2+pit2*y+x*bitCount2+2)=tempB;}}//Invalidate();}void CISLSView::getMaxValue(CImage *myImage1, CImage *myImage2){//if((*myImage1).IsNull())//OnOpenResourceFile();//if(!(*myImage2).IsNull())//(*myImage2).Destroy();//if((*myImage2).IsNull()){//(*myImage2).Create((*myImage1).GetWidth(),(*myImage1).GetHeight(),24,0);//}//COLORREF pixel; int maxY = (*myImage1).GetHeight();int maxX=(*myImage1).GetWidth();byte* pRealData;byte* pRealData2;pRealData=(byte*)(*myImage1).GetBits();pRealData2=(byte*)(*myImage2).GetBits();int pit=(*myImage1).GetPitch();int pit2=(*myImage2).GetPitch();//需要注意,pit和pit2的值并不一样,所以如果使用一个值,会导致不同的结果出现//CString str;//str.Format(TEXT("%d"),pit);//MessageBox(str);//str.Format(TEXT("%d"),pit2);//MessageBox(str);int bitCount=(*myImage1).GetBPP()/8;int bitCount2=(*myImage2).GetBPP()/8;int tempR=255,tempG=255,tempB=0;int tempR1,tempR2,tempG1,tempG2,tempB1,tempB2;float temp;//tempR=tempG=tempG=0;//说明:将生产的图像作为24位图处理。for (int y=0; y<maxY; y++) {for (int x=0; x<maxX; x++) {tempR1=*(pRealData+pit*(y)+(x)*bitCount);tempR2=*(pRealData2+pit2*y+x*bitCount2);if(tempR1<tempR2)tempR1=tempR2;if(bitCount==1){tempG=tempR;tempB=tempR;}else{tempG1=*(pRealData+pit*(y)+(x)*bitCount+1);tempG2=*(pRealData2+pit2*y+x*bitCount2+1);if(tempG1<tempG2)tempG1=tempG2;tempB1=*(pRealData+pit*(y)+(x)*bitCount+2);tempB2=*(pRealData2+pit2*y+x*bitCount2+2);if(tempB1<tempB2)tempB1=tempB2;}*(pRealData+pit*(y)+(x)*bitCount)=tempR1;*(pRealData+pit*(y)+(x)*bitCount+1)=tempG1;*(pRealData+pit*(y)+(x)*bitCount+2)=tempB1;}}}


原创粉丝点击