Example #1
0
// main
// 
// This program accepts three arguments: a processing option
// ("blur", "invert", or "ascii"), a PGM file name for input,
// and a text file name for output.  It reads the PGM file and
// creates an output file with either an appropriate PGM (if 
// one of the first two options are given) or a text file (if
// the last option is given).
//
// Right now, only the "ascii" option works.
//
int main(int argc, char **argv) {

  // input and output file "handles"
  FILE *inf, *outf;

  if (argc < 4) {
    
    // whoops! not enough arguments
    fprintf(stderr,"Error: not enough arguments!\n");
    usage(argv[0]);
    return -1;

  } else {

    // open the input (PGM) file
    inf = fopen(argv[2],"r");
    if (inf == NULL) {
      fprintf(stderr,"Error: can't open file '%s' for reading.\n",argv[2]);
      return -1;
    }

    // open the output file
    outf = fopen(argv[3],"w");
    if (outf == NULL) {
      fprintf(stderr,"Error: can't open file '%s' for writing.\n",argv[3]);
      return -1;
    }

    if (strcmp(argv[1],"--blur") == 0) {

      blurImage(inf,outf);

    } else if (strcmp(argv[1],"--invert") == 0) {

      invertImage(inf,outf);
      //
      // write the code that inverts the image
      //  

    } else if (strcmp(argv[1],"--ascii") == 0) {

      echoASCII(inf,outf);
      //
      // change this so that it is given an image array and
      // the outf
      //

    } else {

      fprintf(stderr,"Error: unrecognized option '%s.'\n",argv[1]);
      usage(argv[0]);
      
      // return FAIL
      return -1;
    }

    // close the files
    fclose(inf);
    fclose(outf);

    // return OK
    return 0;
  }
}
Example #2
0
void UIVMPreviewWindow::repaintBGImages()
{
    /* Delete the old images: */
    if (m_pbgImage)
    {
        delete m_pbgImage;
        m_pbgImage = 0;
    }
    if (m_pGlossyImg)
    {
        delete m_pGlossyImg;
        m_pGlossyImg = 0;
    }

    /* Check that there is enough room for our fancy stuff.
     * If not we just draw nothing (the border and the blur radius). */
    QRect cr = contentsRect();
    if (cr.width()  < 41 || cr.height() < 41)
        return;

    QPalette pal = palette();
    m_wRect = cr.adjusted(10, 10, -10, -10);
    m_vRect = m_wRect.adjusted(m_vMargin, m_vMargin, -m_vMargin, -m_vMargin).adjusted(-3, -3, 3, 3);

    /* First draw the shadow. Its a rounded rectangle which get blurred: */
    QImage imageW(cr.size(), QImage::Format_ARGB32);
    QColor bg = pal.color(QPalette::Base);
    bg.setAlpha(0); /* We want blur to transparent _and_ whatever the base color is. */
    imageW.fill(bg.rgba());
    QPainter pW(&imageW);
    pW.setBrush(QColor(30, 30, 30)); /* Dark gray */
    pW.setPen(Qt::NoPen);
    pW.drawRoundedRect(QRect(QPoint(0, 0), cr.size()).adjusted(10, 10, -10, -10), m_vMargin, m_vMargin);
    pW.end();
    /* Blur the rectangle */
    QImage imageO(cr.size(), QImage::Format_ARGB32);
    blurImage(imageW, imageO, 10);
    QPainter pO(&imageO);

    /* Now paint the border with a gradient to get a look of a monitor: */
    QRect rr = QRect(QPoint(0, 0), cr.size()).adjusted(10, 10, -10, -10);
    QLinearGradient lg(0, rr.y(), 0, rr.height());
    QColor base(200, 200, 200); /* light variant */
    // QColor base(80, 80, 80); /* Dark variant */
    lg.setColorAt(0, base);
    lg.setColorAt(0.4, base.darker(300));
    lg.setColorAt(0.5, base.darker(400));
    lg.setColorAt(0.7, base.darker(300));
    lg.setColorAt(1, base);
    pO.setBrush(lg);
    pO.setPen(QPen(base.darker(150), 1));
    pO.drawRoundedRect(rr, m_vMargin, m_vMargin);
    pO.end();

    /* Make a copy of the new bg image: */
    m_pbgImage = new QImage(imageO);

    /* Now the glossy overlay has to be created.
     * Start with defining a nice looking painter path. */
    QRect gRect = QRect(QPoint(0, 0), m_vRect.size());
    QPainterPath glossyPath(QPointF(gRect.x(), gRect.y()));
    glossyPath.lineTo(gRect.x() + gRect.width(), gRect.y());
    glossyPath.lineTo(gRect.x() + gRect.width(), gRect.y() + gRect.height() * 1.0/3.0);
    glossyPath.cubicTo(gRect.x() + gRect.width() / 2.0, gRect.y() + gRect.height() * 1.0/3.0,
                       gRect.x() + gRect.width() / 2.0, gRect.y() + gRect.height() * 2.0/3.0,
                       gRect.x(), gRect.y() + gRect.height() * 2.0/3.0);
    glossyPath.closeSubpath();

    /* Paint the glossy path on a QImage: */
    QImage image(m_vRect.size(), QImage::Format_ARGB32);
    QColor bg1(Qt::white); /* We want blur to transparent _and_ white. */
    bg1.setAlpha(0);
    image.fill(bg1.rgba());
    QPainter painter(&image);
    painter.fillPath(glossyPath, QColor(255, 255, 255, 80));
    painter.end();
    /* Blur the image to get a much more smooth feeling */
    QImage image1(m_vRect.size(), QImage::Format_ARGB32);
    blurImage(image, image1, 7);
    m_pGlossyImg = new QImage(image1);

    /* Repaint: */
    update();
}
Example #3
0
// Compute MOPs descriptors.
void ComputeMOPSDescriptors(CFloatImage &image, FeatureSet &features)
{
	int w = image.Shape().width;  // image width
	int h = image.Shape().height; // image height

	// Create grayscale image used for Harris detection
	CFloatImage grayImage=ConvertToGray(image);

	// Apply a 7x7 gaussian blur to the grayscale image
	CFloatImage blurImage(w,h,1);
	Convolve(grayImage, blurImage, ConvolveKernel_7x7);

	// Transform matrices
	CTransform3x3 xform;
	CTransform3x3 trans1;
	CTransform3x3 rotate;
	CTransform3x3 scale;
	CTransform3x3 trans2;

	// Declare additional variables
	float pxl;					// pixel value
	double mean, sq_sum, stdev; // variables for normailizing data set

	// This image represents the window around the feature you need to compute to store as the feature descriptor
	const int windowSize = 8;
	CFloatImage destImage(windowSize, windowSize, 1);

	for (vector<Feature>::iterator i = features.begin(); i != features.end(); i++) {
		Feature &f = *i;

		// Compute the transform from each pixel in the 8x8 image to sample from the appropriate 
		// pixels in the 40x40 rotated window surrounding the feature
		trans1 = CTransform3x3::Translation(f.x, f.y);						// translate window to feature point
		rotate = CTransform3x3::Rotation(f.angleRadians * 180.0 / PI);		// rotate window by angle
		scale = CTransform3x3::Scale(5.0);									// scale window by 5
		trans2 = CTransform3x3::Translation(-windowSize/2, -windowSize/2);	// translate window to origin

		// transform resulting from combining above transforms
		xform = trans1*scale*rotate*trans2;

		//Call the Warp Global function to do the mapping
		WarpGlobal(blurImage, destImage, xform, eWarpInterpLinear);

		// Resize data field for a 8x8 square window
		f.data.resize(windowSize * windowSize);	

		// Find mean of window
		mean = 0;
		for (int y = 0; y < windowSize; y++) {
			for (int x = 0; x < windowSize; x++) {
				pxl = destImage.Pixel(x, y, 0);
				f.data[y*windowSize + x] = pxl;
				mean += pxl/(windowSize*windowSize);
			}
		}

		// Find standard deviation of window
		sq_sum = 0;
		for (int k = 0; k < windowSize*windowSize; k++) {
			sq_sum += (mean - f.data[k]) * (mean - f.data[k]);
		}
		stdev = sqrt(sq_sum/(windowSize*windowSize));

		// Normalize window to have 0 mean and unit variance by subtracting
		// by mean and dividing by standard deviation
		for (int k = 0; k < windowSize*windowSize; k++) {
			f.data[k] = (f.data[k]-mean)/stdev;
		}
	}
}
void UIGChooserItemMachine::paintBackground(QPainter *pPainter, const QRect &rect)
{
    /* Save painter: */
    pPainter->save();

    /* Prepare color: */
    QPalette pal = palette();

    /* Selection background: */
    if (model()->selectionList().contains(this))
    {
        /* Highlight color: */
        QColor highlight = pal.color(QPalette::Highlight);

        /* Calculate top rectangle: */
        QRect tRect = rect;
        tRect.setBottom(tRect.top() + tRect.height() / 3);
        /* Calculate bottom rectangle: */
        QRect bRect = rect;
        bRect.setTop(bRect.bottom() - bRect.height() / 3);
        /* Calculate middle rectangle: */
        QRect midRect = QRect(tRect.bottomLeft(), bRect.topRight());

        /* Prepare top gradient: */
        QLinearGradient tGradient(tRect.bottomLeft(), tRect.topLeft());
        tGradient.setColorAt(1, highlight.darker(blackoutDarkness()));
        tGradient.setColorAt(0, highlight.darker(defaultDarkness()));
        /* Prepare bottom gradient: */
        QLinearGradient bGradient(bRect.topLeft(), bRect.bottomLeft());
        bGradient.setColorAt(0, highlight.darker(defaultDarkness()));
        bGradient.setColorAt(1, highlight.darker(blackoutDarkness()));

        /* Paint all the stuff: */
        pPainter->fillRect(midRect, highlight.darker(defaultDarkness()));
        pPainter->fillRect(tRect, tGradient);
        pPainter->fillRect(bRect, bGradient);
    }

    /* Hovering background: */
    if (isHovered())
    {
        /* Choose color: */
        QColor baseLight = model()->selectionList().contains(this) ? pal.color(QPalette::Active, QPalette::Highlight) :
                                                                     QColor(Qt::white);
        QColor blurBase = model()->selectionList().contains(this) ? pal.color(QPalette::Active, QPalette::Highlight) :
                                                                    QColor(Qt::white);
        if (!model()->selectionList().contains(this) && !parentItem()->isRoot())
            blurBase = blurBase.darker(defaultDarkness());
        blurBase.setAlpha(0);

        /* Draw background for blur: */
        QImage background(rect.size(), QImage::Format_ARGB32);
        background.fill(blurBase.rgba());

        /* Add blur itself: */
        QPainter blurPainter(&background);
        blurPainter.setBrush(baseLight.darker(animationDarkness()));
        blurPainter.setPen(Qt::NoPen);
        blurPainter.drawRoundedRect(rect.adjusted(5, 5, -5, -5), 5, 5);
        blurPainter.end();
        QImage bluredBackground(rect.size(), QImage::Format_ARGB32);
        blurImage(background, bluredBackground, 5);

        /* Paint highlight bar: */
        pPainter->drawImage(QPoint(0, 0), bluredBackground);
    }

    /* Paint drag token UP? */
    if (dragTokenPlace() != DragToken_Off)
    {
        /* Window color: */
        QColor base = pal.color(QPalette::Active, model()->selectionList().contains(this) ?
                                QPalette::Highlight : QPalette::Window);
        QLinearGradient dragTokenGradient;
        QRect dragTokenRect = rect;
        if (dragTokenPlace() == DragToken_Up)
        {
            dragTokenRect.setHeight(5);
            dragTokenGradient.setStart(dragTokenRect.bottomLeft());
            dragTokenGradient.setFinalStop(dragTokenRect.topLeft());
        }
        else if (dragTokenPlace() == DragToken_Down)
        {
            dragTokenRect.setTopLeft(dragTokenRect.bottomLeft() - QPoint(0, 5));
            dragTokenGradient.setStart(dragTokenRect.topLeft());
            dragTokenGradient.setFinalStop(dragTokenRect.bottomLeft());
        }
        dragTokenGradient.setColorAt(0, base.darker(blackoutDarkness()));
        dragTokenGradient.setColorAt(1, base.darker(dragTokenDarkness()));
        pPainter->fillRect(dragTokenRect, dragTokenGradient);
    }

    /* Restore painter: */
    pPainter->restore();
}
Example #5
0
// Loop through the image to compute the harris corner values as described in class
// srcImage:  grayscale of original image
// harrisImage:  populate the harris values per pixel in this image
void computeHarrisValues(CFloatImage &srcImage, CFloatImage &harrisImage, CFloatImage &orientationImage)
{
	int w = srcImage.Shape().width;	 // image width
	int h = srcImage.Shape().height; // image height

	// Create images to store x-derivative and y-derivative values
	CFloatImage Ix(w,h,1);
	CFloatImage Iy(w,h,1);
	CFloatImage Ix_blur(w,h,1);
	CFloatImage Iy_blur(w,h,1);

	// Compute x-derivative values by convolving image with x sobel filter 
	Convolve(srcImage, Ix, ConvolveKernel_SobelX);

	// Compute y-derivative values by convolving image with y sobel filter
	Convolve(srcImage, Iy, ConvolveKernel_SobelY);
    
	// Apply a 7x7 gaussian blur to the grayscale image
	CFloatImage blurImage(w,h,1);
	Convolve(srcImage, blurImage, ConvolveKernel_7x7);

	// Compute x-derivative values by convolving blurred image with x sobel filter 
	Convolve(blurImage, Ix_blur, ConvolveKernel_SobelX);

	// Compute y-derivative values by convolving blurred image with y sobel filter
	Convolve(blurImage, Iy_blur, ConvolveKernel_SobelY);

	// Declare additional variables
	int newX, newY;		// (x,y) coordinate for pixel in 5x5 sliding window
	float dx, dy;		// x-derivative, y-derivative values
	double HMatrix[4];	// Harris matrix
	double determinant;	// determinant of Harris matrix
	double trace;		// trace of Harris matrix
	int padType = 2;	// select variable for what type of padding to use: , 0->zero, 1->edge, 2->reflect

	// Loop through 'srcImage' and compute harris score for each pixel
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {

			// reset Harris matrix values to 0
			memset(HMatrix, 0, sizeof(HMatrix));

			// Loop through pixels in 5x5 window to calculate Harris matrix
			for (int j = 0; j < 25; j++) {
				find5x5Index(x,y,j,&newX,&newY);
				if(srcImage.Shape().InBounds(newX, newY)) {
					dx = Ix.Pixel(newX,newY,0);
					dy = Iy.Pixel(newX,newY,0);
				} else {
					// Depending on value of padType, perform different types of border padding
					switch (padType) {
						case 1:
							// 1 -> replicate border values
							if (newX < 0) {
								newX = 0;
							} else if (newX >= w) {
								newX = w-1;
							}
				
							if (newY < 0) {
								newY = 0;
							} else if (newY >= h) {
								newY = h-1;
							}

							dx = Ix.Pixel(newX,newY,0);
							dy = Iy.Pixel(newX,newY,0);
							break;
						case 2:
							// 2 -> reflect border pixels
							if (newX < 0) {
								newX = -newX;
							} else if (newX >= w) {
								newX = w-(newX%w)-1;
							}
				
							if (newY < 0) {
								newY = -newY;
							} else if (newY >= h) {
								newY = h-(newY%h)-1;
							}

							dx = Ix.Pixel(newX,newY,0);
							dy = Iy.Pixel(newX,newY,0);
							break;
						default:
							// 0 -> zero padding
							dx = 0.0;
							dy = 0.0;
							break;
					}
				}
				HMatrix[0] += dx*dx*gaussian5x5[j];
				HMatrix[1] += dx*dy*gaussian5x5[j];
				HMatrix[2] += dx*dy*gaussian5x5[j];
				HMatrix[3] += dy*dy*gaussian5x5[j];
			}

			// Calculate determinant and trace of harris matrix
			determinant = (HMatrix[0] * HMatrix[3]) - (HMatrix[1] * HMatrix[2]);
			trace = HMatrix[0] + HMatrix[3];

			// Compute corner strength function c(H) = determinant(H)/trace(H) 
			// and save result into harrisImage 
			if(trace == 0)
				harrisImage.Pixel(x,y,0) = 0.0;
			else
				harrisImage.Pixel(x,y,0) = (determinant / trace);

			// Compute orientation and save result in 'orientationImage'
			dx = Ix_blur.Pixel(x,y,0);
			dy = Iy_blur.Pixel(x,y,0);

			if(dx == 0.0 && dy == 0.0)
				orientationImage.Pixel(x,y,0) = 0.0;
			else
				orientationImage.Pixel(x,y,0) = atan2(dy, dx);
		}
	}
}