cropping blob

hi all,
i suspect this is obvious but i can’t seem to find the flaw in my math here.
i’ve been playing with the face-tracking example on the forum
http://www.openframeworks.cc/forum/view-…-e+tracking

i am trying to load faces from the vid feed into an RGBA texture. but in my current implementation, as you move around it seems to be cropping from the wrong x,y location in the vid feed

any feedback appreciated:

  
  
ofSetColor(0xffffff);  
		colorImg.draw(0,0); //draw vid feed  
		  
		ofEnableAlphaBlending();  
		ofFill();  
		ofSetColor(0,0,0,200);  
		ofRect(0,0,320,240); //darken vid feed to see what's happening  
		ofDisableAlphaBlending();  
		  
		ofSetColor(0xffffff);  
		int numFace = haarFinder.blobs.size();  
		for(int i = 0; i < numFace; i++){  
		  
			//face tracking:  
			float x = haarFinder.blobs[i].boundingRect.x;  
			float y = haarFinder.blobs[i].boundingRect.y;  
			float w = haarFinder.blobs[i].boundingRect.width;  
			float h = haarFinder.blobs[i].boundingRect.height;  
			  
			float cx = haarFinder.blobs[i].centroid.x;  
			float cy = haarFinder.blobs[i].centroid.y;  
			  
			//crop face:  
			unsigned char subRegion[ (int) (w * h * 4) ];  // R G B A  
			unsigned char * videoPixels = colorImg.getPixels();  
			  
			for (int i = 0; i < w; i++){  
				for (int j = 0; j < h; j++){  
					int mainPixelPos = ((j+x) * 320 + (i+y)) * 3;  
					int subPixlPos = (j * w + i) * 4;  
					subRegion[subPixlPos] = videoPixels[mainPixelPos];   // R  
					subRegion[subPixlPos + 1] = videoPixels[mainPixelPos + 1];  // G  
					subRegion[subPixlPos + 2] = videoPixels[mainPixelPos + 2];  // B  
					subRegion[subPixlPos + 3] = 255; // A  
				}  
			}   
			  
			ofTexture crop;  
			crop.allocate(w,h,GL_RGBA);  
			crop.loadData(subRegion, w, h, GL_RGBA);  
			ofEnableAlphaBlending();  
			crop.draw(cx-(w/2),cy-(h/2)); //draw face  
			ofDisableAlphaBlending();  
			crop.clear();  
	  
		}  
  

looks to me like you’re mixing up x and y here:

  
  
   for (int i = 0; i < w; i++){  
            for (int j = 0; j < h; j++){  
               int mainPixelPos = ((j+x) * 320 + (i+y)) * 3;  
       

should be
mainPixelPos = ((j+y) * 320 + (i+x)) * 3;
and i assume your video is RGB, not RGBA, hence … * 3 and not … * 4?

(i had to stare at this for a while before i figured out what you were doing, though - the convention is to have y in the outer loop and x in the inner, the other way round is a bit confusing.)

:oops: that’s it - thanks damian!