__2017-12-16 如一模式识别研究

如一模式识别研究

智能算法>>颜色提取,图像分割 rgb hsv(转)

转自:http://www.cnblogs.com/carekee/articles/2279923.html

// Note: 颜色分割:提取特定颜色

/////////////////////////////////////////////////////////////////////////////

void CColorSegDlg::ColorSegByHSV(IplImage* img)

// 提取特定颜色

{

//====================== 变量定义====================//

int x,y; //循环

//====================== 输入彩色图像信息====================//

IplImage* pSrc = NULL;

pSrc = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);

cvCopyImage(img,pSrc);

int width = pSrc->width; //图像宽度

int height = pSrc->height; //图像高度

int depth = pSrc->depth; //图像位深(IPL_DEPTH_8U...)

int channels = pSrc->nChannels; //图像通道数(1、2、3、4)

int imgSize = pSrc->imageSize; //图像大小 imageSize = height*widthStep

int step = pSrc->widthStep/sizeof(uchar); //相邻行的同列点之间的字节数: 注意widthStep != width*nChannels (有字节填零补充)

uchar* data = (uchar *)pSrc->imageData;

int imageLen = width*height; //

//=========================================//

double B=0.0,G=0.0,R=0.0,H=0.0,S=0.0,V=0.0;

IplImage* dstColorSegByColor = cvCreateImage(cvGetSize(pSrc),IPL_DEPTH_8U,3);

IplImage* dstColorSegByColorGray = cvCreateImage(cvGetSize(pSrc),IPL_DEPTH_8U,1);

//CvFont font = cvFont( 1, 1 );

for (y=0; y

{

for ( x=0; x

{

// 获取BGR值

B = ((uchar*)(pSrc->imageData + y*pSrc->widthStep))[x*pSrc->nChannels];

G = ((uchar*)(pSrc->imageData + y*pSrc->widthStep))[x*pSrc->nChannels+1];

R = ((uchar*)(pSrc->imageData + y*pSrc->widthStep))[x*pSrc->nChannels+2];

// RGB-HSV

pMyColorSpace.RGB2HSV(R,G,B,H,S,V);

H = (360*H)/(2*PI);

// 黑白

//黑色

if(V<0.35)

{

((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]

= 0; //灰度

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]

= 0; //B

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]

= 0; //G

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]

= 0; //R

}

//白色

if(S<0.15 && V>0.75)

{

((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]

= 255; //灰度

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]

= 255; //B

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]

= 255; //G

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]

= 255; //R

}

//灰色

if(S<0.15 && 0.35

{

((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]

= 128; //灰度

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]

= 128; //B

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]

= 128; //G

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]

= 128; //R

}

// 彩色

if(V>=0.35 && S>=0.15)

{

//红色相近

if((H>=0 && H<15) || (H>=340 && H<360))

{

((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]

= 40; //灰度

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]

= 0; //B

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]

= 0; //G

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]

= 255; //R

}

//黄色相近

else if(H>=15 && H<75)

{

((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]

= 80; //灰度

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]

= 0; //B

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]

= 255; //G

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]

= 255; //R

}

//绿色相近

else if(H>=75 && H<150)

{

((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]

= 120; //灰度

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]

= 0; //B

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]

= 255; //G

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]

= 0; //R

}

//

//蓝色相近

else if(H>=185 && H<270)

{

((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]

= 200; //灰度

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]

= 255; //B

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]

= 0; //G

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]

= 0; //R

}

//

else

{

((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]

= 180; //灰度

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]

= 128; //B //紫色Purple

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]

= 0; //G

((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]

= 128; //R

}

}

}

}

//cvNamedWindow("src",1);

//cvShowImage("src",pSrc);

cvNamedWindow("dstColorSegByColor",1);

cvShowImage("dstColorSegByColor",dstColorSegByColor);

cvNamedWindow("dstColorSegByColorGray",1);

cvShowImage("dstColorSegByColorGray",dstColorSegByColorGray);

cvSaveImage(".\\dstColorSegByColor.jpg",dstColorSegByColor);

cvSaveImage(".\\dstColorSegByColorGray.jpg",dstColorSegByColorGray);

cvWaitKey(0);

cvDestroyAllWindows();

cvReleaseImage(&pSrc);

cvReleaseImage(&dstColorSegByColor);

cvReleaseImage(&dstColorSegByColorGray);

}

分类: OpenCV

评论留言区

:
  

作者: 游客 ; *
评论内容: *
带*号为必填项目

如一模式识别更新提示

matlab在图像处理方面的应用有更新

如一模式识别 友情链接

关于本站作者     chinaw3c     mozilla