예제 #1
0
ofTexture ofxImageTS::tanB(ofImage image){
    
    ofTexture texture;
    ofPixels copy;
    copy.allocate(image.getWidth(), image.getHeight(), OF_PIXELS_RGB);
    texture.allocate(image);
    float Tan;
    for(int i = 0; i < image.getPixels().size()-3; i += 3){
        Tan = tan(i);
        copy[i] =  image.getPixels()[i];
        copy[i+1] = image.getPixels()[i+1];
        copy[i+2] = Tan * image.getPixels()[i+2];
    }
    texture.loadData(copy);
    return texture;
}
예제 #2
0
ofTexture ofxImageTS::noise(ofImage image, float mix) {
    
    float avg;
    ofTexture texture;
    ofPixels copy;
    copy.allocate(image.getWidth(), image.getHeight(), OF_PIXELS_RGB);
    texture.allocate(image);
    for(int i = 0; i < image.getPixels().size()-3; i += 3){
        avg = (image.getPixels()[i] + image.getPixels()[i+1] + image.getPixels()[i+2])/3.0f;
        copy[i] = avg * (125 - avg) * mix;
        copy[i+1] = avg * (125 - avg) * mix;
        copy[i+2] = avg * (125 - avg) * mix;
    }
    texture.loadData(copy);
    return texture;
}
예제 #3
0
//--------------------------------------------------------------
ofImage ofxContrast::setBrightnessAndContrast(ofImage& _img, float brightnessAmount, float contrastAmount){
    ofxCvColorImage cvimg;
    cvimg.allocate(_img.width, _img.height);
    cvimg.setFromPixels(_img.getPixels(), _img.width, _img.height);
    
	float brightnessVal = MAX(-127, MIN(127, brightnessAmount));
	float contrastVal = MAX(-127, MIN(127, contrastAmount));
	
	unsigned char data[ 256 ];
	CvMat * matrix;
	double delta, a, b;
	
	matrix = cvCreateMatHeader( 1, 256, CV_8UC1 );
    cvSetData( matrix, data, 0 );
	
	if ( contrastVal>0 ) {
        delta = (127.0f*contrastVal) / 128.0f;
        a = 255.0f / ( 255.0f-(delta*2.0f) );
        b = a * (brightnessVal-delta);
    }
    else {
		delta = (-128.0f*contrastVal) / 128.0f;
		a = ( 256.0f-(delta*2.0f) ) / 255.0f;
		b = ( a*brightnessVal )+delta;
    }
	
	for( int i=0; i<256; i++ ) {
		int value = cvRound( (a*i)+b );
		data[i]	= (unsigned char) min( max(0,value), 255 );
	}
	
    cvLUT( cvimg.getCvImage(), cvimg.getCvImage(), matrix );
	cvReleaseMat( &matrix );
    
    ofImage ofimg;
    ofimg.allocate(_img.width, _img.height, OF_IMAGE_COLOR);
    ofimg.setFromPixels(cvimg.getPixels(), _img.width, _img.height, OF_IMAGE_COLOR);
	return ofimg;
}
예제 #4
0
//--------------------------------------------------------------
void testApp::setup(){
	ofBackground(255, 255, 255);
	ofSetVerticalSync(true);
	ofSetFrameRate(60);
	
	ballImage.loadImage("ball.png");
	
	width = ofGetWidth();
	height = ofGetHeight();
	
	//	physics.verbose = true;			// dump activity to log
	physics.setGravity(ofPoint(0, GRAVITY, 0));
	
	// set world dimensions, not essential, but speeds up collision
	physics.setWorldSize(ofPoint(-width/2, -height, -width/2), ofPoint(width/2, height, width/2));
	physics.setSectorCount(SECTOR_COUNT);
    physics.setDrag(0.97f);
	physics.setDrag(1);		// FIXTHIS
	physics.enableCollision();
	
	initScene();
	
	// setup lighting
	GLfloat mat_shininess[] = { 50.0 };
	GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat light_position[] = { 0, height/2, 0.0, 0.0 };
	glShadeModel(GL_SMOOTH);
	
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_LIGHT0);
	
	// enable back-face culling (so we can see through the walls)
	glCullFace(GL_BACK);
	glEnable(GL_CULL_FACE);
}
예제 #5
0
bool ofxEpilog::send(ofImage img, int w_mm, int h_mm)
{
    //
    // TODO([email protected]): finish implementation
    //
    
    if(!img.isAllocated())
        return false;
    
    if(w_mm <= 0 || h_mm <= 0)
        return false;

    ofLog(OF_LOG_WARNING) << "bool ofxEpilog::send(ofImage img, int w_mm, int h_mm) is not implemented yet.";
    return false;
    
    //
    // dummy implementation
    //
    ofBuffer buffer;
    buffer.append(createPayloadHeader(getMachineProfile(), getOutputConfig()));
    
    // create raster part
    buffer.append(createPayloadRasterBody(img, getOutputConfig()));
    
    // end raster part regardless of it's empty
    buffer.append(PCL_RASTER_END);
    
    // begin HPGL commands (vector part)
    buffer.append(HPGL_START);
    buffer.append(HPGL_VECTOR_INIT + HPGL_CMD_DELIMITER);
    
    if(!keep_alive)
        buffer.append(createPayloadFooter()); // end the session
    
    if(pjl_file.get() != NULL)
        pjl_file->writeFromBuffer(buffer);
    
    return tcp_client.sendRaw(buffer);
}
예제 #6
0
void ForbiddenPlanet::drawContours(float contrast, ofImage img,float sizeMultX,float sizeMultY, ofRectangle faceBoundingBox)
{
    
    
    ofxCvGrayscaleImage faceImg;
    
    faceImg.setFromPixels(img.getPixelsRef());
    
    faceImg.brightnessContrast(0.05f,contrast);
    
    contourFinder.findContours(faceImg, 5, (340*240), 45, true);
    ofSetColor(200,0,50);
    
    for (int i = 0; i < contourFinder.nBlobs; i++){
        
        float lastX = 0;
        float lastY = 0;
        
        glBegin(GL_LINES);
        for(int j =0; j < contourFinder.blobs[i].nPts;j++)
        {
            float x = FACE_TARGET_POS_X - (faceImg.width*sizeMultX)/2+ contourFinder.blobs[i].pts[j].x * sizeMultX;
            float y = FACE_TARGET_POS_Y - (faceImg.height*sizeMultY)/2 + contourFinder.blobs[i].pts[j].y * sizeMultY;
            
            if(ofDist(lastX,lastY,x,y) < 25.0 && (int)lastX != 0 && (int)lastY != 0)
            {
                glVertex2f(x,y);
                glVertex2f(lastX,lastY);
            }
            lastX = x;
            lastY = y;
            //ofCurveVertex( contourFinder.blobs[i].pts[j].x, contourFinder.blobs[i].pts[j].y);
            
        }
        glEnd();
    }
}
예제 #7
0
void ParticleTrace :: drawSampleImage ( ofImage& img )
{
	int w = sampleGridW;
	int h = sampleGridH;
	int b = img.bpp / 8;
	
	unsigned char* pixels = img.getPixels();	
	
	for( int y=0; y<img.height; y++ )
	{
		for( int x=0; x<img.width; x++ )
		{
			int p = ( y * img.width + x ) * b;
			
			if( img.type == OF_IMAGE_GRAYSCALE )
			{
				ofSetColor( pixels[ p ], pixels[ p ], pixels[ p ] );
			}
			else if( img.type == OF_IMAGE_COLOR )
			{
				ofSetColor( pixels[ p + 0 ], pixels[ p + 1 ], pixels[ p + 2 ] );
			}
			else if( img.type == OF_IMAGE_COLOR_ALPHA )
			{
				ofSetColor( pixels[ p + 0 ], pixels[ p + 1 ], pixels[ p + 2 ], pixels[ p + 3 ] );
			}

			ofFill();
			ofRect( x * w, y * h, w, h );
			
			ofNoFill();
			ofSetColor( 100, 100, 100 );
			ofRect( x * w, y * h, w, h );
		}
	}
}
예제 #8
0
void faceColorToTexture(ofMesh& mesh, ofImage& image)
{
	vector<ofFloatColor> &color = mesh.getColors();
	int num_face = color.size() / 3;
	
	int tex_size = ofNextPow2(ceil(sqrt(num_face)));
	
	bool arb = ofGetUsingArbTex();
	ofDisableArbTex();
	image.allocate(tex_size, tex_size, OF_IMAGE_COLOR);
	if (arb) ofEnableArbTex();
	
	mesh.clearTexCoords();
	
	image.getPixelsRef().set(0);
	
	float texel_size = (1. / image.getWidth()) * 0.5;
	
	for (int i = 0; i < num_face; i++)
	{
		int u = (i % tex_size);
		int v = (i / tex_size);
		
		ofColor c = color[i * 3];
		
		image.setColor(u, v, c);
		
		float uu = (float)u / image.getWidth() + texel_size;
		float vv = (float)v / image.getHeight() + texel_size;
		
		mesh.addTexCoord(ofVec2f(uu, vv));
		mesh.addTexCoord(ofVec2f(uu, vv));
		mesh.addTexCoord(ofVec2f(uu, vv));
	}
	
	image.update();
	mesh.clearColors();
}
예제 #9
0
ofTexture ofxImageTS::dualist(ofImage image, ofColor base, ofColor top) {
    int avg;
    ofTexture texture;
    ofPixels copy;
    copy.allocate(image.getWidth(), image.getHeight(), OF_PIXELS_RGB);
    texture.allocate(image);
    for(int i = 0; i < image.getPixels().size()-3; i += 3){
        avg = (image.getPixels()[i] + image.getPixels()[i+1] + image.getPixels()[i+2])/3;
        if(avg < 128) {
            copy[i] = base.r;
            copy[i+1] = base.g;
            copy[i+2] = base.b;
        }
        else {
            copy[i] = top.r;
            copy[i+1] = top.g;
            copy[i+2] = top.b;
        }
    }
    texture.loadData(copy);
    return texture;
}
예제 #10
0
ofTexture ofxImageTS::whiteBlack(ofImage image) {
    
    int avg;
    ofTexture texture;
    ofPixels copy;
    copy.allocate(image.getWidth(), image.getHeight(), OF_PIXELS_RGB);
    texture.allocate(image);
    for(int i = 0; i < image.getPixels().size()-3; i += 3){
        avg = (image.getPixels()[i] + image.getPixels()[i+1] + image.getPixels()[i+2])/3;
        if(avg >= 128) {
            copy[i] = 254;
            copy[i+1] = 254;
            copy[i+2] = 254;
        }
        else {
            copy[i] = 0;
            copy[i+1] = 0;
            copy[i+2] = 0;
        }
    }
    texture.loadData(copy);
    return texture;
}
예제 #11
0
//--------------------------------------------------------------
void testApp::draw()
{
	if (image.isAllocated())
		image.draw(0, 0);
}
예제 #12
0
void loadImageRaw(string path, int w, int h, ofImage &img) {
    ofFile file(path);
    img.setFromPixels((unsigned char*)file.readToBuffer().getData(), w, h, OF_IMAGE_COLOR);
}
예제 #13
0
void ofApp::show_back() {
    img.setFromPixels(back, width, height, OF_IMAGE_COLOR);
    img.draw(0, height);
}
예제 #14
0
void ofApp::show_bgs() {
    memcpy(tmp, show, image_size);
    bgs(tmp);
    img.setFromPixels(tmp, width, height, OF_IMAGE_COLOR);
    img.draw(0, 0);
}
void ofxOpticalFlowLK :: update	( ofImage& source )
{
	update( source.getPixels(), source.width, source.height, source.type );
}
예제 #16
0
	void setup(){
		deep.load("dog.jpg","../../../networks/jetpac.ntwk");
		img.load("dog.jpg");
	}
