Last active
August 29, 2015 14:26
-
-
Save brunocribeiro/a1a67485a8145ce4e197 to your computer and use it in GitHub Desktop.
Open CV Util Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.opencv.core.Core; | |
import org.opencv.core.Core.MinMaxLocResult; | |
import org.opencv.core.CvType; | |
import org.opencv.core.Mat; | |
import org.opencv.core.Rect; | |
import org.opencv.imgproc.Imgproc; | |
/** | |
* Utilitários para o uso da biblioteca do OpenCV | |
*/ | |
public final class OpenCVUtil { | |
/** | |
* Inicializa a biblioteca nativa do OpenCV | |
*/ | |
public static void loadOpenCVLibrary() { | |
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | |
} | |
/** | |
* Recortada uma área da imagem, de acordo com o posicionamento do retângula passado como parâmetro | |
* | |
* @param imageSource | |
* imagem a ter a área recortada | |
* @param cropArea | |
* área a ser cortada na imagem | |
* @return {@link Mat} da área recortada | |
*/ | |
public static Mat cropImage(final Mat imageSource, final Rect cropArea) { | |
return new Mat(imageSource, cropArea); | |
} | |
/** | |
* Recorta uma área em uma imagem | |
* | |
* @param imageSource | |
* imagem a ter a área recortada | |
* @param x | |
* início horizontal na imagem a ter a área cortada | |
* @param y | |
* início vertical na imagem a ter a área cortada | |
* @param width | |
* largura do recorte | |
* @param height | |
* altura do recorte | |
* @return {@link Mat} da área recortada | |
*/ | |
public static Mat cropImage(final Mat imageSource, final Integer x, final Integer y, final Integer width, final Integer height) { | |
final Rect cropArea = new Rect(x, y, width, height); | |
return cropImage(imageSource, cropArea); | |
} | |
/** | |
* Procura em uma imagem o padrão de baseado em template | |
* | |
* <p> | |
* {@code WARNING}: solicita ao OpenCV apenas o {@link Mat#release()} de objetos internos. {@code image} e {@code template} devem ter a | |
* memória desalocada pelo chamador da função | |
* </p> | |
* | |
* @param image | |
* imagem a ter o template/padrão a ser verificado | |
* @param template | |
* template/padrão de imagem a ser buscado na imagem | |
* @param taxaDeAcerto | |
* taxa de acerto a ser verificada na busca pelo padrão | |
* @return {@link MinMaxLocResult} de início do local com maior taxa de acerto | |
*/ | |
public static MinMaxLocResult matchByTemplate(final Mat image, final Mat template, final Double taxaDeAcerto) { | |
// cria a matriz de resultado | |
final Integer resultCols = image.cols() - template.cols() + 1; | |
final Integer resultRows = image.rows() - template.rows() + 1; | |
final Mat res = new Mat(resultRows, resultCols, CvType.CV_32FC1); | |
final Integer matchMethod = Imgproc.TM_CCOEFF_NORMED; | |
// executa a pesquisa do template | |
Imgproc.matchTemplate(image, template, res, matchMethod); | |
Imgproc.threshold(res, res, taxaDeAcerto, 255, Imgproc.THRESH_TOZERO); | |
final MinMaxLocResult result = Core.minMaxLoc(res); | |
OpenCVUtil.memoryDeallocate(res); | |
return result; | |
} | |
/** | |
* <p> | |
* O OpenCV sobrescreve o método {@link #finalize()}, que tenta desalocar a memória por método nativo. No entanto, ao fazer isto, a JVM | |
* acaba tentando desalocar o mesmo endereçamento de memória, não consegue e acaba ficando muita memória alocada. | |
* </p> | |
* | |
* <p> | |
* Este método tenta chamar de outra forma o desalocamento de mémoria | |
* </p> | |
* | |
* @param matrizImagem | |
* matriz do OpenCV a ter a memória desalocada | |
*/ | |
public static void memoryDeallocate(final Mat matrizImagem) { | |
matrizImagem.release(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment