Example usage for org.opencv.core Core normalize

List of usage examples for org.opencv.core Core normalize

Introduction

In this page you can find the example usage for org.opencv.core Core normalize.

Prototype

public static void normalize(Mat src, Mat dst, double alpha, double beta, int norm_type, int dtype, Mat mask) 

Source Link

Usage

From source file:Retrive.java

public Mat query_histo(Mat query_img) {
    Vector<Mat> bgr_planes = new Vector<>();
    Core.split(query_img, bgr_planes);/*from   w  ww.  j a v  a 2 s. c  o  m*/

    MatOfInt histSize = new MatOfInt(256);
    final MatOfFloat histRange = new MatOfFloat(0f, 256f);
    //boolean accumulate = false;
    Mat q_hist = new Mat();
    int hist_w = 512;
    int hist_h = 600;
    long bin_w;
    bin_w = Math.round((double) (hist_w / 256));

    Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC3);

    Imgproc.calcHist(bgr_planes, new MatOfInt(0), new Mat(), q_hist, histSize, histRange);
    Core.normalize(q_hist, q_hist, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());
    return q_hist;
}

From source file:Retrive.java

public Mat histo(Mat imgs) {
    Vector<Mat> bgr_planes = new Vector<>();
    Core.split(imgs, bgr_planes);/*from  w w  w. j  av a2s  .  c  o  m*/

    MatOfInt histSize = new MatOfInt(256);
    final MatOfFloat histRange = new MatOfFloat(0f, 256f);
    boolean accumulate = false;
    Mat b_hist = new Mat();
    int hist_w = 512;
    int hist_h = 600;
    //long bin_w;
    //bin_w = Math.round((double) (hist_w / 256));

    Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC3);

    Imgproc.calcHist(bgr_planes, new MatOfInt(0), new Mat(), b_hist, histSize, histRange, accumulate);
    Core.normalize(b_hist, b_hist, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());
    return b_hist;
}

From source file:ch.hslu.pren.t37.camera.BildAuswertungKorb.java

public int bildAuswerten() {

    //Bild in dem gesucht werden soll
    String inFile = "../camera.jpg";
    //das Bild dass im infile gesucht wird
    String templateFile = "../Bilder/korb.jpg";
    //Lsung wird in diesem Bild prsentiert
    String outFile = "../LoesungsBild.jpg";
    //berprfungswert wird gesetzt
    int match_method = Imgproc.TM_CCOEFF_NORMED;

    //das original Bild und das zu suchende werden geladen
    Mat img = Highgui.imread(inFile, Highgui.CV_LOAD_IMAGE_COLOR);
    Mat templ = Highgui.imread(templateFile, Highgui.CV_LOAD_IMAGE_COLOR);

    // Lsungsmatrix generieren
    int result_cols = img.cols() - templ.cols() + 1;
    int result_rows = img.rows() - templ.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

    // Suchen und normalisieren
    Imgproc.matchTemplate(img, templ, result, match_method);
    Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    // Mit MinMax Logik wird der beste "Match" gesucht
    Core.MinMaxLocResult mmr = Core.minMaxLoc(result);

    Point matchLoc;/*from  w ww.  j av a  2s .  c om*/
    if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
        matchLoc = mmr.minLoc;
    } else {
        matchLoc = mmr.maxLoc;
    }

    // Darstellen
    Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(), matchLoc.y + templ.rows()),
            new Scalar(0, 255, 0), 10);

    // Alle 4 Eckpunkte speichern
    Point topLeft = new Point(matchLoc.x, matchLoc.y);
    Point topRight = new Point(matchLoc.x + templ.cols(), matchLoc.y);
    Point downLeft = new Point(matchLoc.x, matchLoc.y + templ.rows());
    Point downRight = new Point(matchLoc.x + templ.cols(), matchLoc.y + templ.rows());

    // Lsungsbild speichern
    Highgui.imwrite(outFile, img);

    //Mittelpunkt berechnen
    double mittePicture;
    double mitteKorb;
    double differnez;

    Mat sol = Highgui.imread(outFile, Highgui.CV_LOAD_IMAGE_COLOR);

    mittePicture = sol.width() / 2;
    mitteKorb = (topRight.x - topLeft.x) / 2;
    mitteKorb = topLeft.x + mitteKorb;
    differnez = mitteKorb - mittePicture;

    logger.log(PrenLogger.LogLevel.DEBUG, "Mitte Korb: " + mitteKorb);
    logger.log(PrenLogger.LogLevel.DEBUG, "Mitte Bild: " + mittePicture);
    logger.log(PrenLogger.LogLevel.DEBUG,
            "Differenz: " + differnez + "\nWenn Differnez negativ, nach rechts drehen");

    return (int) differnez;
}

