项目 | 参数 |
---|---|
OS | Windows 10 Professional amd x64 |
IDE | Visual Studio 2017 Professional |
OpenCV | 3.2 |
cvLoadImage
函数将图片读入到一个IplImage
中。/* load image from file
iscolor can be a combination of above flags where CV_LOAD_IMAGE_UNCHANGED
overrides the other flags
using CV_LOAD_IMAGE_ANYCOLOR alone is equivalent to CV_LOAD_IMAGE_UNCHANGED
unless CV_LOAD_IMAGE_ANYDEPTH is specified images are converted to 8bit
*/
CVAPI(IplImage*) cvLoadImage( const char* filename, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR));
cvCreateImage
创建单通道图片容器,用来存放图片。/** @brief Creates an image header and allocates the image data.
This function call is equivalent to the following code:
@code
header = cvCreateImageHeader(size, depth, channels);
cvCreateData(header);
@endcode
@param size Image width and height
@param depth Bit depth of image elements. See IplImage for valid depths.
@param channels Number of channels per pixel. See IplImage for details. This function only creates
images with interleaved channels.
*/
CVAPI(IplImage*) cvCreateImage( CvSize size, int depth, int channels );
cvSetZero
用来清除图片中的数据。/** @brief Clears the array.
The function clears the array. In the case of dense arrays (CvMat, CvMatND or IplImage),
cvZero(array) is equivalent to cvSet(array,cvScalarAll(0),0). In the case of sparse arrays all the
elements are removed.
@param arr Array to be cleared
*/
CVAPI(void) cvSetZero( CvArr* arr );
cvMerge
用来合并图片中的数据。/** Merges a set of single-channel arrays into the single multi-channel array
or inserts one particular [color] plane to the array */
CVAPI(void) cvMerge( const CvArr* src0, const CvArr* src1,
const CvArr* src2, const CvArr* src3,
CvArr* dst );
cvShowImage
用来显示处理结果。/* display image within window (highgui windows remember their content) */
CVAPI(void) cvShowImage( const char* name, const CvArr* image );
cvCvtColor
转换为灰图/** @brief Converts input array pixels from one color space to another
@see cv::cvtColor
*/
CVAPI(void) cvCvtColor( const CvArr* src, CvArr* dst, int code );
下面部署真实代码:
#include "stdafx.h" //注意这个放到最前面,有时候可能会报错
#include //解决cv命名空间找不到问题
#include
#include
using namespace std;
using namespace cv;
int main()
{
IplImage* imgload = cvLoadImage("20180726095642.png", 1);
IplImage* Bimg = cvCreateImage(cvGetSize(imgload), IPL_DEPTH_8U, 1);
IplImage* Gimg = cvCreateImage(cvGetSize(imgload), IPL_DEPTH_8U, 1);
IplImage* Rimg = cvCreateImage(cvGetSize(imgload), IPL_DEPTH_8U, 1);
cvSplit(imgload, Bimg, Gimg, Rimg, 0);
IplImage* pImg1 = cvCreateImage(cvGetSize(imgload), IPL_DEPTH_8U, 3);
IplImage* pImg2 = cvCreateImage(cvGetSize(imgload), IPL_DEPTH_8U, 3);
IplImage* pImg3 = cvCreateImage(cvGetSize(imgload), IPL_DEPTH_8U, 3);
cvSetZero(pImg1);
cvSetZero(pImg2);
cvSetZero(pImg3);
cvMerge(Bimg, 0, 0, 0, pImg3);
cvMerge(0, Gimg, 0, 0, pImg2);
cvMerge(0, 0, Rimg, 0, pImg1);
cvShowImage("RGB", imgload);
cvShowImage("R", pImg1);
cvShowImage("G", pImg2);
cvShowImage("B", pImg3);
waitKey(0);
return 0;
}
IplImage* ImgGray = cvCreateImage(cvGetSize(imgload), IPL_DEPTH_8U, 1);
cvCvtColor(pImg1, ImgGray, CV_BGR2GRAY);
本文叙述了如何使用OpenCV将RGB图像转换为单通道的图像,然后对单通道图像进行灰度化,主要用到的函数有cvLoadImage
、cvCreateImage
、cvSetZero
、cvMerge
、cvShowImage
、cvCvtColor
等。