예제 #17
0
파일: ofxImGui.cpp 프로젝트: 4ker/ofxImGui
GLuint ofxImGui::loadImage(ofImage& image)
{
    if(!engine) return -1;
    return loadPixels(image.getPixels());
}
예제 #18
0
void ofxVectorField::setFromImage(ofImage & image) {

    int imgW = image.width;
    int imgH = image.height;
    int imgPixelCount = imgW * imgH;

    if( !bIsAllocated) {

        printf("ofxVectorField not allocated -- allocating automatically using default spacing\n");
        setup(imgW, imgH, OFX_VECFIELD_DEFALT_SPACING);
    }

    // storage for brightness
    unsigned char * imagePixels = image.getPixels();
    unsigned char brightness[imgPixelCount];

    if( image.getPixelsRef().getImageType() == OF_IMAGE_GRAYSCALE) {

        for(int x=0; x<imgW; x++) {
            for(int y=0; y<imgH; y++) {

                int srcPos = y * imgW + x;

                unsigned char b = imagePixels[srcPos];

                brightness[srcPos] = b;
            }
        }

    } else {

        // convert RGB to luma
        unsigned char * imagePixels = image.getPixels();
        unsigned char brightness[imgPixelCount];
        int bpp = image.getPixelsRef().getBytesPerPixel();

        for(int x=0; x<imgW; x++) {
            for(int y=0; y<imgH; y++) {

                int dstPos = y * imgW + x;
                int srcPos = dstPos * 3;

                unsigned char r = imagePixels[srcPos];
                unsigned char g = imagePixels[srcPos+1];
                unsigned char b = imagePixels[srcPos+2];

                brightness[dstPos] = ( r * 0.299) + (.587 * g) + (.114 * b);
            }
        }
    }

    // detetermine the vector at each position in the image

    for(int x=1; x<width-1; x++) {
        for(int y=1; y<height-1; y++) {

            int vecPos = y * width + x;
            char areaPixels[9];

            // loop through the area pixels
            for(int i=-1; i<=1; i++) {
                for(int j=-1; j<=1; j++) {

                    // determine where to read from in the area (not optimized)
                    int readPos = ((y + j) * spacing * imgW + (x + i)*spacing) * 3;

                    unsigned char R = imagePixels[readPos];
                    unsigned char G = imagePixels[readPos+1];
                    unsigned char B = imagePixels[readPos+2];

                    char BR = (0.299 * R) + (.587 * G) + (.114 * B);

                    int writePos = (j+1) * 3 + (i + 1);

                    areaPixels[writePos] = BR;
                }
            }

            float dX = (areaPixels[0] + areaPixels[3] + areaPixels[6])/3 - (areaPixels[2] + areaPixels[5] + areaPixels[8])/3;
            float dY = (areaPixels[0] + areaPixels[1] + areaPixels[2])/3 - (areaPixels[6] + areaPixels[7] + areaPixels[8])/3;

            vectorField[vecPos].x = dY;
            vectorField[vecPos].y = dX;
        }
    }

    // copy pixels to the top and bottom

    for(int x=0; x<width; x++) {

        int dstPos = x;
        int srcPos = x + width;

        vectorField[dstPos].x = vectorField[srcPos].x;
        vectorField[dstPos].y = vectorField[srcPos].y;

        dstPos = x + (height-1)*width;
        srcPos = x + (height-2)*width;

        vectorField[dstPos].x = vectorField[srcPos].x;
        vectorField[dstPos].y = vectorField[srcPos].y;
    }

    // copy pixels to the left and right sides

    for(int y=0; y<height; y++) {

        int dstPos = y * width;
        int srcPos = y * width + 1;

        vectorField[dstPos].x = vectorField[srcPos].x;
        vectorField[dstPos].y = vectorField[srcPos].y;

        dstPos = y * width + width-1;
        srcPos = y * width + width-2;

        vectorField[dstPos].x = vectorField[srcPos].x;
        vectorField[dstPos].y = vectorField[srcPos].y;
    }

    // copy pixels to the corners

    // top left
    vectorField[0].x = vectorField[width+1].x;
    vectorField[0].y = vectorField[width+1].y;

    // top right
    vectorField[width-1].x = vectorField[width+width-2].x;
    vectorField[width-1].y = vectorField[width+width-2].y;

    // bottom right
    vectorField[(height-1)*width+width-1].x = vectorField[(height-2)*width+width-2].x;
    vectorField[(height-1)*width+width-1].y = vectorField[(height-2)*width+width-2].y;

    // bottom left
    vectorField[(height-1)*width].x = vectorField[(height-2)*width+1].x;
    vectorField[(height-1)*width].y = vectorField[(height-2)*width+1].y;
}
예제 #19
0
void ofxCorrectPerspective::Rectify_Image(ofImage & my_image,double focal_length,double sensor_width, double & alpha, double & beta){
    resize_image=1; //To Enable or Disable Resize

	//Rescale Image to be Max in 1000px
	if (resize_image && max(my_image.width,my_image.height)>1000)
    {
        float s=float(1000)/max(my_image.width,my_image.height);
        my_image.resize(floor(my_image.width*s),floor(my_image.height*s));
    }

    float f=focal_length*(float)max(my_image.width,my_image.height)/sensor_width;
    K.set(f,0.0,0.0,0.0,f,0.0,0.0,0.0,1.0);
    center.set(double(my_image.width)/2.0,double(my_image.height)/2.0);
    if (talk) cout << K;

    my_img_gray=my_image;
    my_img_gray.setImageType(OF_IMAGE_GRAYSCALE);

    //Converting Image to Image Double//
    image_double dub_image;
    ntuple_list lsd_out;
    unsigned int w=my_img_gray.width;
    unsigned int h=my_img_gray.height;
    unsigned char * imgP=my_img_gray.getPixels();

    // LSD parameters start
    double scale = 0.8;       // Scale the image by Gaussian filter to 'scale'.
    double sigma_scale = 0.6; // Sigma for Gaussian filter is computed as sigma = sigma_scale/scale.
    double quant = 2.0;       // Bound to the quantization error on the gradient norm.
    double ang_th = 22.5;     // Gradient angle tolerance in degrees.
    double eps = 0.0;         // Detection threshold, -log10(NFA).
    double density_th = 0.7;  // Minimal density of region points in rectangle.
    int n_bins = 1024;        // Number of bins in pseudo-ordering of gradient modulus.
    double max_grad = 255.0;  // Gradient modulus in the highest bin. The default value corresponds to the highest
                              // gradient modulus on images with gray levels in [0,255].
    // LSD parameters end

    bool verbose=0;

    dub_image = new_image_double(w,h);
    double px=0;
    cout << "\n--------\nInput data being written to image buffer \n";
    for(int j=0;j<(w*h);j++){
        px=imgP[j];
        dub_image->data[j] = px;
        if (verbose){
            cout << " " << dub_image->data[j];
        }
    }
    // Call LSD //
    lsd_out = LineSegmentDetection( dub_image, scale, sigma_scale, quant, ang_th, eps,
                               density_th, n_bins, max_grad, NULL );
    cout << "LSD has done it's thing!\n";
    if (talk) cout << "Number of Lines: "<< lsd_out->size << "Number of Dimensions: " << lsd_out->dim << "\n";

    if (verbose)
    {
    cout << "LSD Values: " << lsd_out->values[0] << " " << lsd_out->values[1] <<" " <<  lsd_out->values[2] <<" " <<  lsd_out->values[3] <<" " <<  lsd_out->values[4] <<" " <<  lsd_out->values[5] << "\n";
    }

    //Sorting in (Value, Index) pairs
        //http://stackoverflow.com/questions/1577475/c-sorting-and-keeping-track-of-indexes
    std::vector<mypair> line_lengths;
    double sqd_distance;

    mesh.setMode(OF_PRIMITIVE_LINES);
    mesh.enableColors();

    ofVec3f first(0.0,0.0,0.0);
    ofVec3f second(0.0,0.0,0.0);
    double x1,x2,y1,y2;

    for(int j=0;j<(lsd_out->size*lsd_out->dim);j=j+5){
        x1=lsd_out->values[j];
        y1=lsd_out->values[j+1];
        x2=lsd_out->values[j+2];
        y2=lsd_out->values[j+3];
        sqd_distance=(x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);

        line_lengths.push_back(make_pair(sqd_distance,j));

        //To Draw as Primitive Lines//
        /*first.set(x2,y2,0.0);
        second.set(x1,y1,0.0);
        mesh.addVertex(first);
        mesh.addColor(ofFloatColor(1.0, 0.0, 0.0));
        mesh.addVertex(second);
        mesh.addColor(ofFloatColor(1.0, 0.0, 0.0));*/
        //Lines Added, will be drawn//
       }
    sort(line_lengths.begin(),line_lengths.end());
    reverse(line_lengths.begin(), line_lengths.end());

    if (talk) cout << line_lengths[0].first << " " << line_lengths[0].second << " " << line_lengths[1].first << " " << line_lengths[1].second << "\n";

    unsigned int maxlines=700;
    unsigned int no_of_lines=min(lsd_out->size,maxlines);
    //Store these Lines pairs in a Matrix, in descending order of Distance
    cout << "Number of Lines: " << no_of_lines << "\n";
    L.resize(4);
    for (int i = 0; i < 4; ++i){
        L[i].resize(no_of_lines);
    }

    for (int j=0; j<no_of_lines; j++){
        L[0][j] = lsd_out->values[line_lengths[j].second];
        L[1][j] = lsd_out->values[(line_lengths[j].second)+1];
        L[2][j] = lsd_out->values[(line_lengths[j].second)+2];
        L[3][j] = lsd_out->values[(line_lengths[j].second)+3];
    }

    //LINE EXTENSION
    double extension_fac=.15; //For one side of the line
    double line_length;
    double line_gradient;
    double rise_angle;
    double delta_x;
    double delta_y;
    for (int j=0; j<no_of_lines; j++){
        x1=L[0][j];
        y1=L[1][j];
        x2=L[2][j];
        y2=L[3][j];
        line_length=sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
        line_gradient=(y2-y1)/(x2-x1);
        rise_angle=atan(abs(line_gradient));
        delta_x=cos(rise_angle)*(line_length*extension_fac);
        delta_y=sin(rise_angle)*(line_length*extension_fac);
        if (line_gradient<0)
        {
            if (x1>x2)
            {
                L[0][j]+=delta_x;
                L[1][j]-=delta_y;
                L[2][j]-=delta_x;
                L[3][j]+=delta_y;
            }
            else
            {
                L[2][j]+=delta_x;
                L[3][j]-=delta_y;
                L[0][j]-=delta_x;
                L[1][j]+=delta_y;
            }
        }
        else
        {
            if (x1>x2)
            {
                L[0][j]+=delta_x;
                L[1][j]+=delta_y;
                L[2][j]-=delta_x;
                L[3][j]-=delta_y;
            }
            else
            {
                L[2][j]+=delta_x;
                L[3][j]+=delta_y;
                L[0][j]-=delta_x;
                L[1][j]-=delta_y;
            }
        }
        x1=L[0][j];
        y1=L[1][j];
        x2=L[2][j];
        y2=L[3][j];

        L[2][j]-=center.x; //Move Origin to the Principle Point
        L[3][j]-=center.y;
        L[0][j]-=center.x;
        L[1][j]-=center.y;
    }

    //Finding ADJACENT Lines//
    bool adjflag=1;
    std::vector<int> ar; //To hold Adjacent Row Values
    std::vector<int> ac; //To hold Adjacent Column Values

    if (adjflag)
    {
    double athreshadj=10;

    std::vector<std::vector<bool> > adj; //Line x Line Inf Matrix Initialization, adj
                                            //Not Used at the Moment.
    adj.resize(no_of_lines); //Height
    for (int i = 0; i < no_of_lines; ++i){
        adj[i].resize(no_of_lines);
        for (int j = 0; j < no_of_lines; ++j){
            //adj[i][j]=1.0/0.0;
            adj[i][j]=0;
        }
    }

    ofVec2f v1,v2,x;
    athreshadj=abs(cos((athreshadj*PI)/180.0));
    for (int i = 0; i < no_of_lines; ++i){
        for (int j = i+1; j < no_of_lines; ++j){ //Everyline in-front
            v1.set(L[0][i]-L[2][i],L[1][i]-L[3][i]);
            v2.set(L[0][j]-L[2][j],L[1][j]-L[3][j]);
            v1.normalize();
            v2.normalize();
            if (abs(v1.dot(v2))<athreshadj) //acos(v1.dot(v2)) //So Angle is greater!
            {
               x=solveLinearSys(L[0][i]-L[2][i],-L[0][j]+L[2][j],L[1][i]-L[3][i],-L[1][j]+L[3][j],-L[2][i]+L[2][j],-L[3][i]+L[3][j]);
               if (not isinf(x.x) and not isinf(x.y))
               {
                   adj[i][j]=(x.x>=-DBL_EPSILON) && (x.x<=1+DBL_EPSILON) && (x.y>=-DBL_EPSILON) && (x.y<=1+DBL_EPSILON);
                   adj[j][i]=adj[i][j] || adj[j][i];
                   if (adj[i][j])
                   {
                      ar.push_back(i);
                      ac.push_back(j);
                   }
               }
            }
        }
    }
    }
    else
    {
    for (int i = 0; i < no_of_lines; ++i){ // nC2 Pairs
        for (int j = i+1; j < no_of_lines; ++j){
            ar.push_back(i);
            ac.push_back(j);
            }
        }
    }
    cout << "No. of Pairs: "<< ac.size() << "\n";

    //Adjacent Matrix FOUND//

    //Convert Line Segments to vector format for Rectification//
    std::vector<std::vector<double> > L_vec; //Line's Vector Form
    L_vec.resize(3); //Height
    for (int i = 0; i < 3; ++i){
        L_vec[i].resize(no_of_lines);
    }

    Mat A(3,3,CV_32F);
    Mat s, u, vt;
    A.at<float>(0,2)=1;
    A.at<float>(1,2)=1;
    A.at<float>(2,0)=0;
    A.at<float>(2,1)=0;
    A.at<float>(2,2)=0;
    for (int i = 0; i < no_of_lines; ++i){
            A.at<float>(0,0)=L[0][i];
            A.at<float>(0,1)=L[1][i];
            A.at<float>(1,0)=L[2][i];
            A.at<float>(1,1)=L[3][i];
            SVD::compute(A, s, u, vt);  //YY=U*S*V'
            vt=vt.t();
            vt.col(0)=vt.col(0)*-1;

            L_vec[0][i]=vt.at<float>(0,2);
            L_vec[1][i]=vt.at<float>(1,2);
            L_vec[2][i]=vt.at<float>(2,2);
    }

    //RANSAC//
    //int PairsC2=(ac.size()-1)*ac.size()/2;
    unsigned int maxTrials=400; //min(PairsC2*5,200);
    cout << "RANSAC: No. of Trials = " << maxTrials << endl;
    unsigned int trialcount=0;
    unsigned int r1, r2, r3, r4;
    unsigned int r_ind1, r_ind2;
    column_vector modelX;
    std::vector<int> arIn; //To hold Adjacent Row Values
    std::vector<int> acIn; //To hold Adjacent Column Values
    double thresh=0.001;

    std::vector<int> Best_arIn; //Best Adjacent Row Values
    std::vector<int> Best_acIn; //Best Adjacent Column Values
    unsigned int Bestscore=0; //Number of Inliers
    unsigned int score=0;
    column_vector Best_modelX;

    while (trialcount<maxTrials)
    {
        arIn.resize(0);
        acIn.resize(0);

        r_ind1=floor(ofRandom(ac.size()));
        r_ind2=floor(ofRandom(ac.size()));
        r1=ar[r_ind1];
        r2=ar[r_ind2];
        r3=ac[r_ind1];
        r4=ac[r_ind2];

        modelX=fitFunc4Lines(L_vec, r1, r2, r3, r4, f); //Fitting Function
        distFunc(L_vec,modelX,f,thresh,arIn,acIn);
        score=arIn.size();
        //score=distFunc2(L_vec,modelX,f,thresh,arIn,acIn, ar, ac); //Distance Function

        if (Bestscore<score)
        {
            Bestscore=score;
            Best_arIn=arIn;
            Best_acIn=acIn;
            Best_modelX=modelX;
            if (talk) cout << "No. of Inliers: "<< Bestscore<<endl;
        }

        trialcount++;
    }
    cout << "RANSAC Done." << endl;
    cout << "No. of Inliers: "<< Bestscore<<endl;

    column_vector solution=Best_modelX;

    if (talk) cout << "cost_function solution:\n" << Best_modelX << endl;
    cout << "cost_function solution:\n" << solution << endl;

    alpha=solution(0);
    beta=solution(1);

    //APPLYING HOMOGRAPHY for this SOLUTION//
    ofMatrix4x4 R=ofMatrix4x4::newRotationMatrix(solution(0)*180.0/PI, ofVec3f(-1, 0, 0), solution(1)*180.0/PI, ofVec3f(0, -1, 0), 0, ofVec3f(0, 0, -1));
    double m[3][3] = {{R(0,0), R(0,1), R(0,2)}, {R(1,0), R(1,1), R(1,2)}, {R(2,0), R(2,1), R(2,2)}};
    cv::Mat R_mat = cv::Mat(3, 3, CV_64F, m);

    cv::Mat K_mat = (cv::Mat_<double>(3,3)<< f,0.0,0.0,0.0,f,0.0,0.0,0.0,1.0);
    cv::Mat K_c= K_mat.clone();
    K_c=K_c.inv();

    cv::Mat C = (cv::Mat_<double>(3,3)<< 1,0,-center.x,0,1,-center.y,0,0,1);
    cv::Mat H=K_mat*R_mat*K_c*C;

    //Calclating Resultant Translation and Scale
    std::vector<Point2f> Ref_c;
    std::vector<Point2f> Ref_c_out;
    Ref_c.resize(4);
    Ref_c_out.resize(4);
    Ref_c[0].x=0;
    Ref_c[0].y=0;
    Ref_c[1].x=double(my_image.width);
    Ref_c[1].y=0;
    Ref_c[2].x=double(my_image.width);
    Ref_c[2].y=double(my_image.height);
    Ref_c[3].x=0;
    Ref_c[3].y=double(my_image.height);

    perspectiveTransform(Ref_c, Ref_c_out, H);

    if (talk) cout << "Ref Out New: " << Ref_c_out << endl;

    //Scalling:
    double scale_fac=abs((max(Ref_c_out[1].x,Ref_c_out[2].x)-min(Ref_c_out[0].x,Ref_c_out[3].x))/my_image.width); //Based on Length
    if (talk) cout << "Scale Factor: " << scale_fac << endl;

    Ref_c_out[0].x=Ref_c_out[0].x/scale_fac;
    Ref_c_out[0].y=Ref_c_out[0].y/scale_fac;
    Ref_c_out[1].x=Ref_c_out[1].x/scale_fac;
    Ref_c_out[1].y=Ref_c_out[1].y/scale_fac;
    Ref_c_out[2].x=Ref_c_out[2].x/scale_fac;
    Ref_c_out[2].y=Ref_c_out[2].y/scale_fac;
    Ref_c_out[3].x=Ref_c_out[3].x/scale_fac;
    Ref_c_out[3].y=Ref_c_out[3].y/scale_fac;

    Ref_c_out[1].x=Ref_c_out[1].x-Ref_c_out[0].x;
    Ref_c_out[1].y=Ref_c_out[1].y-Ref_c_out[0].y;
    Ref_c_out[2].x=Ref_c_out[2].x-Ref_c_out[0].x;
    Ref_c_out[2].y=Ref_c_out[2].y-Ref_c_out[0].y;
    Ref_c_out[3].x=Ref_c_out[3].x-Ref_c_out[0].x;
    Ref_c_out[3].y=Ref_c_out[3].y-Ref_c_out[0].y;
    Ref_c_out[0].x=Ref_c_out[0].x-Ref_c_out[0].x;
    Ref_c_out[0].y=Ref_c_out[0].y-Ref_c_out[0].y;
    if (talk) (cout << "Ref Out New: " << Ref_c_out << endl);

    H = getPerspectiveTransform( Ref_c, Ref_c_out ); //For the Translated/Scalled Image

    //Applying Homography//
    Mat src_img(cv:: Size (my_image.width, my_image.height),CV_8UC3,my_image.getPixels()); //OF to OpenCV
    cv::Mat dst_img;
	dst_img.create(src_img.size(), src_img.type());

	cv::warpPerspective(src_img, dst_img, H, src_img.size(), cv::INTER_LINEAR);

    //OpenCV to OF
    my_image.setFromPixels((unsigned char *) IplImage(dst_img). imageData,dst_img.size().width, dst_img.size().height,OF_IMAGE_COLOR);

    cout << endl <<"Press any key to Save Output Image." << endl;
}
//------------------------------------------------------------------------------
void ofxIpVideoServerRoute::pushFrame(ofImage& img) {
    pushFrame(img.getPixelsRef());
}
예제 #21
0
//--------------------------------------------------------------
void ofApp::draw(){
    
    //Merci Ludo pour ton aide
	
    currentFrame = vidGrabber.getPixelsRef();
    currentFrameCopy.allocate(currentFrame.getWidth(), currentFrame.getHeight(), OF_IMAGE_GRAYSCALE);


    for(int x=0 ; x < 256 ; x++) {
        histogram[x] = 0;
    }

    
    for (int i = 0; i < camWidth; i++){
        for (int j = 0; j < camHeight; j++){
            int lightness = currentFrame.getColor(i,j).getLightness();
            histogram[lightness] = histogram[lightness]+1;
            ofColor pixel;
            pixel.set(lightness, lightness, lightness);
            currentFrame.setColor(i, j, pixel);

        }
    }
    
    ofSetHexColor(0xffffff);
    currentFrame.reloadTexture();
    currentFrame.draw(0,0);
    
    ofFill();
	ofSetHexColor(0x000000);
	ofSetPolyMode(OF_POLY_WINDING_ODD);
    
    ofLine(770, 400, 770, 400-255);
    ofLine(770, 400, 770+255, 400);
    
    histogramMax = 0;
    maxIndex = 0;
    for(int x = 0 ; x < 256 ; x++) {
        if (histogram[x]>histogramMax) {
            histogramMax = histogram[x];
            maxIndex = x;
            }
        histogram[x] = histogram[x]/100;
        //cout << x << " : " << histogram[x] << "\n";
        ofLine(x+770, 400-histogram[x], x+770, 400);
    }

    ofSetColor(255,0,0);
    ofLine(maxIndex+770, 400-histogram[maxIndex], maxIndex+770, 400);
    
    ofSetColor(0);
	ofDrawBitmapString("Histogram : ", 770, 100);
	ofDrawBitmapString("0                             255 ", 770, 415);
	ofDrawBitmapString("0", 755, 400);
	ofDrawBitmapString("???", 773, 150);

    threshold = 128;
    for(int y = 0; y < camHeight; y++) {
        for(int x = 0; x < camWidth; x++) {
            ofColor cur = currentFrame.getColor(x, y);
            int lightness = cur.getLightness();
            ofColor pixel;
            if (lightness<threshold) pixel.set(0, 0, 0);
            else pixel.set(255, 255, 255);
            currentFrameCopy.setColor(x, y, pixel);
        }
    }
    
    ofSetColor(255);
    currentFrameCopy.reloadTexture();
    currentFrameCopy.draw(0, 480);


    

}
예제 #22
0
void ckvdTileGrabber::setColorFromFrame(ofImage& frame)
{
    _fixture.setVideoRect(x - width/2, y - height/2, width, height);
    _fixture.setSourceData(frame.getPixels(), frame.width, frame.height, 3);
}
예제 #23
0
void ckvdSingleColorGrabber::setColorFromFrame(ofImage& frame)
{
    _color = frame.getColor(x, y);
    _fixture.set_rgb(_color.r, _color.g, _color.b);
}
예제 #24
0
//--------------------------------------------
void ofCairoRenderer::draw(const ofImage & img, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh) const{
	draw(img.getPixels(),x,y,z,w,h,sx,sy,sw,sh);
}
//--------------------------------------------
void ofCairoRenderer::draw(ofImage & img, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh){
	ofPixelsRef raw = img.getPixelsRef();
	bool shouldCrop = sx != 0 || sy != 0 || sw != w || sh != h;
	ofPixels cropped;
	if(shouldCrop) {
		cropped.allocate(sw, sh, raw.getImageType());
		raw.cropTo(cropped, sx, sy, sw, sh);
	}
	ofPixelsRef pix = shouldCrop ? cropped : raw;

	pushMatrix();
	translate(x,y,z);
	scale(w/pix.getWidth(),h/pix.getHeight());
	cairo_surface_t *image;
	int stride=0;
	int picsize = pix.getWidth()* pix.getHeight();
	unsigned char *imgPix = pix.getPixels();

	static vector<unsigned char> swapPixels;

	switch(pix.getImageType()){
	case OF_IMAGE_COLOR:
#ifdef TARGET_LITTLE_ENDIAN
		swapPixels.resize(picsize * 4);

		for(int p= 0; p<picsize; p++) {
			swapPixels[p*4] = imgPix[p*3 +2];
			swapPixels[p*4 +1] = imgPix[p*3 +1];
			swapPixels[p*4 +2] = imgPix[p*3];
		}
#else
		swapPixels.resize(picsize * 4);

		for(int p= 0; p<picsize; p++) {
			swapPixels[p*4] = imgPix[p*3];
			swapPixels[p*4 +1] = imgPix[p*3 +1];
			swapPixels[p*4 +2] = imgPix[p*3 +2];
		}
#endif
		stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, pix.getWidth());
		image = cairo_image_surface_create_for_data(&swapPixels[0], CAIRO_FORMAT_RGB24, pix.getWidth(), pix.getHeight(), stride);
		break;
	case OF_IMAGE_COLOR_ALPHA:
#ifdef TARGET_LITTLE_ENDIAN
		swapPixels.resize(picsize * 4);

		for(int p= 0; p<picsize; p++) {
			swapPixels[p*4] = imgPix[p*4+2];
			swapPixels[p*4 +1] = imgPix[p*4+1];
			swapPixels[p*4 +2] = imgPix[p*4];
			swapPixels[p*4 +3] = imgPix[p*4+3];
		}
		stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, pix.getWidth());
		image = cairo_image_surface_create_for_data(&swapPixels[0], CAIRO_FORMAT_ARGB32, pix.getWidth(), pix.getHeight(), stride);
