private void compareImages() { for (int row = 0; row < image1.height(); row++) { for (int cell = 0; cell < image1.width(); cell++) { CvScalar rgbBack = cvGet2D(image1, row, cell); double b_back = rgbBack.getVal(0) + 100; double g_back = rgbBack.getVal(1) + 100; double r_back = rgbBack.getVal(2) + 100; CvScalar rgb_Obj = cvGet2D(image2, row, cell); double b_obj = rgb_Obj.getVal(0); double g_obj = rgb_Obj.getVal(1); double r_obj = rgb_Obj.getVal(2); double r_diff = r_back + r_obj - 2 * r_obj; // Math.min(r_back, r_obj ); double g_diff = g_back + g_obj - 2 * r_obj; // Math.min(g_back, g_obj ); double b_diff = b_back + b_obj - 2 * r_obj; // Math.min(b_back, b_obj ); CvScalar rgb = CV_RGB(r_diff, g_diff, b_diff); cvSet2D(diff, row, cell, rgb); double bl_diff = (r_diff + g_diff + b_diff) / 3; CvScalar bl_pix = CV_RGB(bl_diff, bl_diff, bl_diff); cvSet2D(diff_black, row, cell, bl_pix); } } }
protected void processImage(byte[] data, int width, int height) { // First, downsample our image and convert it into a grayscale IplImage int f = SUBSAMPLING_FACTOR; if (grayImage == null || grayImage.width() != width / f || grayImage.height() != height / f) { grayImage = IplImage.create(width / f, height / f, IPL_DEPTH_8U, 1); } int imageWidth = grayImage.width(); int imageHeight = grayImage.height(); int dataStride = f * width; int imageStride = grayImage.widthStep(); ByteBuffer imageBuffer = grayImage.getByteBuffer(); for (int y = 0; y < imageHeight; y++) { int dataLine = y * dataStride; int imageLine = y * imageStride; for (int x = 0; x < imageWidth; x++) { imageBuffer.put(imageLine + x, data[dataLine + f * x]); } } cvClearMemStorage(storage); faces = cvHaarDetectObjects(grayImage, classifier, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING); postInvalidate(); }
public ImageHandler(String imagePath_1, String imagePath_2) { this.image1 = cvLoadImage(imagePath_1); this.image2 = cvLoadImage(imagePath_2); this.diff = image1.clone(); this.diff_black = image2.clone(); compareImages(); this.image1_line = image1.clone(); this.image2_line = image2.clone(); this.diff_line = diff.clone(); this.width = image1.width(); this.height = image1.height(); this.vals = new ArrayList<Map<String, Double>>(); this.canvas1 = new CanvasFrame("Image 1", 1.0); this.canvas2 = new CanvasFrame("Image 2", 1.0); this.canvas3 = new CanvasFrame("Diff", 1.0); this.canvas4 = new CanvasFrame("Diff_black", 1.0); }
@Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); paint.setTextSize(20); String s = "FacePreview - This side up."; float textWidth = paint.measureText(s); canvas.drawText(s, (getWidth() - textWidth) / 2, 20, paint); if (faces != null) { paint.setStrokeWidth(2); paint.setStyle(Paint.Style.STROKE); float scaleX = (float) getWidth() / grayImage.width(); float scaleY = (float) getHeight() / grayImage.height(); int total = faces.total(); for (int i = 0; i < total; i++) { CvRect r = new CvRect(cvGetSeqElem(faces, i)); int x = r.x(), y = r.y(), w = r.width(), h = r.height(); canvas.drawRect(x * scaleX, y * scaleY, (x + w) * scaleX, (y + h) * scaleY, paint); } } }