Fork me on GitHub

【图像算法】彩色图像分割专题四:测试图片的生成

【图像算法】彩色图像分割专题四:测试图片的生成

  SkySeraph May 29th 2011  HQU

Email:zgzhaobo@gmail.com    QQ:452728574

Latest Modified Date:May 29th 2011 HQU

一 说明:

用于对彩色分割算法的测试。

 

二 源码:

 说明:读者可修改代码中的H、S、V值生成所需要的测试图片

/////////////////////////////////////////////////////////////////////////////
// Note:		生成测试图片
// Copyright:	zgzhaobo@gmail.com  @skyseraph 
// Version:	5/23/2011
/////////////////////////////////////////////////////////////////////////////
void CColorSegDlg::OnCreatImage() 
{
	if(!(ToDisplayCtr1))
	{
		MessageBox("Please Load Pic!");
		return;
	}	
	
	//  定义工作位图
	IplImage* src;
	src = ToDisplayCtr1;

	IplImage * dst = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,3);
	double R,G,B,H,S,V;
	int x,y;

	//给定HSV值,转换为RGB后生成测试图像

	//①和③ V相同/近,H相距较大,S相距2倍(测试S大小对聚类影响)
	//①
	for (y=0; y<src->height/4; y++)
	{
		for (x=0; x<src->width/4; x++)
		{
			H = (2*PI*260/360);//弧度制
			S = 5/100.0;
			V = 71*2.55/255.0;
			//  RGB-HSV
			pMyColorSpace.HSV2RGB(R,G,B,H,S,V);	
			R = R*255;
			G = G*255;
			B = B*255;
			// 获取当前点BGR值
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels] = (int)B;
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels+1] = (int)G;
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels+2] = (int)R;	
		}
	}
	//③
	for (y=0; y<src->height/4; y++)
	{
		for (x=src->width/4; x<src->width; x++)
		{
			H = (2*PI*104/360);//弧度制
			S = 7/100.0;
			V = 71*2.55/255.0;
			//  RGB-HSV
			pMyColorSpace.HSV2RGB(R,G,B,H,S,V);	
			R = R*255;
			G = G*255;
			B = B*255;
			// 获取当前点BGR值
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels] = (int)B;
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels+1] = (int)G;
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels+2] = (int)R;	
		}
	}


	//②和④ V相同,H相同/近,测试S(S大小对聚类影响)
    //②
	for (y=src->height/4; y<src->height; y++)
	{
		for (x=0; x<src->width/4; x++)
		{
			H = (2*PI*260/360);//弧度制
			S = 20.0/100.0;
			V = 71*2.55/255.0;
			//  RGB-HSV
			pMyColorSpace.HSV2RGB(R,G,B,H,S,V);	
			R = R*255;
			G = G*255;
			B = B*255;
			// 获取当前点BGR值
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels] = (int)B;
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels+1] = (int)G;
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels+2] = (int)R;	
		}
	}
	//④
	for (y=src->height/4; y<src->height; y++)
	{
		for (x=src->width/4; x<src->width; x++)
		{
			H = (2*PI*104/360);//弧度制
			S = 40.0/100.0;
			V = 71*2.55/255.0;
			//  RGB-HSV
			pMyColorSpace.HSV2RGB(R,G,B,H,S,V);	
			R = R*255;
			G = G*255;
			B = B*255;
			// 获取当前点BGR值
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels] = (int)B;
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels+1] = (int)G;
			((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels+2] = (int)R;	
		}
	}

	cvNamedWindow("ImageRGB",0);
	cvShowImage("ImageRGB",dst);
	cvSaveImage(".\\imageRGB.jpg",dst);


/*	// 对生成的图像转换为HSV输出显示
	IplImage * dstHSV = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,3);
	for (y=0; y<src->height; y++)
	{
		for ( x=0; x<src->width; x++)
		{
			// 获取当前点BGR值
			B = ((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels];
			G = ((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels+1];
			R = ((uchar*)(dst->imageData + y*dst->widthStep))[x*dst->nChannels+2];
			//
			pMyColorSpace.RGB2HSV(R,G,B,H,S,V);	
			H = 360*H/(2*PI); //弧度转换为角度制
			V = V*255.0;  //[0,1] to [0,255]
			S = S*100.0;  //[0,1] to [0,100]
			//
			((uchar*)(dstHSV->imageData + y*dstHSV->widthStep))[x*dstHSV->nChannels] = (int)H;
			((uchar*)(dstHSV->imageData + y*dstHSV->widthStep))[x*dstHSV->nChannels+1] = (int)S;
			((uchar*)(dstHSV->imageData + y*dstHSV->widthStep))[x*dstHSV->nChannels+2] = (int)V;	
		}
	}

	cvNamedWindow("ImageHSV",0);
	cvShowImage("ImageHSV",dstHSV);
	cvSaveImage(".\\imageHSV.jpg",dstHSV);
*/

	cvWaitKey(0);
	cvDestroyAllWindows();
}

 

三 图片:

图片一: V相同/相近、H相距较大,用于测试饱和度S大小对分割算法的影响

左上部分和右上部分的V相同(71),H相距较大(260、104),S为5、7

左下部分和右下部分的V相同(71),H为(260、104),S为20、40

Note:S范围[0,100] V[0,255] H范围[0,2PI]

 

 图片二  S相同/相近、H相距较大,用于测试亮度V大小对分割算法的影响

左上部分和右上部分的S相同(80),H相距较大(260、104),S为10、20

左下部分和右下部分的S相同(80),H为(260、104),S为50、70

Note:S范围[0,100] V[0,255] H范围[0,2PI]

 

More in  http://skyseraph.com/2011/08/27/CV/图像算法专题/ 

 

Author:         SKySeraph

Email/GTalk: zgzhaobo@gmail.com    QQ:452728574

From:         http://www.cnblogs.com/skyseraph/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,请尊重作者的劳动成果

posted @ 2011-05-29 11:12  SkySeraph  阅读(3276)  评论(0编辑  收藏  举报