【原文:http://blog.csdn.net/yangtrees/article/details/9090607】
==============================================
版权所有:小熊不去实验室CSDN博客
==============================================
浮雕的算法是对图像的每一个点进行卷积处理,采用的矩阵如下
[1 0 0;
0 0 0;
0 0 -1 ];
假设原图像为X,处理后的图像为Y ,也就是说,对于坐标为(i,j)点,其浮雕效果图的算法为Y(i,j)=X(i+1,j+1)-X(i-1,j-1)+128。当然,X,Y的取值均在0~255之间。
同时也可以
[1 0
0 -1]
假设原图像为X,处理后的图像为Y ,也就是说,对于坐标为(i,j)点,其浮雕效果图的算法为Y(i,j)=X(i,j)-X(i-1,j-1)+128。当然,X,Y的取值均在0~255之间。
- #include <opencv/cv.h>
- #include <opencv/highgui.h>
- using namespace cv;
- int main()
- {
- Mat src = imread("D:/test5.jpg");
- Mat img0(src.size(),CV_8UC3);
- Mat img1(src.size(),CV_8UC3);
- for (int y=1; y<src.rows-1; y++)
- {
- uchar *p0 = src.ptr<uchar>(y);
- uchar *p1 = src.ptr<uchar>(y+1);
- uchar *q0 = img0.ptr<uchar>(y);
- uchar *q1 = img1.ptr<uchar>(y);
- for (int x=1; x<src.cols-1; x++)
- {
- for (int i=0; i<3; i++)
- {
- int tmp0 = p1[3*(x+1)+i]-p0[3*(x-1)+i]+128;//浮雕
- if (tmp0<0)
- q0[3*x+i]=0;
- else if(tmp0>255)
- q0[3*x+i]=255;
- else
- q0[3*x+i]=tmp0;
- int tmp1 = p0[3*(x-1)+i]-p1[3*(x+1)+i]+128;//雕刻
- if (tmp1<0)
- q1[3*x+i]=0;
- else if(tmp1>255)
- q1[3*x+i]=255;
- else
- q1[3*x+i]=tmp1;
- }
- }
- }
- imshow("src",src);
- imshow("浮雕",img0);
- imshow("雕刻",img1);
- waitKey();
- }
原图:
浮雕:
雕刻: