#include //CreateMask 関数 //原画像を元にマスクを作成します //mask :マスク画像 //MaskColor :背景として扱う色 //FaceColor :追跡対象として扱う色 //image :原画像 //iWidth :原画像の横幅 //iHeight :原画像の高さ //BackColor :原画像の背景色 //戻り値 :なし void CreateMask(LPBYTE mask,BYTE MaskColor,BYTE FaceColor, LPDWORD image,int iWidth,int iHeight,DWORD BackColor) { for(int y=0;y=iWidth*iHeight){ //lpPoints 配列は iWidth*iHeight 個の要素を持つ *lpPolyCounts=n; return -1; //無限ループに陥った可能性が高い } switch(vec){ //優先順位を付けてチェックする(闇雲に調べると無限ループに陥る可能性が高い) case 6: //右中が輪郭? from=((x+1)<0 || iWidth<=(x+1) || y<0 || iHeight<=y)?OutColor:mask[x+1+y*iWidth]; to=((x+1)<0 || iWidth<=(x+1) || (y+1)<0 || iHeight<=(y+1))?OutColor:mask[x+1+(y+1)*iWidth]; if(from==OutColor && to==InColor){ x=x+1; y=y; lpPoints[n].x=x; lpPoints[n].y=(iHeight-1)-y; n++; vec=7; continue; } case 3: //右上が輪郭? from=((x+1)<0 || iWidth<=(x+1) || (y+1)<0 || iHeight<=(y+1))?OutColor:mask[x+1+(y+1)*iWidth]; to=(x<0 || iWidth<=x || (y+1)<0 || iHeight<=(y+1))?OutColor:mask[x+(y+1)*iWidth]; if(from==OutColor && to==InColor){ x=x+1; y=y+1; lpPoints[n].x=x; lpPoints[n].y=(iHeight-1)-y; n++; vec=8; continue; } case 2: //中上が輪郭? from=(x<0 || iWidth<=x || (y+1)<0 || iHeight<=(y+1))?OutColor:mask[x+(y+1)*iWidth]; to=((x-1)<0 || iWidth<=(x-1) || (y+1)<0 || iHeight<=(y+1))?OutColor:mask[x-1+(y+1)*iWidth]; if(from==OutColor && to==InColor){ x=x; y=y+1; lpPoints[n].x=x; lpPoints[n].y=(iHeight-1)-y; n++; vec=9; continue; } case 1: //左上が輪郭? from=((x-1)<0 || iWidth<=(x-1) || (y+1)<0 || iHeight<=(y+1))?OutColor:mask[x-1+(y+1)*iWidth]; to=((x-1)<0 || iWidth<=(x-1) || y<0 || iHeight<=y)?OutColor:mask[x-1+y*iWidth]; if(from==OutColor && to==InColor){ x=x-1; y=y+1; lpPoints[n].x=x; lpPoints[n].y=(iHeight-1)-y; n++; vec=6; continue; } case 4: //左中が輪郭? from=((x-1)<0 || iWidth<=(x-1) || y<0 || iHeight<=y)?OutColor:mask[x-1+y*iWidth]; to=((x-1)<0 || iWidth<=(x-1) || (y-1)<0 || iHeight<=(y-1))?OutColor:mask[x-1+(y-1)*iWidth]; if(from==OutColor && to==InColor){ x=x-1; y=y; lpPoints[n].x=x; lpPoints[n].y=(iHeight-1)-y; n++; vec=3; continue; } case 7: //左下が輪郭? from=((x-1)<0 || iWidth<=(x-1) || (y-1)<0 || iHeight<=(y-1))?OutColor:mask[x-1+(y-1)*iWidth]; to=(x<0 || iWidth<=x || (y-1)<0 || iHeight<=(y-1))?OutColor:mask[x+(y-1)*iWidth]; if(from==OutColor && to==InColor){ x=x-1; y=y-1; lpPoints[n].x=x; lpPoints[n].y=(iHeight-1)-y; n++; vec=2; continue; } case 8: //中下が輪郭? from=(x<0 || iWidth<=x || (y-1)<0 || iHeight<=(y-1))?OutColor:mask[x+(y-1)*iWidth]; to=((x+1)<0 || iWidth<=(x+1) || (y-1)<0 || iHeight<=(y-1))?OutColor:mask[x+1+(y-1)*iWidth]; if(from==OutColor && to==InColor){ x=x; y=y-1; lpPoints[n].x=x; lpPoints[n].y=(iHeight-1)-y; n++; vec=1; continue; } case 9: //右下が輪郭? from=((x+1)<0 || iWidth<=(x+1) || (y-1)<0 || iHeight<=(y-1))?OutColor:mask[x+1+(y-1)*iWidth]; to=((x+1)<0 || iWidth<=(x+1) || y<0 || iHeight<=y)?OutColor:mask[x+1+y*iWidth]; if(from==OutColor && to==InColor){ x=x+1; y=y-1; lpPoints[n].x=x; lpPoints[n].y=(iHeight-1)-y; n++; vec=4; continue; } vec=6; //一番上へ } } } //EditMask 関数 //ある座標に隣接している特定の色を置き換えます //mask :マスク画像 //iWidth :マスク画像の横幅 //iHeight :マスク画像の高さ //BeforeColor :置き換え対象の色 //xStart :この座標に隣接している BeforeColor の色を持つ画素が置き換えられます //yStart :この座標に隣接している BeforeColor の色を持つ画素が置き換えられます //AfterColor :置き換える色 //戻り値 :なし void EditMask(LPBYTE mask,int iWidth,int iHeight,BYTE BeforeColor, int xStart,int yStart,BYTE AfterColor) { int flag; int x,y; int xm,xp,ym,yp; mask[xStart+yStart*iWidth]=AfterColor; while(1){ flag=0; for(y=0;yiWidth-1) xp=iWidth-1; if(ym<0) ym=0; if(yp>iHeight-1) yp=iHeight-1; if(mask[xm+ym*iWidth]==BeforeColor){ mask[xm+ym*iWidth]=AfterColor; flag++; } if(mask[x+ym*iWidth]==BeforeColor){ mask[x+ym*iWidth]=AfterColor; flag++; } if(mask[xp+ym*iWidth]==BeforeColor){ mask[xp+ym*iWidth]=AfterColor; flag++; } if(mask[xm+y*iWidth]==BeforeColor){ mask[xm+y*iWidth]=AfterColor; flag++; } if(mask[xp+y*iWidth]==BeforeColor){ mask[xp+y*iWidth]=AfterColor; flag++; } if(mask[xm+yp*iWidth]==BeforeColor){ mask[xm+yp*iWidth]=AfterColor; flag++; } if(mask[x+yp*iWidth]==BeforeColor){ mask[x+yp*iWidth]=AfterColor; flag++; } if(mask[xp+yp*iWidth]==BeforeColor){ mask[xp+yp*iWidth]=AfterColor; flag++; } } } } if(flag==0) return; } } //EditMaskAssist 関数 //画像の四辺に隣接している特定の色を置き換えるように //EditMask 関数を呼び出します //mask :マスク画像 //iWidth :マスク画像の横幅 //iHeight :マスク画像の高さ //MaskColor :置き換え対象の色 //EditColor :置き換える色 //戻り値 :なし void EditMaskAssist(LPBYTE mask,int iWidth,int iHeight,BYTE MaskColor,BYTE EditColor) { int x,y; for(x=0;x