opencv,图片遍历

Posted 一样菜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv,图片遍历相关的知识,希望对你有一定的参考价值。

//肤色提取,skinArea为二值化肤色图像
void skinExtract(const Mat &frame, Mat &skinArea)
{
	Mat YCbCr;
	vector<Mat> planes;

	//转换为YCrCb颜色空间
	cvtColor(frame, YCbCr, CV_RGB2YCrCb);
	
	//将多通道图像分离为多个单通道图像
	split(YCbCr, planes);
	skinArea.create(YCbCr.size(), planes[1].type());
	//人的皮肤颜色在YCbCr色度空间的分布范围:100<=Cb<=127, 138<=Cr<=170
	int Cols = frame.cols;
	for (int r = 0; r < frame.rows; r++){
		const uchar* Cb = planes[1].ptr<uchar>(r);
		const uchar* Cr = planes[2].ptr<uchar>(r);
		uchar* data = skinArea.ptr<uchar>(r);
		for (int c = 0; c < Cols; c++)
		{
			if (100 <= Cb[c] && Cb[c] <= 127 && 138 <= Cr[c] && Cr[c] <= 170)
			{
				data[c] = 0;
			}
			else{
				data[c] = 255;
			}
		}

	}

	//膨胀和腐蚀,膨胀可以填补凹洞(将裂缝桥接),腐蚀可以消除细的凸起(“斑点”噪声)
	
	//dilate(skinArea, skinArea, Mat(3, 3, CV_8UC1), Point(-1, -1));
	//erode(skinArea, skinArea, Mat(3, 3, CV_8UC1), Point(-1, -1));
	//blur(skinArea, skinArea, cvSize(2, 2));
	//erode(skinArea, skinArea,cv::Mat());
}

 

//肤色提取
double skinExtract(const IplImage *pFrame)
{
	IplImage *pYCbCr = cvCreateImage(cvGetSize(pFrame), 8, 3);;
	IplImage *pCb = cvCreateImage(cvGetSize(pFrame), 8, 1);
	IplImage *pCr = cvCreateImage(cvGetSize(pFrame), 8, 1);

	cv::vector<IplImage*> planes;

	//转换成YCrCb颜色空间
	cvCvtColor(pFrame, pYCbCr, CV_RGB2YCrCb);
	//把多通道图像分离为单通道图像
	cvSplit(pYCbCr, 0, pCb, pCr, 0);
	//Cb[100,127]  Cr[138,170]
	long area = pYCbCr->height * pYCbCr->width;
	long skin = 0;
	for (int h = 0; h < pYCbCr->height; h++){
		unsigned char *strCb = (unsigned char*)pCb->imageData + h*pCb->widthStep;
		unsigned char *strCr = (unsigned char*)pCr->imageData + h*pCr->widthStep;
		for (int w = 0; w < pYCbCr->width; w++){
			if (strCb[w]>100 && strCb[w]<127 && (int)strCr[w]>138 && (int)strCr[w] < 170){
				skin++;
			}
		}
	}
	
	/*
	cvErode(skinFrame, skinFrame, NULL, 1); //腐蚀  
	//cvDilate(skinFrame, skinFrame, NULL, 2); //膨胀  
	cvSmooth(skinFrame, skinFrame, CV_BLUR, 3, 3, 0, 0);
	//cvShowImage("dst1", skinFrame);
	*/
	cvReleaseImage(&pYCbCr);
	cvReleaseImage(&pCb);
	cvReleaseImage(&pCr);
	return skin*1.0 / area;
}

 

以上是关于opencv,图片遍历的主要内容,如果未能解决你的问题,请参考以下文章

手把手:使用OpenCV进行面部合成— C++ / Python

opencv怎么截取视频图片

如何用opencv提取一张图片的像素矩阵

在 Python 多处理进程中运行较慢的 OpenCV 代码片段

OpenCV中对Mat的遍历访问与赋值

针对opencv的提问!!!浮点型图像像素值范围是多少?