[ImageAlgorithm] Color image segmentation Topic 5: extracting specific colors of color images
Skyseraph Jun 8th 2011 hqu
Email: zgzhaobo@gmail.com QQ: 452728574
Latest modified Date: Jun 8th 2011 hqu
I. Principles and instructions:
1 RGB (red, green, blue) mode is a device-related color space, the most commonly used is the display system.In RGB, each component is highly correlated, and each channel is programmed with brightness information, which is easily affected by the surrounding environment (light, etc.). This is not consistent with the human eye's cognitive color process, it is not suitable for color image analysis and segmentation. Compared with the human visual system division, the HSV space is more suitable for image analysis. For more information about various color space models, see http://www.cnblogs.com/skyseraph/archive/2011/05/03/2035643.html
2In many papers on License Plate Recognition in China, color information is usually used in the HSV/yiq/Lab mode according to the specific license plate color information (Common license plate colors include: white-bottom, black-bottom, white-bottom, and black-bottom. The color extraction method is described in this article. This method is only applicable to the extraction of specific colors. It is similar to supervised learning in PR terms. On the contrary, unsupervised color segmentation is applied to any image, which belongs to the field of color segmentation.
3. Division of HSV ranges:
<1> paper: car color recognition from CCTV camera image: http://www.docin.com/p-211572110.html
The author uses the following method:
<2> paper: Using Support Vector Machine to identify vehicle color: http://www.cnki.com.cn/Article/CJFDTotal-JSJF200405018.htm
The author first splits 16 colors in the lab space and then splits the sample space in the HSV mode, using the following method:
<3> based on the experiment, this article adopts the division method as shown in the source code. In this mode, the test results are better.
Source code:
//////////////////////////////////////// /// // Note: color Segmentation: extract specific colors // version: 5/11/2011 skyseraph/zhaobo zgzhaobo@gmail.com /////////////////////////////////// //////////////////////////////////////// // void ccolorsegdlg:: colorsegbyhsv (iplimage * IMG) // extract a specific color. ================/// int X, y; // loop // ================================== input color image information ========== ========== ===// Iplimage * psrc = NULL; psrc = cvcreateimage (cvgetsize (IMG), IMG-> depth, IMG-> nchannels); cvcopyimage (IMG, psrc ); int width = psrc-> width; // Image Width int Height = psrc-> height; // Image Height int depth = psrc-> depth; // image depth (ipl_depth_8u ...) int channels = psrc-> nchannels; // Number of image channels (1, 2, 3, 4) int imgsize = psrc-> imagesize; // image size imagesize = height * widthstepint step = psrc-> widthstep/sizeof (uchar); // number of bytes between points in the same column of adjacent rows: Pay attention to width Step! = Width * nchannels (with Bytes filled with zeros) 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 Effect 3:
(1) Source image
(2) color chart after color segmentation
(3) grayscale image after color segmentation (displayed at different gray levels)
Supplement 4 (source network in RGB mode)
1 Source Code
Void cfindrgbdlg: onfind () {int color = m_colorlist.getcursel (); PIC = cvcreateimage (cvsize (image-> width, image-> height), 8, 1 ); cvzero (PIC); For (INT x = 0; x <image-> height; X ++) {for (INT y = 0; y <image-> width; Y ++) {uchar * ptrimg = & cv_image_elem (image, uchar, x, y * 3); // uchar * ptrpic = & (uchar *) (PIC-> imagedata + pic-> widthstep * y) [x]; // redif (color = 0) {If (ptrimg [0]-ptrimg [1])> 200 & (ptrimg [0]-ptrimg [2])> 200) cv_image_elem (PIC, uchar, X, y) = 255;} // greenelse if (color = 1) {If (ptrimg [1]-ptrimg [0])> 200 & (ptrimg [1]-ptrimg [2])> 200) cv_image_elem (PIC, uchar, x, y) = 255 ;} // blueelse if (color = 2) {If (ptrimg [2]-ptrimg [0])> 200 & (ptrimg [2]-ptrimg [1])> 200) cv_image_elem (PIC, uchar, x, y) = 255 ;}} cvnamedwindow ("Temp",-1); cvshowimage ("Temp", PIC ); cvwaitkey (); storage = cvcreatememstorage (0); contour = 0; mode = cv_retr_external; cvfindcontours (PIC, storage, & contour, sizeof (cvcontour), mode, duration ); cvdrawcontours (image, contour, cv_rgb (0, 0), cv_rgb (0, 0, 0), 2, 2, 8); crect rect; getdlgitem (idc_picture) -> getclientrect (& rect); invalidaterect (rect, true );}
2 results:
Author: skyseraph
Email/Gtalk: zgzhaobo@gmail.com QQ: 452728574
From: http://www.cnblogs.com/skyseraph/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Please respect the author's Labor achievements.