下面代碼的基礎是對圖像像素的訪問。實現浮雕和雕刻的代碼是統一的,如下
- #include <cv.h>
- #include <highgui.h>
- #pragma comment( lib, "cv.lib" )
- #pragma comment( lib, "cxcore.lib" )
- #pragma comment( lib, "highgui.lib" )
- int main()
- {
- IplImage *org=cvLoadImage("1.jpg",1);
- IplImage *image=cvCloneImage(org);
- int width=image->width;
- int height=image->height;
- int step=image->widthStep;
- int channel=image->nChannels;
- uchar* data=(uchar *)image->imageData;
- for(int i=0;i<width-1;i++)
- {
- for(int j=0;j<height-1;j++)
- {
- for(int k=0;k<channel;k++)
- {
- int temp = data[(j+1)*step+(i+1)*channel+k]-data[j*step+i*channel+k]+128;//浮雕
- //int temp = data[j*step+i*channel+k]-data[(j+1)*step+(i+1)*channel+k]+128;//雕刻
- if(temp>255)
- {
- data[j*step+i*channel+k]=255;
- }
- else if(temp<0)
- {
- data[j*step+i*channel+k]=0;
- }
- else
- {
- data[j*step+i*channel+k]=temp;
- }
- }
- }
- }
- cvNamedWindow("original",1);
- cvShowImage("original",org);
- cvNamedWindow("image",1);
- cvShowImage("image",image);
- cvWaitKey(0);
- cvDestroyAllWindows();
- cvReleaseImage(&image);
- cvReleaseImage(&org);
- return 0;
- }
原圖為
浮雕效果圖如下
雕刻效果圖如下
下面是實現圖像褶皺的代碼,效果不是太好,結構過渡不平滑,以後再改進一下。希望能做到波浪化。
- #include <cv.h>
- #include <highgui.h>
- #pragma comment( lib, "cv.lib" )
- #pragma comment( lib, "cxcore.lib" )
- #pragma comment( lib, "highgui.lib" )
- int main()
- {
- IplImage *org=cvLoadImage("lena.jpg",1);
- IplImage *image=cvCloneImage(org);
- int width=image->width;
- int height=image->height;
- int step=image->widthStep;
- int channel=image->nChannels;
- uchar* data=(uchar *)image->imageData;
- int sign=-1;
- for(int i=0;i<height;i++)
- {
- int cycle=10;
- int margin=(i%cycle);
- if((i/cycle)%2==0)
- {
- sign=-1;
- }
- else
- {
- sign=1;
- }
- if(sign==-1)
- {
- margin=cycle-margin;
- for(int j=0;j<width-margin;j++)
- {
- for(int k=0;k<channel;k++)
- {
- data[i*step+j*channel+k]=data[i*step+(j+margin)*channel+k];
- }
- }
- }
- else if(sign==1)
- {
- for(int j=0;j<width-margin;j++)
- {
- for(int k=0;k<channel;k++)
- {
- data[i*step+j*channel+k]=data[i*step+(j+margin)*channel+k];
- }
- }
- }
- }
- cvNamedWindow("original",1);
- cvShowImage("original",org);
- cvNamedWindow("image",1);
- cvShowImage("image",image);
- cvSaveImage("image.jpg",image);
- cvWaitKey(0);
- cvDestroyAllWindows();
- cvReleaseImage(&image);
- cvReleaseImage(&org);
- return 0;
- }
測試圖是標准的lena圖,效果圖如下