1 前备知识
各颜色对应的范围
2 所用到的主要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
floating-point.
@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
center.
@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 程序代码
#include "opencv2opencv.hpp" #include <iostream> using namespace std; using namespace cv; int main(int argc, char**argv) { Mat src = imread("G:\CVworkstudy\program_wwx\研习社140课时\ZhaiZhigang140\lena.jpg"); if (src.empty()) { printf("Could not load image... "); return -1; } //色彩空间转换 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);//颜色提取需要先转换为HSV颜色空间 //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. inRange(getColorImg, Scalar(100, 43, 46), Scalar(124, 255, 255), getMask);//蓝色范围内的颜色转换为255,其他为0 blur(getMask, getMask1, Size(5, 5));//均值滤波会使边缘变得模糊 imshow("GetMask1", getMask1); medianBlur(getMask, getMask, 5);//中值滤波对边缘轮廓影响较小 imshow("GetMask", getMask); waitKey(0); return 0; }
4 运行结果
5 扩展及注意事项
NULL