- void CISLSView::OnEdgeSengOutlineTract()
- {
- //程序编制:李立宗 lilizong@gmail.com
- //2012-8-14
- if(myImage1.IsNull())
- OnOpenResourceFile();
- if(!myImage2.IsNull())
- myImage2.Destroy();
- if(myImage2.IsNull()){
- myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
- }
- if(myImage3.IsNull()){
- myImage3.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
- }
- //COLORREF pixel;
- int maxY = myImage1.GetHeight();
- int maxX=myImage1.GetWidth();
- byte* pRealData;
- byte* pRealData2;
- byte* pRealData3;
- pRealData=(byte*)myImage1.GetBits();
- pRealData2=(byte*)myImage2.GetBits();
- pRealData3=(byte*)myImage3.GetBits();
- int pit=myImage1.GetPitch();
- int pit2=myImage2.GetPitch();
- int pit3=myImage3.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 bitCount3=myImage3.GetBPP()/8;
- int tempR,tempG,tempB;
- //float temp,tempX,tempY;
- int temp;
- int T=128;
- //int pixel[4];
- float u0,u1; //均值
- float w0,w1; //概率
- float sum0,sum1; //像素和
- int optIndex,optT; //最优阈值,及其所在像素的值
- float fVaria,fMaxVaria=0; //临时方差,最大方差
- //int pixelR[256],pixelG[256],pixelB[256];
- int pixel[256]={0}; //不要忘记初始化
- //灰度化
- for (int y=0; y<maxY; y++) {
- for (int x=0; x<maxX; x++) {
- temp=*(pRealData+pit*(y)+(x)*bitCount);
- if(bitCount==3)
- {
- tempR=*(pRealData+pit*(y)+(x)*bitCount);
- tempG=*(pRealData+pit*(y)+(x)*bitCount+1);
- tempB=*(pRealData+pit*(y)+(x)*bitCount+2);
- temp=(int)(tempR*0.49+tempG*0.31+tempB*0.2);
- //temp=(int)((tempR+tempG+tempB)/3);
- }
- *(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;
- }
- }
- //二值化
- for (int y=0; y<maxY; y++) {
- for (int x=0; x<maxX; x++) {
- temp=*(pRealData3+pit3*(y)+(x)*bitCount3);
- if(temp>128)
- temp=255;
- else
- temp=0;
- *(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;
- }
- }
- int grayindex;
- int i,j;
- BOOL FindStartPoint;
- BOOL FindPoint;
- CPoint StartPoint,CurrentPoint;
- int Direction[8][2]={{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0}};
- int BeginDirect;
- for(int y=0;y<maxY;y++)
- {
- for(int x=0;x<maxX;x++)
- {
- *(pRealData2+pit2*(y)+(x)*bitCount2)=255;
- *(pRealData2+pit2*(y)+(x)*bitCount2+1)=255;
- *(pRealData2+pit2*(y)+(x)*bitCount2+2)=255;
- }
- }
- FindStartPoint=FALSE;
- for(int y=0;y<maxY && !FindStartPoint;y++)
- {
- for(int x=0;x<maxX && !FindStartPoint;x++)
- {
- if(*(pRealData3+pit3*(y)+(x)*bitCount3)==0)
- {
- FindStartPoint=TRUE;
- StartPoint.y=y;
- StartPoint.x=x;
- *(pRealData2+pit2*(y)+(x)*bitCount2)=0;
- *(pRealData2+pit2*(y)+(x)*bitCount2+1)=0;
- *(pRealData2+pit2*(y)+(x)*bitCount2+2)=0;
- }
- }
- }
- BeginDirect=0;
- FindStartPoint=FALSE;
- CurrentPoint.y=StartPoint.y;
- CurrentPoint.x=StartPoint.x;
- while(!FindStartPoint)
- {
- FindPoint=FALSE;
- while(!FindPoint)
- {
- byte *pc=pRealData3 + pit3*(CurrentPoint.y+Direction[BeginDirect][1]) +bitCount3*(CurrentPoint.x+Direction[BeginDirect][0]);
- grayindex=pc[0];
- if(grayindex==0)
- {
- FindPoint=TRUE;
- CurrentPoint.y=CurrentPoint.y+Direction[BeginDirect][1];
- CurrentPoint.x=CurrentPoint.x+Direction[BeginDirect][0];
- if(CurrentPoint.y==StartPoint.y && CurrentPoint.x==StartPoint.x)
- {
- FindStartPoint=TRUE;
- }
- else
- {
- *(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2)=0;
- *(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2+1)=0;
- *(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2+2)=0;
- BeginDirect -= 2;
- if(BeginDirect<0)
- {
- BeginDirect += 8;
- }
- }
- }
- else
- {
- BeginDirect++;
- if(BeginDirect==8)
- {
- BeginDirect=0;
- }
- }
- }
- }
- Invalidate();
- }
void CISLSView::OnEdgeSengOutlineTract()
{
//程序编制:李立宗 lilizong@gmail.com
//2012-8-14
if(myImage1.IsNull())
OnOpenResourceFile();
if(!myImage2.IsNull())
myImage2.Destroy();
if(myImage2.IsNull()){
myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
}
if(myImage3.IsNull()){
myImage3.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
}
//COLORREF pixel;
int maxY = myImage1.GetHeight();
int maxX=myImage1.GetWidth();
byte* pRealData;
byte* pRealData2;
byte* pRealData3;
pRealData=(byte*)myImage1.GetBits();
pRealData2=(byte*)myImage2.GetBits();
pRealData3=(byte*)myImage3.GetBits();
int pit=myImage1.GetPitch();
int pit2=myImage2.GetPitch();
int pit3=myImage3.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 bitCount3=myImage3.GetBPP()/8;
int tempR,tempG,tempB;
//float temp,tempX,tempY;
int temp;
int T=128;
//int pixel[4];
float u0,u1; //均值
float w0,w1; //概率
float sum0,sum1; //像素和
int optIndex,optT; //最优阈值,及其所在像素的值
float fVaria,fMaxVaria=0; //临时方差,最大方差
//int pixelR[256],pixelG[256],pixelB[256];
int pixel[256]={0}; //不要忘记初始化
//灰度化
for (int y=0; y<maxY; y++) {
for (int x=0; x<maxX; x++) {
temp=*(pRealData+pit*(y)+(x)*bitCount);
if(bitCount==3)
{
tempR=*(pRealData+pit*(y)+(x)*bitCount);
tempG=*(pRealData+pit*(y)+(x)*bitCount+1);
tempB=*(pRealData+pit*(y)+(x)*bitCount+2);
temp=(int)(tempR*0.49+tempG*0.31+tempB*0.2);
//temp=(int)((tempR+tempG+tempB)/3);
}
*(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
*(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
*(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;
}
}
//二值化
for (int y=0; y<maxY; y++) {
for (int x=0; x<maxX; x++) {
temp=*(pRealData3+pit3*(y)+(x)*bitCount3);
if(temp>128)
temp=255;
else
temp=0;
*(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
*(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
*(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;
}
}
int grayindex;
int i,j;
BOOL FindStartPoint;
BOOL FindPoint;
CPoint StartPoint,CurrentPoint;
int Direction[8][2]={{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0}};
int BeginDirect;
for(int y=0;y<maxY;y++)
{
for(int x=0;x<maxX;x++)
{
*(pRealData2+pit2*(y)+(x)*bitCount2)=255;
*(pRealData2+pit2*(y)+(x)*bitCount2+1)=255;
*(pRealData2+pit2*(y)+(x)*bitCount2+2)=255;
}
}
FindStartPoint=FALSE;
for(int y=0;y<maxY && !FindStartPoint;y++)
{
for(int x=0;x<maxX && !FindStartPoint;x++)
{
if(*(pRealData3+pit3*(y)+(x)*bitCount3)==0)
{
FindStartPoint=TRUE;
StartPoint.y=y;
StartPoint.x=x;
*(pRealData2+pit2*(y)+(x)*bitCount2)=0;
*(pRealData2+pit2*(y)+(x)*bitCount2+1)=0;
*(pRealData2+pit2*(y)+(x)*bitCount2+2)=0;
}
}
}
BeginDirect=0;
FindStartPoint=FALSE;
CurrentPoint.y=StartPoint.y;
CurrentPoint.x=StartPoint.x;
while(!FindStartPoint)
{
FindPoint=FALSE;
while(!FindPoint)
{
byte *pc=pRealData3 + pit3*(CurrentPoint.y+Direction[BeginDirect][1]) +bitCount3*(CurrentPoint.x+Direction[BeginDirect][0]);
grayindex=pc[0];
if(grayindex==0)
{
FindPoint=TRUE;
CurrentPoint.y=CurrentPoint.y+Direction[BeginDirect][1];
CurrentPoint.x=CurrentPoint.x+Direction[BeginDirect][0];
if(CurrentPoint.y==StartPoint.y && CurrentPoint.x==StartPoint.x)
{
FindStartPoint=TRUE;
}
else
{
*(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2)=0;
*(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2+1)=0;
*(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2+2)=0;
BeginDirect -= 2;
if(BeginDirect<0)
{
BeginDirect += 8;
}
}
}
else
{
BeginDirect++;
if(BeginDirect==8)
{
BeginDirect=0;
}
}
}
}
Invalidate();
}
修改:
因为上述函数遇到灰度图像,或者有在边缘的图像时会遇到溢出,所以,进行了处理修改,如下:
- void CISLSView::OnEdgeSengOutlineTract()
- {
- //程序编制:李立宗 lilizong@gmail.com
- //2012-8-15 00:05
- if(myImage1.IsNull())
- OnOpenResourceFile();
- if(!myImage2.IsNull())
- myImage2.Destroy();
- if(myImage2.IsNull()){
- myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
- }
- if(!myImage3.IsNull())
- myImage3.Destroy();
- if(myImage3.IsNull()){
- myImage3.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
- }
- //COLORREF pixel;
- int maxY = myImage1.GetHeight();
- int maxX=myImage1.GetWidth();
- byte* pRealData;
- byte* pRealData2;
- byte* pRealData3;
- pRealData=(byte*)myImage1.GetBits();
- pRealData2=(byte*)myImage2.GetBits();
- pRealData3=(byte*)myImage3.GetBits();
- int pit=myImage1.GetPitch();
- int pit2=myImage2.GetPitch();
- int pit3=myImage3.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 bitCount3=myImage3.GetBPP()/8;
- int tempR,tempG,tempB;
- //float temp,tempX,tempY;
- int temp;
- int T=128;
- //int pixel[4];
- float u0,u1; //均值
- float w0,w1; //概率
- float sum0,sum1; //像素和
- int optIndex,optT; //最优阈值,及其所在像素的值
- float fVaria,fMaxVaria=0; //临时方差,最大方差
- //int pixelR[256],pixelG[256],pixelB[256];
- int pixel[256]={0}; //不要忘记初始化
- //灰度化
- for (int y=0; y<maxY; y++) {
- for (int x=0; x<maxX; x++) {
- temp=*(pRealData+pit*(y)+(x)*bitCount);
- if(bitCount==3)
- {
- tempR=*(pRealData+pit*(y)+(x)*bitCount);
- tempG=*(pRealData+pit*(y)+(x)*bitCount+1);
- tempB=*(pRealData+pit*(y)+(x)*bitCount+2);
- temp=(int)(tempR*0.49+tempG*0.31+tempB*0.2);
- //temp=(int)((tempR+tempG+tempB)/3);
- }
- *(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;
- }
- }
- //二值化
- for (int y=0; y<maxY; y++) {
- for (int x=0; x<maxX; x++) {
- temp=*(pRealData3+pit3*(y)+(x)*bitCount3);
- if(temp>127)
- temp=255;
- else
- temp=0;
- /* temp=0;*/
- *(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;
- }
- }
- //修理边界,防止溢出,写在了后面
- /* for (int y=0; y<maxY; y++) {
- for (int x=0; x<=0; x++) {
- temp=255;
- *(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;
- }
- }
- for (int y=0; y<maxY; y++) {
- for (int x=maxX-1; x<maxX; x++) {
- temp=255;
- *(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;
- }
- }
- for (int y=0; y<1; y++) {
- for (int x=0; x<maxX; x++) {
- temp=255;
- *(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;
- }
- }
- for (int y=maxY-1; y<maxY; y++) {
- for (int x=0; x<maxX; x++) {
- temp=255;
- *(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
- *(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;
- }
- }
- */
- int grayindex;
- int i,j;
- BOOL FindStartPoint;
- BOOL FindPoint;
- CPoint StartPoint,CurrentPoint;
- int Direction[8][2]={{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0}};
- int BeginDirect;
- for(int y=0;y<maxY;y++)
- {
- for(int x=0;x<maxX;x++)
- {
- *(pRealData2+pit2*(y)+(x)*bitCount2)=255;
- *(pRealData2+pit2*(y)+(x)*bitCount2+1)=255;
- *(pRealData2+pit2*(y)+(x)*bitCount2+2)=255;
- }
- }
- FindStartPoint=FALSE;
- //为了防止溢出,将myImage3的起始点选取在边界以内。?????此思路不对,因为无法保证寻找其邻域仍在图像内不溢出
- //对图像进行修边处理,将其四边军处理为255.确保不会溢出。开始将终止点设定为maxX,maxY,无法运算出
- for(int y=0;y<maxY;y++)
- {
- *(pRealData3+pit3*(y)+(0)*bitCount3)=255;
- *(pRealData3+pit3*(y)+(maxX-1)*bitCount3)=255;
- }
- for(int x=0;x<maxX ;x++)
- {
- *(pRealData3+pit3*(0)+(x)*bitCount3)=255;
- *(pRealData3+pit3*(maxY-1)+(x)*bitCount3)=255;
- }
- for(int y=0;y<maxY && !FindStartPoint;y++)
- {
- for(int x=0;x<maxX && !FindStartPoint;x++)
- {
- if(*(pRealData3+pit3*(y)+(x)*bitCount3)==0)
- {
- FindStartPoint=TRUE;
- StartPoint.y=y;
- StartPoint.x=x;
- *(pRealData2+pit2*(y)+(x)*bitCount2)=0;
- *(pRealData2+pit2*(y)+(x)*bitCount2+1)=0;
- *(pRealData2+pit2*(y)+(x)*bitCount2+2)=0;
- }
- }
- }
- BeginDirect=0;
- FindStartPoint=FALSE;
- CurrentPoint.y=StartPoint.y;
- CurrentPoint.x=StartPoint.x;
- while(!FindStartPoint)
- {
- FindPoint=FALSE;
- while(!FindPoint)
- {
- grayindex=*(pRealData3 + pit3*(CurrentPoint.y+Direction[BeginDirect][1]) +bitCount3*(CurrentPoint.x+Direction[BeginDirect][0]));
- if(grayindex==0)
- {
- FindPoint=TRUE;
- CurrentPoint.y=CurrentPoint.y+Direction[BeginDirect][1];
- CurrentPoint.x=CurrentPoint.x+Direction[BeginDirect][0];
- if(CurrentPoint.y==StartPoint.y && CurrentPoint.x==StartPoint.x)
- {
- FindStartPoint=TRUE;
- }
- //else
- //{
- // *(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2)=0;
- // *(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2+1)=0;
- // *(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2+2)=0;
- // BeginDirect -= 2;
- // if(BeginDirect<0)
- // {
- // BeginDirect += 8;
- // }
- //}
- *(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2)=0;
- *(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2+1)=0;
- *(pRealData2+pit2*(CurrentPoint.y)+(CurrentPoint.x)*bitCount2+2)=0;
- BeginDirect -= 2;
- if(BeginDirect<0)
- {
- BeginDirect += 8;
- }
- }
- else
- {
- BeginDirect++;
- if(BeginDirect==8)
- {
- BeginDirect=0;
- }
- }
- }
- }
- Invalidate();
- }