From source file:contador_de_moedas.Reconhecimento.java

public Mat CalculaHistograma(Mat mask, Mat img_Original) {
    // Converte para um tipo de imagem com canais separados
    List<Mat> imagesList = new ArrayList<>();
    imagesList.add(img_Original);/*from   w w w  . ja  v a2  s.co m*/
    // Parametros do calcHist
    Mat histograma = new Mat();
    int h_bins = 30;
    int s_bins = 32;
    MatOfInt mHistSize = new MatOfInt(h_bins, s_bins);
    MatOfFloat mRanges = new MatOfFloat(0, 179, 0, 255);
    MatOfInt mChannels = new MatOfInt(0, 1);
    // Calcula o histograma
    Imgproc.calcHist(imagesList, mChannels, mask, histograma, mHistSize, mRanges, false);
    // Normalizao necessria para ajustar valores
    Core.normalize(histograma, histograma, 0, 255, Core.NORM_MINMAX, -1, new Mat());

    return (histograma);
}

From source file:edu.soict.hust.k57.mmdb.components.HistogramCaculator.java

@Override
public void accept(ImgEnt t) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
    Mat m = Imgcodecs.imread(t.getF().getPath());
    List<Mat> images = new ArrayList<Mat>();
    Core.split(m, images);//  www  . ja v  a  2 s  .  co m

    MatOfInt histSize = new MatOfInt(t.getBin()); // kch thc ca histogram
    MatOfInt channels = new MatOfInt(0); // Knh mu mun tnh
    MatOfFloat histRange = new MatOfFloat(0, 256);

    Mat bHist = new Mat();
    Mat gHist = new Mat();
    Mat rHist = new Mat();

    Imgproc.calcHist(images.subList(0, 1), channels, new Mat(), bHist, histSize, histRange, false);
    Core.normalize(bHist, bHist, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    Imgproc.calcHist(images.subList(1, 2), channels, new Mat(), gHist, histSize, histRange, false);
    Core.normalize(gHist, gHist, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    Imgproc.calcHist(images.subList(2, 3), channels, new Mat(), rHist, histSize, histRange, false);
    Core.normalize(rHist, rHist, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    t.setbHistogram(bHist);
    t.setgHistogram(gHist);
    t.setrHistogram(rHist);
}

From source file:edu.soict.hust.k57.mmdb.components.HistogramImageBulder.java

private ImageIcon createImageIcon(Mat hist, int bin, Channel c) {
    int hist_w = 150; // width of the histogram image
    int hist_h = 100; // height of the histogram image
    int bin_w = (int) Math.round(hist_w * 1.0 / bin);

    Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC3, new Scalar(80, 60, 60));
    Mat normalizeHist = hist.clone();/*w  ww . j  a v  a2 s .  com*/
    Core.normalize(normalizeHist, normalizeHist, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());

    Scalar scalar = null;
    switch (c) {
    case B:
        scalar = new Scalar(255, 0, 0);
        break;
    case G:
        scalar = new Scalar(0, 255, 0);
        break;
    case R:
        scalar = new Scalar(0, 0, 255);
    }

    for (int i = 1; i < bin; i++) {
        Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - Math.round(normalizeHist.get(i - 1, 0)[0])),
                new Point(bin_w * (i), hist_h - Math.round(normalizeHist.get(i - 1, 0)[0])), scalar, 1, 8, 0);
        Imgproc.line(histImage, new Point(bin_w * (i), hist_h - Math.round(normalizeHist.get(i - 1, 0)[0])),
                new Point(bin_w * (i), hist_h - Math.round(normalizeHist.get(i, 0)[0])), scalar, 1, 8, 0);
    }
    MatOfByte buffer = new MatOfByte();
    Imgcodecs.imencode(".png", histImage, buffer);
    return new ImageIcon(buffer.toArray());
}

From source file:info.jmfavreau.bifrostcore.imageprocessing.ImageToColor.java

License:Open Source License

private Mat extract_main_region(Mat img, Mat roi) {
    Mat hist = new Mat();
    int h_bins = 30;
    int s_bins = 32;
    MatOfInt mHistSize = new MatOfInt(h_bins, s_bins);

    MatOfFloat mRanges = new MatOfFloat(0, 179, 0, 255);
    MatOfInt mChannels = new MatOfInt(0, 1);

    Imgproc.calcHist(Arrays.asList(img), mChannels, roi, hist, mHistSize, mRanges, false);

    Core.normalize(hist, hist, 0, 255, Core.NORM_MINMAX, -1, new Mat());

    Mat backproj = new Mat();
    Imgproc.calcBackProject(Arrays.asList(img), mChannels, hist, backproj, mRanges, 1);

    Log.w("bifrostcore",
            "Number of pixels in the biggest region: " + String.valueOf(Core.countNonZero(backproj)));
    return backproj.mul(roi);
}