#else
		stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, pix.getWidth());
		image = cairo_image_surface_create_for_data(pix.getPixels(), CAIRO_FORMAT_ARGB32, pix.getWidth(), pix.getHeight(), stride);
#endif
		break;
	case OF_IMAGE_GRAYSCALE:
		swapPixels.resize(picsize * 4);

		for(int p= 0; p<picsize; p++) {
			swapPixels[p*4] = imgPix[p];
			swapPixels[p*4 +1] = imgPix[p];
			swapPixels[p*4 +2] = imgPix[p];
		}
		stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, pix.getWidth());
		image = cairo_image_surface_create_for_data(&swapPixels[0], CAIRO_FORMAT_RGB24, pix.getWidth(), pix.getHeight(), stride);
		break;
	case OF_IMAGE_UNDEFINED:
	default:
		ofLogError("ofCairoRenderer") << "draw(): trying to draw undefined image type " << pix.getImageType();
		popMatrix();
		return;
		break;
	}
	cairo_set_source_surface (cr, image, 0,0);
	cairo_paint (cr);
	cairo_surface_flush(image);
	cairo_surface_destroy (image);
	popMatrix();
}
예제 #26
0
//----------------------------------------------------------
void ofGLRenderer::draw(ofImage & image, float x, float y, float z, float w, float h){
	if(image.isUsingTexture()){
		image.getTextureReference().draw(x,y,z,w,h);
	}
}
//--------------------------------------------------------------
void ofxAEOverlay::setImg(ofImage & i){
    img.clear();
    img.setFromPixels(i.getPixelsRef());
    img.update();
}
예제 #28
0
//--------------------------------------------------------------
void ofApp::draw(){
	ofBackground( 255, 255, 255 );	//Set up white background
	ofSetColor( 255, 255, 255 );	//Set color for image drawing
	image.draw( 0, 0 );				//Draw image
}
예제 #29
0
//--------------------------------------------
void ofCairoRenderer::draw(ofImage & img, float x, float y, float z, float w, float h){
	ofPixelsRef pix = img.getPixelsRef();
	pushMatrix();
	translate(x,y,z);
	scale(w/pix.getWidth(),h/pix.getHeight());
	cairo_surface_t *image;
	int stride=0;
	int picsize = pix.getWidth()* pix.getHeight();
	unsigned char *imgPix = pix.getPixels();

	static vector<unsigned char> swapPixels;

	switch(pix.getImageType()){
	case OF_IMAGE_COLOR:
#ifdef TARGET_LITTLE_ENDIAN
		swapPixels.resize(picsize * 4);

		for(int p= 0; p<picsize; p++) {
			swapPixels[p*4] = imgPix[p*3 +2];
			swapPixels[p*4 +1] = imgPix[p*3 +1];
			swapPixels[p*4 +2] = imgPix[p*3];
		}
#else
		swapPixels.resize(picsize * 4);

		for(int p= 0; p<picsize; p++) {
			swapPixels[p*4] = imgPix[p*3];
			swapPixels[p*4 +1] = imgPix[p*3 +1];
			swapPixels[p*4 +2] = imgPix[p*3 +2];
		}
#endif
		stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, pix.getWidth());
		image = cairo_image_surface_create_for_data(&swapPixels[0], CAIRO_FORMAT_RGB24, pix.getWidth(), pix.getHeight(), stride);
		break;
	case OF_IMAGE_COLOR_ALPHA:
#ifdef TARGET_LITTLE_ENDIAN
		swapPixels.resize(picsize * 4);

		for(int p= 0; p<picsize; p++) {
			swapPixels[p*4] = imgPix[p*4+2];
			swapPixels[p*4 +1] = imgPix[p*4+1];
			swapPixels[p*4 +2] = imgPix[p*4];
			swapPixels[p*4 +3] = imgPix[p*4+3];
		}
		stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, pix.getWidth());
		image = cairo_image_surface_create_for_data(&swapPixels[0], CAIRO_FORMAT_ARGB32, pix.getWidth(), pix.getHeight(), stride);
