CImage* Sobel(CImage* image) { CImage* returnImage = new CImage(); returnImage->Create(image->GetWidth(), image->GetHeight(), image->GetBPP(),0); matrix GX(3,3); matrix GY(3,3); int sumX[3]; int sumY[3]; int SUM[3]; int piX = 0; int piY = 0; int R,G,B = 0; int NC = 0; int I, J = 0; COLORREF pixVal; // Masks // //X// GX(0,0) = -1; GX(0,1) = 0; GX(0,2) = 1; GX(1,0) = -2; GX(1,1) = 0; GX(1,2) = 2; GX(2,0) = -1; GX(2,1) = 0; GX(2,2) = 1; //Y// GY(0,0) = 1; GY(0,1) = 2; GY(0,2) = 1; GY(1,0) = 0; GY(1,1) = 0; GY(1,2) = 0; GY(2,0) = -1; GY(2,1) = -2; GY(2,2) = -1; for(int Y=0; YGetHeight(); Y++) { for(int X=0; X < image->GetWidth(); X++) { sumX[0] = 0;sumX[1] = 0;sumX[2] = 0; sumY[0] = 0;sumY[1] = 0;sumY[2] = 0; if(Y==0 || Y == image->GetHeight()-1) { SUM[0] = 0; SUM[1] = 0; SUM[2] = 0; } else if(X==0 || X == image->GetWidth()-1) { SUM[0] = 0; SUM[1] = 0; SUM[2] = 0; } else { for(I=-1; I<=1; I++) { for(J=-1; J<=1; J++) { piX = J + X; piY = I + Y; pixVal = image->GetPixel(piX,piY); R = GetRValue(pixVal); G = GetGValue(pixVal); B = GetBValue(pixVal); sumX[0] = sumX[0] + (R) * GX(J+1,I+1); sumX[1] = sumX[1] + (G) * GX(J+1,I+1); sumX[2] = sumX[2] + (B) * GX(J+1,I+1); sumY[0] = sumY[0] + (R) * GY(J+1,I+1); sumY[1] = sumY[1] + (G) * GY(J+1,I+1); sumY[2] = sumY[2] + (B) * GY(J+1,I+1); } } SUM[0] = abs(sumX[0]) + abs(sumY[0]); SUM[1] = abs(sumX[1]) + abs(sumY[1]); SUM[2] = abs(sumX[2]) + abs(sumY[2]); } if(SUM[0]>255) SUM[0]=255; if(SUM[0]<0) SUM[0]=0; if(SUM[1]>255) SUM[1]=255; if(SUM[1]<0) SUM[1]=0; if(SUM[2]>255) SUM[2]=255; if(SUM[2]<0) SUM[2]=0; int newPixel[3]; newPixel[0] = (255 - (unsigned char)(SUM[0])); newPixel[1] = (255 - (unsigned char)(SUM[1])); newPixel[2] = (255 - (unsigned char)(SUM[2])); COLORREF newPixCol = RGB(newPixel[0],newPixel[1],newPixel[2]); returnImage->SetPixel(X,Y,newPixCol) ; } } return returnImage; }
obj-C 基于索贝尔算子的彩色素描效果
最新推荐文章于 2021-12-29 22:38:50 发布