OpenCv 009 --- color space and color space conversion

1 prepared before knowledge

Corresponding to each color range

2 used mainly OpenCv API

/** @brief Converts an image from one color space to another.

@param src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision
@param dst output image of the same size and depth as src.
@param code color space conversion code (see #ColorConversionCodes).
@param dstCn number of channels in the destination image; if the parameter is 0, the number of the
channels is derived automatically from src and code.

@see @ref imgproc_color_conversions

CV_EXPORTS_W void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 );

/** @brief Checks if array elements lie between the elements of two other arrays.

That is, dst (I) is set to 255 (all 1 -bits) if src (I) is within the
specified 1D, 2D, 3D, ... box and 0 otherwise.

When the lower and/or upper boundary parameters are scalars, the indexes
(I) at lowerb and upperb in the above formulas should be omitted.
@param src first input array.
@param lowerb inclusive lower boundary array or a scalar.
@param upperb inclusive upper boundary array or a scalar.
@param dst output array of the same size as src and CV_8U type.

CV_EXPORTS_W void inRange(InputArray src, InputArray lowerb,
                          InputArray upperb, OutputArray dst);

/** @brief Blurs an image using the normalized box filter.

The function smooths an image using the kernel:

@param src input image; it can have any number of channels, which are processed independently, but
the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
@param dst output image of the same size and type as src.
@param ksize blurring kernel size.
@param anchor anchor point; default value Point(-1,-1) means that the anchor is at the kernel
@param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes
@sa boxFilter, bilateralFilter, GaussianBlur, medianBlur

CV_EXPORTS_W void blur( InputArray src, OutputArray dst,
                        Size ksize, Point anchor = Point(-1,-1),
                        int borderType = BORDER_DEFAULT );

/** @brief Blurs an image using the median filter.

@note The median filter uses #BORDER_REPLICATE internally to cope with border pixels, see #BorderTypes

@param src input 1-, 3-, or 4-channel image; when ksize is 3 or 5, the image depth should be
CV_8U, CV_16U, or CV_32F, for larger aperture sizes, it can only be CV_8U.
@param dst destination array of the same size and type as src.
@param ksize aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...
@sa bilateralFilter, blur, boxFilter, GaussianBlur

CV_EXPORTS_W void medianBlur( InputArray src, OutputArray dst, int ksize );

3 program code

#include " opencv2 \ opencv.hpp " 
#include <the iostream> the using namespace STD;
 the using namespace CV; int main ( int argc, char ** the argv) 
    Mat the src = imread ( " G: program_wwx \\ \\ \\ CVworkstudy Yanxishe 140 hours ZhaiZhigang140 \\ lena.jpg \\ " );
     IF (src.empty ()) 
        the printf ( " Could Not Load Image ... \ n- " );
         return - . 1 ; 
    } // color space conversion


    Mat yuvImg, hsvImg, ycrcbImg;
    cvtColor(src, yuvImg, CV_BGR2YUV);
    cvtColor(src, hsvImg, CV_BGR2HSV);
    cvtColor(src, ycrcbImg, CV_BGR2YCrCb);
    imshow("YUV", yuvImg);
    imshow("HSV", hsvImg);
    imshow("YCRCB", ycrcbImg);

    Mat getColorImg, getMask, getMask1;
    getColorImg = imread("G:\\CVworkstudy\\program_wwx\\研习社140课时\\ZhaiZhigang140\\myPhoto.jpg");
    //imshow("MyPhoto", getColorImg);
    cvtColor (getColorImg, getColorImg, CV_BGR2HSV); // color extraction need to be converted HSV color space
     // That IS, DST (the I) IS SET to 255 (All -Bits. 1) IF the src (the I) The IS WITHIN
     // specified . 1D, 2D, 3D, ... and 0 otherwise Box 
    inRange (getColorImg, the Scalar ( 100 , 43 is , 46 is ), the Scalar ( 124 , 255 , 255 ), getMask); // in the blue color range is converted to 255 , the other is 0 
    blur (getMask, getMask1, Size ( . 5 , . 5 )); // mean edge blur filter will 
    imshow ( " GetMask1 " , getMask1);
    medianBlur (getMask, getMask, . 5 ); // median filtering little effect on the edge profile 
    imshow ( " GetMask " , getMask); 

    waitKey ( 0 );
     return  0 ; 

4 run results

5 Expansion and precautions


Guess you like