Exemplo n.º 1
0
  void ImageRGB8::toYUY2(Image& dst) const throw(Image::FormatMismatch){
    if (dst.format() != ImageYUY2::FORMAT_YUY2)
      throw Image::FormatMismatch("FORMAT_YUY2 required for dst");
    if ((dst.width % 2 != 0) || (this->width % 2 != 0))
      throw Image::FormatMismatch("src and dst images have to have even number of columns");

    cv::Mat_<cv::Vec3b> ycrcb(dst.height,dst.width,dst.type());//YUV444 previous conversion
    cv::Mat_<cv::Vec2b> yuy2(dst);
    cv::cvtColor(*this,ycrcb,CV_RGB2YCrCb);

    for (int i=0; i < height; i++){
      for (int j=0; j < width; j+=2){//two pixels each loop
	yuy2(i,j)[0] = ycrcb(i,j)[0];//Y0
	yuy2(i,j)[1] = ycrcb(i,j)[2];//U0
	yuy2(i,j+1)[0] = ycrcb(i,j+1)[0];//Y1
	yuy2(i,j+1)[1] = ycrcb(i,j)[1];//V0
      }
    }
  }
Exemplo n.º 2
0
void skinSegAndBackground(const Mat& src, Mat& background, Mat& dst){
	dst.create( src.rows ,src.cols , CV_8U);
	Mat imgYCrCb;

	cvtColor(src, imgYCrCb, CV_BGR2YCrCb);

	vector<Mat> ycrcb( imgYCrCb.channels());
	split(imgYCrCb, ycrcb); 
	int y, cr, cb, x1, y1, value;
	int iRows=src.rows;
	int iCols=src.cols;
	for (int h=0; h< iRows ;h++){              // h:行
		uchar* data_YCrCb0=ycrcb[0].ptr<uchar>(h);
		uchar* data_YCrCb1=ycrcb[1].ptr<uchar>(h);
		uchar* data_YCrCb2=ycrcb[2].ptr<uchar>(h);

		const uchar* d1 = src.ptr<uchar>(h);
		const uchar* d2 = background.ptr<uchar>(h);   

		uchar* data_dst = dst.ptr<uchar>(h);
		for (int w=0; w< iCols ;w++){
			y = data_YCrCb0[w];
			cr = data_YCrCb1[w];
			cb = data_YCrCb2[w];
			cb -= 109;
			cr -= 152;
			x1 = (819*cr-614*cb)/32 + 51;
			y1 = (819*cr+614*cb)/32 + 77;
			x1 = x1*41/1024;
			y1 = y1*73/1024;
			value = x1*x1+y1*y1;

			int temp=(d1[3*w]-d2[3*w])*(d1[3*w]-d2[3*w])+
				(d1[3*w+1]-d2[3*w+1])*(d1[3*w+1]-d2[3*w+1])+
				(d1[3*w+2]-d2[3*w+2])*(d1[3*w+2]-d2[3*w+2]);
			if ( temp> 100)
			{
				if(y<100)	
					data_dst[w] =(value<700) ? 255:0;
				else	
					data_dst[w] =(value<850)? 255:0;		
			}
			else	data_dst[w]= 0;
		}
	}
}
Exemplo n.º 3
0
  void ImageYUY2::toYCRCB(Image& dst) const throw(FormatMismatch){
    if (dst.format() != ImageYCRCB::FORMAT_YCRCB)
      throw Image::FormatMismatch("FORMAT_YCRCB required for dst");
    cv::Mat_<cv::Vec3b> ycrcb(dst);
    cv::Mat_<cv::Vec2b> yuy2(*this);

    for (int i=0; i < height; i++){
      for (int j=0; j < width; j+=2){//two pixels each loop
	ycrcb(i,j)[0] = yuy2(i,j)[0];//Y0<-Y0
	ycrcb(i,j)[1] = yuy2(i,j+1)[1];//V0<-V0
	ycrcb(i,j)[2] = yuy2(i,j)[1];//U0<-U0
	ycrcb(i,j+1)[0] = yuy2(i,j+1)[0];//Y1<-Y1
	ycrcb(i,j+1)[1] = yuy2(i,j+1)[1];//V1<-V0
	ycrcb(i,j+1)[2] = yuy2(i,j)[1];//U1<-U0
      }
    }
  }