void CISLSView::OnRoberts()
{
//程序编制:李立宗 lilizong@gmail.com
//2012-8-12
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,tempG,tempB;
float temp,tempX,tempY;
int pixel[4];
// tempR=tempG=tempG=0;
//说明:将生产的图像作为24位图处理。
for (int y=0; y<maxY-1; y++) {
for (int x=0; x<maxX-1; x++) {
pixel[0]=*(pRealData+pit*(y)+(x)*bitCount);
pixel[1]=*(pRealData+pit*(y)+(x+1)*bitCount);
pixel[2]=*(pRealData+pit*(y+1)+(x)*bitCount);
pixel[3]=*(pRealData+pit*(y+1)+(x+1)*bitCount);
tempR=(int)(sqrt((float)
(pixel[0]-pixel[3])*(pixel[0]-pixel[3])
+(pixel[1]-pixel[2])*(pixel[1]-pixel[2])
));
if(bitCount==1)
{
tempG=tempR;
tempB=tempR;
}
else
{
pixel[0]=*(pRealData+pit*(y)+(x)*bitCount+1);
pixel[1]=*(pRealData+pit*(y)+(x+1)*bitCount+1);
pixel[2]=*(pRealData+pit*(y+1)+(x)*bitCount+1);
pixel[3]=*(pRealData+pit*(y+1)+(x+1)*bitCount+1);
tempG=(int)(sqrt((float)
(pixel[0]-pixel[3])*(pixel[0]-pixel[3])
+(pixel[1]-pixel[2])*(pixel[1]-pixel[2])
));
pixel[0]=*(pRealData+pit*(y)+(x)*bitCount+2);
pixel[1]=*(pRealData+pit*(y)+(x+1)*bitCount+2);
pixel[2]=*(pRealData+pit*(y+1)+(x)*bitCount+2);
pixel[3]=*(pRealData+pit*(y+1)+(x+1)*bitCount+2);
tempB=(int)(sqrt((float)
(pixel[0]-pixel[3])*(pixel[0]-pixel[3])
+(pixel[1]-pixel[2])*(pixel[1]-pixel[2])
));
}
*(pRealData2+pit2*y+x*bitCount2)=tempR;
*(pRealData2+pit2*y+x*bitCount2+1)=tempG;
*(pRealData2+pit2*y+x*bitCount2+2)=tempB;
}
}
Invalidate();
}
Roberts算子边缘检测(vc实现)
最新推荐文章于 2023-11-12 14:51:14 发布