From source file:mvision.Bhattacharyya.java

public Mat histogram(String img) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat image = Highgui.imread(img);//from ww  w . ja  v  a2 s . c o  m

    //Mat image = Highgui.imread("C:\\image1.jpg");

    //Mat src = new Mat(image.height(), image.width(), CvType.CV_8UC2);

    Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2HSV);
    java.util.List<Mat> matList = new LinkedList<Mat>();
    matList.add(image);
    Mat histogram = new Mat();
    MatOfFloat ranges = new MatOfFloat(0, 256);
    MatOfInt histSize = new MatOfInt(255);
    Imgproc.calcHist(matList, new MatOfInt(0), new Mat(), histogram, histSize, ranges);

    // Create space for histogram image
    Mat histImage = Mat.zeros(100, (int) histSize.get(0, 0)[0], CvType.CV_8UC1);

    histogram.convertTo(histogram, CvType.CV_32F);

    // Normalize histogram      
    Core.normalize(histogram, histogram, 1, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());
    // Draw lines for histogram points
    for (int i = 0; i < (int) histSize.get(0, 0)[0]; i++) {
        Core.line(histImage, new org.opencv.core.Point(i, histImage.rows()),
                new org.opencv.core.Point(i, histImage.rows() - Math.round(histogram.get(i, 0)[0])),
                new Scalar(255, 255, 255), 1, 8, 0);
    }
    return histogram;

}

From source file:Recognizer.Recognizer.java

public Image TemplateMatching(Image imQuery, Image imDB, int match_method) {
    System.out.println("Running Template Matching ...");

    //Mat img = Highgui.imread(inFile); // Image in which area has to be searched
    //Mat template_img = Highgui.imread(templateFile); // Search Image

    Mat matQuery = imQuery.Image3CtoMat_CV();
    Mat matDB = imDB.Image3CtoMat_CV();

    Mat hsvQ = new Mat(), hsvDB = new Mat();

    Imgproc.cvtColor(matQuery, hsvQ, COLOR_RGB2HSV);
    Imgproc.cvtColor(matDB, hsvDB, COLOR_RGB2HSV);

    // Create result image matrix
    int resultImg_cols = matDB.cols() - matQuery.cols() + 1;
    int resultImg_rows = matDB.rows() - matQuery.rows() + 1;

    Mat matRes = new Mat(resultImg_rows, resultImg_cols, CvType.CV_32FC1);

    // Template Matching with Normalization
    Imgproc.matchTemplate(hsvDB, hsvQ, matRes, match_method);
    Core.normalize(matRes, matRes, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    // / Localizing the best match with minMaxLoc
    Core.MinMaxLocResult Location_Result = Core.minMaxLoc(matRes);
    Point matchLocation;//from w  w  w .jav a2 s  .  c o  m

    if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
        matchLocation = Location_Result.minLoc;
    } else {
        matchLocation = Location_Result.maxLoc;
    }

    // Display Area by Rectangle
    Core.rectangle(matDB, matchLocation,
            new Point(matchLocation.x + matQuery.cols(), matchLocation.y + matQuery.rows()),
            new Scalar(0, 255, 0));

    Image imOut = new Image(matDB.width(), matDB.height());
    //Image imOut = new Image(matQuery.cols(), matQuery.rows());

    //Mat m = new Mat(matDB);

    //m =//matDB.submat((int)matchLocation.y, (int)matchLocation.y + matQuery.rows(),(int)matchLocation.x, (int)matchLocation.x + matQuery.cols());

    imOut.Mat_CVtoImage3C(matDB);

    System.out.println("Location: " + Location_Result.minLoc.x + " " + Location_Result.minLoc.y + "   "
            + Location_Result.maxLoc.x + " " + Location_Result.maxLoc.y);

    return imOut;
}

From source file:Recognizer.Recognizer.java

