图形与图像编程(四)-图像特效

来源:互联网 发布:深圳市历年gdp数据 编辑:程序博客网 时间:2024/05/22 16:43

一、图像锐化处理

//图像锐化处理void CGDIEffectSampleView::OnMenuitemRuihua() {Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok)        return;Bitmap oldBitmap(L"girl.JPG");status = oldBitmap.GetLastStatus();if (status != Ok)        return;UINT width = oldBitmap.GetWidth();UINT height = oldBitmap.GetHeight();Bitmap newBitmap(width, height);Color pixel1, pixel2, pixel;graphics.DrawImage(&oldBitmap, 10, 0, width, height);int Laplacian[] ={ -1, -1, -1, -1, 9, -1, -1, -1, -1 };//拉普拉斯模板for (int x = 1; x < (int)width - 1; x++){for (int y = 1; y < (int)height - 1; y++){int r = 0, g = 0, b = 0;int Index = 0;for (int col = -1; col <= 1; col++)for (int row = -1; row <= 1; row++){oldBitmap.GetPixel(x + row, y + col, &pixel); r += pixel.GetRed() * Laplacian[Index];g += pixel.GetGreen() * Laplacian[Index];b += pixel.GetBlue() * Laplacian[Index];Index++;}if ( r > 255 ) r = 255;else if ( r < 0 ) r = -r;if ( g > 255 ) g = 255;else if ( g < 0 ) g = -g;if ( b > 255 ) b = 255;else if ( b < 0 ) b = -b;pixel.SetFromCOLORREF(RGB(r, g, b));newBitmap.SetPixel(x - 1, y - 1, pixel);}}graphics.DrawImage(&newBitmap, width+20, 0, width, height);}


二、图像柔化处理

//图像柔化处理void CGDIEffectSampleView::OnMenuitemRouhua() {Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok) return;Bitmap oldBitmap(L"girl.JPG");status = oldBitmap.GetLastStatus();if (status != Ok) return;UINT width = oldBitmap.GetWidth();UINT height = oldBitmap.GetHeight();Bitmap newBitmap(width, height);Color pixel1, pixel2, pixel;graphics.DrawImage(&oldBitmap, 10, 0, width, height);int smoothGauss[9] = {1,2,1,2,4,2,1,2,1}; // 高斯模板for (int x = 1; x < (int)width - 1; x++){for (int y = 1; y < (int)height - 1; y++){int r = 0, g = 0, b = 0;int Index = 0;for (int col = -1; col <= 1; col++)for (int row = -1; row <= 1; row++){oldBitmap.GetPixel(x + row, y + col, &pixel); r += pixel.GetRed() * smoothGauss[Index];g += pixel.GetGreen() * smoothGauss[Index];b += pixel.GetBlue() * smoothGauss[Index];Index++;}r = (r/16);g = (g/16);b = (b/16);if ( r > 255 ) r = 255;else if ( r < 0 ) r = -r;if ( g > 255 ) g = 255;else if ( g < 0 ) g = -g;if ( b > 255 ) b = 255;else if ( b < 0 ) b = -b;pixel.SetFromCOLORREF(RGB(r, g, b));newBitmap.SetPixel(x - 1, y - 1, pixel);}}graphics.DrawImage(&newBitmap, width+20, 0, width, height);   }


三、图像反色处理

//图像反色处理void CGDIEffectSampleView::OnMenuitemFanse() {//创建反色图像的颜色矩阵    ColorMatrix colorMatrix = {-1.0f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f, -1.0f, 0.0f, 0.0f, 0.0f,0.0f, 0.0f, -1.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f, 1.0f, 0.0f,1.0f, 1.0f, 1.0f, 1.0f, 1.0f};Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok) return;Image image(L"girl.JPG");status = image.GetLastStatus();if (status != Ok) return;UINT width = image.GetWidth();UINT height = image.GetHeight();ImageAttributes imageAttributes;Rect destRect1(width+20, 10, width, height);    imageAttributes.SetColorMatrix(&colorMatrix,ColorMatrixFlagsDefault,ColorAdjustTypeBitmap);graphics.DrawImage(&image, 10, 10, width, height);graphics.DrawImage(&image, destRect1, 0, 0, width, height, UnitPixel,&imageAttributes);}


四、图像灰度处理

//图像灰度处理void CGDIEffectSampleView::OnMenuitemHuidu() {//创建灰度图像的颜色矩阵    ColorMatrix colorMatrix = {0.299f, 0.299f, 0.299f, 0.0f, 0.0f,0.587f, 0.587f, 0.587f, 0.0f, 0.0f,0.114f, 0.114f, 0.114f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f, 1.0f, 0.0f,0.0f, 0.0f, 0.0f, 0.0f, 1.0f};Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok) return;Image image(L"girl.JPG");status = image.GetLastStatus();if (status != Ok) return;UINT width = image.GetWidth();UINT height = image.GetHeight();ImageAttributes imageAttributes;Rect destRect1(width+20, 10, width, height);imageAttributes.SetColorMatrix(&colorMatrix,ColorMatrixFlagsDefault,ColorAdjustTypeBitmap);graphics.DrawImage(&image, 10, 10, width, height);graphics.DrawImage(&image, destRect1, 0, 0, width, height, UnitPixel,&imageAttributes);}


五、图像浮雕处理

//图像浮雕效果void CGDIEffectSampleView::OnMenuitemFudiao() {Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok) return;Bitmap oldBitmap(L"girl.JPG");status = oldBitmap.GetLastStatus();if (status != Ok) return;UINT width = oldBitmap.GetWidth();UINT height = oldBitmap.GetHeight();Bitmap newBitmap(width, height);Color pixel1, pixel2, pixel;graphics.DrawImage(&oldBitmap, 10, 0, width, height);for (int x = 0; x < (int)width-1; x++){for (int y = 0; y < (int)height-1; y++){int r = 0, g = 0, b = 0;oldBitmap.GetPixel(x, y, &pixel1);oldBitmap.GetPixel(x + 1, y + 1, &pixel2);r = abs(pixel1.GetRed() - pixel2.GetRed() + 128);g = abs(pixel1.GetGreen() - pixel2.GetGreen() + 128);b = abs(pixel1.GetBlue() - pixel2.GetBlue() + 128);if (r > 255) r = 255;if (r < 0) r = 0;if (g > 255) g = 255;if (g < 0) g = 0;if (b > 255) b = 255;if (b < 0) b = 0;pixel.SetFromCOLORREF(RGB(r, g, b));newBitmap.SetPixel(x, y, pixel);}}graphics.DrawImage(&newBitmap, width+20, 0, width, height);}


六、图像翻转

//图像翻转void CGDIEffectSampleView::OnMenuitemReserve() {Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok) return;Image image(L"girl.JPG");status = image.GetLastStatus();if (status != Ok) return;UINT width = image.GetWidth();UINT height = image.GetHeight();graphics.DrawImage(&image, 10, 0, width, height);image.RotateFlip(Rotate180FlipX);width = image.GetWidth();height = image.GetHeight();graphics.DrawImage(&image, 20 + width, 0, width, height);}


七、图像缩放

//图像缩放void CGDIEffectSampleView::OnMenuitemResize() {Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok) return;Image image(L"girl.JPG");status = image.GetLastStatus();if (status != Ok) return;UINT width = image.GetWidth();UINT height = image.GetHeight();graphics.DrawImage(&image, 0, 0, width, height);graphics.DrawImage(&image, width, 0, (int)0.75 * width, (int)0.75 * height);}


八、图像剪切

//图片剪切void CGDIEffectSampleView::OnMenuitemCut() {Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok) return;Image image(L"girl.JPG");status = image.GetLastStatus();if (status != Ok) return;UINT width = image.GetWidth();UINT height = image.GetHeight();Rect destRect1(width+20, 20, 30, 30);//小范围Rect destRect2(width+20, 100, 120, 120); //大范围graphics.DrawImage(&image, 0, 0, width, height);//原始图像graphics.DrawImage(&image, destRect1, 87, 123, 35, 35, UnitPixel);//缩小剪切graphics.SetInterpolationMode(InterpolationModeHighQualityBilinear);graphics.DrawImage(&image, destRect2, 87, 123, 35, 35, UnitPixel);//放大剪切}


九、图像马赛克

//图片马赛克效果void CGDIEffectSampleView::OnMenuitemMasaike() {Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok) return;graphics.Clear(Color::White);Image image(L"girl.JPG");status = image.GetLastStatus();if (status != Ok) return;UINT width = image.GetWidth();UINT height = image.GetHeight();int dw = width / 50;int dh = height / 50;int points[2500]={0};int nCount=0;while(nCount < 2500){int index = rand()%2500;if (points[index] == 0){nCount ++;points[index] = 1;int m = index/50;int n = index%50;Rect destRect(dw*m, dh*n, (m+1)*dw, (n+1)*dh);graphics.DrawImage(&image, destRect, dw*m, dh*n, (m+1)*dw, (n+1)*dh, UnitPixel);}Sleep(10);}}


十、垂直百叶窗

//垂直百叶窗显示图片void CGDIEffectSampleView::OnMenuitemVbaiye() {Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok) return;graphics.Clear(Color::White);Image image(L"girl.JPG");status = image.GetLastStatus();if (status != Ok) return;UINT width = image.GetWidth();UINT height = image.GetHeight();int nPixes = 15;int nNum = width/nPixes;for (int i = 0;i <nPixes; i++){for(int j=0;j<nNum;j++){//分别扫描每条Rect destRect1(0, j*nPixes+i, width, 1);graphics.DrawImage(&image, destRect1, 0, j*nPixes+i, width, 1, UnitPixel);}Sleep(50);}}


十一、水平百叶窗


//水平百叶窗显示图片void CGDIEffectSampleView::OnMenuitemHbaiye() {Status status = GenericError;Graphics graphics(m_hWnd);status = graphics.GetLastStatus();if (status != Ok) return;graphics.Clear(Color::White);Image image(L"girl.JPG");status = image.GetLastStatus();if (status != Ok) return;UINT width = image.GetWidth();UINT height = image.GetHeight();int nPixes = 30;int nNum = width/nPixes;for (int i = 0;i <nPixes; i++){for(int j=0;j<nNum;j++){//分别扫描每条Rect destRect1(j*nPixes+i, 0, 1, height);graphics.DrawImage(&image, destRect1, j*nPixes+i, 0, 1, height, UnitPixel);}Sleep(10);}}



原创粉丝点击