利用OpenCV生成关于某点的颜色径向均匀渐变图像
2015-01-20 16:33
211 查看
#include "cv.h" #include "highgui.h" #include <math.h> #pragma comment(lib,"highgui.lib") #pragma comment(lib,"cxcore.lib") #pragma comment(lib,"cv.lib") int main( int argc, char** argv ) { IplImage* image = cvCreateImage(cvSize(800,600),IPL_DEPTH_8U,3); if(!image) { return -1; } CvScalar a=CV_RGB(0,255,0); CvScalar b=CV_RGB(0,0,0); cvSet(image,a); CvPoint origin=cvPoint(800,600); CvPoint center=cvPoint(image->width/2,image->height/2); double distance; if(origin.x<=center.x && origin.y<=center.y) { distance=sqrt((image->width-1-origin.x)*(image->width-1-origin.x)+ (image->height-1-origin.y)*(image->height-1-origin.y)); } else if(origin.x<=center.x && origin.y>center.y) { distance=sqrt((image->width-1-origin.x)*(image->width-1-origin.x)+ origin.y*origin.y); } else if(origin.x>center.x && origin.y<=center.y) { distance=sqrt(origin.x*origin.x+ (image->height-1-origin.y)*(image->height-1-origin.y)); } else if(origin.x>center.x && origin.y>center.y) { distance=sqrt(origin.x*origin.x+origin.y*origin.y); } double weightB=(b.val[0]-a.val[0])/distance; double weightG=(b.val[1]-a.val[1])/distance; double weightR=(b.val[2]-a.val[2])/distance; for(int i=0;i<image->width;i++) { for(int j=0;j<image->height;j++) { double dist=sqrt((i-origin.x)*(i-origin.x)+(j-origin.y)*(j-origin.y)); uchar* ptr = &CV_IMAGE_ELEM(image,uchar,j,i*3); ptr[0] = cvRound(ptr[0]+weightB*dist); ptr[1] = cvRound(ptr[1]+weightG*dist); ptr[2] = cvRound(ptr[2]+weightR*dist); } } cvSaveImage( "radial.jpg", image ); cvNamedWindow( "test", 1 ); cvShowImage( "test", image ); cvWaitKey(); cvDestroyWindow("test"); cvReleaseImage(&image); return 0; }
下图是上面的代码生成的效果图。通过改写上面代码中的相关参数,主要是变量a,b和origin,可以生成更炫的渐变图。
当修改为a=CV_RGB(0,255,0); b=CV_RGB(255,0,0);产生如下的图像
当修改为a=CV_RGB(255,255,0);b=CV_RGB(0,0,255);origin=cvPoint(200,100);生成的图像如下
相关文章推荐
- 利用OpenCV生成关于某点的颜色径向均匀渐变图像
- Opencv 生成径向渐变图像
- OpenCV——颜色均匀渐变
- 关于OpenCV中利用函数cvConvert实现图像数据类型转换(8U->16S)
- python3-opencv库(3)--图片颜色空间转换,利用HSV进行物体跟踪,图像通道分离与合并
- opencv2-利用imwrite函数生成带透明通道的png图像
- 关于C#操作EXCEL,生成图表的全面应用之一(利用Microsoft.Office.Interop.Excel) 收藏
- OpenCV 利用MFC的Picture控件显示和处理图像
- 利用递增的数字返回循环渐变的颜色的js代码
- j2me 颜色渐变与图像透明效果
- MFC+OpenCV 利用Show方法使图像不拉伸显示在Picture控件上
- 关于利用Schema解析XML,生成、编辑XML的节点问题
- ArcEngine中利用IRandomColorRamp 生成颜色带
- 如何利用VB.Net生成动态“关于”对话框
- j2me中生成渐变的颜色条
- 利用ASP.NET一般处理程序动态生成Web图像
- 利用GradientFill函数实现颜色渐变效果
- 利用递增的数字返回循环渐变的颜色的js函数
- 练习:利用颜色直方图匹配算法实现图像中目标的识别
- 关于图像生成略缩图的问题,