#else
		stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, pix.getWidth());
		image = cairo_image_surface_create_for_data(pix.getPixels(), CAIRO_FORMAT_ARGB32, pix.getWidth(), pix.getHeight(), stride);
#endif
		break;
	case OF_IMAGE_GRAYSCALE:
		swapPixels.resize(picsize * 4);

		for(int p= 0; p<picsize; p++) {
			swapPixels[p*4] = imgPix[p];
			swapPixels[p*4 +1] = imgPix[p];
			swapPixels[p*4 +2] = imgPix[p];
		}
		stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, pix.getWidth());
		image = cairo_image_surface_create_for_data(&swapPixels[0], CAIRO_FORMAT_RGB24, pix.getWidth(), pix.getHeight(), stride);
		break;
	case OF_IMAGE_UNDEFINED:
		ofLog(OF_LOG_ERROR,"ofCairoRenderer: trying to render undefined type image");
		popMatrix();
		return;
		break;
	}
	cairo_set_source_surface (cr, image, 0,0);
	cairo_paint (cr);
	cairo_surface_flush(image);
	cairo_surface_destroy (image);
	popMatrix();
}
예제 #30
0
void ofApp::show_cut(int play_cnt) {
    memcpy(tmp, recorded + image_size * play_cnt, image_size);
    bgs(tmp);
    img.setFromPixels(tmp, width, height, OF_IMAGE_COLOR);
    img.draw(width, 0);
}