public Image HistMatch(Image imQuery, Image imDB) {
    Image imOut = new Image(352, 288);

    Mat srcQ, srcDB;/*from  ww w.j  a va 2s.c  o  m*/
    Mat hsvQ = new Mat(), hsvDB = new Mat();

    srcQ = imQuery.Image3CtoMat_CV();
    srcDB = imDB.Image3CtoMat_CV();

    //Convert To HSV
    Imgproc.cvtColor(srcQ, hsvQ, Imgproc.COLOR_RGB2HSV);
    Imgproc.cvtColor(srcDB, hsvDB, Imgproc.COLOR_RGB2HSV);

    java.util.List<Mat> matlistQ = Arrays.asList(hsvQ);
    java.util.List<Mat> matlistDB = Arrays.asList(hsvDB);

    //Use 100 bins for hue, 100 for Saturation
    int h_bins = 360, s_bins = 4;
    int[] histsize = { h_bins, s_bins };
    MatOfInt histSize = new MatOfInt(histsize);

    MatOfFloat Ranges = new MatOfFloat(0, 180, 0, 256);

    int[] channels = { 0, 1 };
    MatOfInt CH = new MatOfInt(channels);

    Mat hist_Q = new Mat();
    Mat hist_DB = new Mat();

    Imgproc.calcHist(matlistQ, CH, new Mat(), hist_Q, histSize, Ranges);
    Core.normalize(hist_Q, hist_Q, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    float res;

    Mat[] hsvaLev1 = new Mat[4];
    Mat[] hsvaLev2 = new Mat[16];
    Mat[] hsvaLev3 = new Mat[64];
    // Mat[] hsvaLev4 = new Mat[256];

    float[] iaLev1 = new float[4];
    float[] iaLev2 = new float[16];
    float[] iaLev3 = new float[64];
    //float[] iaLev4 = new float[256];

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            hsvaLev1[i * 2 + j] = hsvDB.submat(0 + i * 288 / 2, 143 + i * 288 / 2, 0 + j * 352 / 2,
                    175 + j * 352 / 2);
        }
    }

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            hsvaLev2[i * 4 + j] = hsvDB.submat(0 + i * 288 / 4, 71 + i * 288 / 4, 0 + j * 352 / 4,
                    87 + j * 352 / 4);
        }
    }

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            hsvaLev3[i * 8 + j] = hsvDB.submat(0 + i * 288 / 8, 35 + i * 288 / 8, 0 + j * 352 / 8,
                    43 + j * 352 / 8);
        }
    }

    System.out.println("Lev_1");
    for (int m = 0; m < 4; m++) {
        matlistDB = Arrays.asList(hsvaLev1[m]);
        Imgproc.calcHist(matlistDB, CH, new Mat(), hist_DB, histSize, Ranges);
        Core.normalize(hist_DB, hist_DB, 0, 1, Core.NORM_MINMAX, -1, new Mat());
        res = (float) Imgproc.compareHist(hist_Q, hist_DB, Imgproc.CV_COMP_BHATTACHARYYA);

        System.out.println("Res: " + res);
        iaLev1[m] = res;
    }

    System.out.println("Lev_2");
    for (int m = 0; m < 16; m++) {
        matlistDB = Arrays.asList(hsvaLev2[m]);
        Imgproc.calcHist(matlistDB, CH, new Mat(), hist_DB, histSize, Ranges);
        Core.normalize(hist_DB, hist_DB, 0, 1, Core.NORM_MINMAX, -1, new Mat());
        res = (float) Imgproc.compareHist(hist_Q, hist_DB, Imgproc.CV_COMP_BHATTACHARYYA);

        System.out.println("Res: " + res);
        iaLev2[m] = res;
    }

    System.out.println("Lev_3");
    for (int m = 0; m < 64; m++) {
        matlistDB = Arrays.asList(hsvaLev3[m]);
        Imgproc.calcHist(matlistDB, CH, new Mat(), hist_DB, histSize, Ranges);
        Core.normalize(hist_DB, hist_DB, 0, 1, Core.NORM_MINMAX, -1, new Mat());
        res = (float) Imgproc.compareHist(hist_Q, hist_DB, Imgproc.CV_COMP_BHATTACHARYYA);

        System.out.println("Res: " + res);
        iaLev3[m] = res;
    }

    int x = MinIndex(iaLev1);
    int i = x % 2;
    int j = x / 2;
    Core.rectangle(srcDB, new Point(0 + j * 352 / 2, 0 + i * 288 / 2),
            new Point(175 + j * 352 / 2, 143 + i * 288 / 2), new Scalar(0, 255, 0));

    x = MinIndex(iaLev2);
    i = x % 4;
    j = x / 4;
    Core.rectangle(srcDB, new Point(0 + j * 352 / 4, 0 + i * 288 / 4),
            new Point(87 + j * 352 / 4, 71 + i * 288 / 4), new Scalar(0, 0, 255));

    x = MinIndex(iaLev3);
    i = x % 8;
    j = x / 8;
    Core.rectangle(srcDB, new Point(0 + j * 352 / 8, 0 + i * 288 / 8),
            new Point(43 + j * 352 / 8, 35 + i * 288 / 8), new Scalar(255, 0, 0));

    imOut.Mat_CVtoImage3C(srcDB);

    return imOut;
}