Created
October 20, 2011 01:42
-
-
Save lecram/1300207 to your computer and use it in GitHub Desktop.
Limiarização com OpenCV.
This file contains 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
/* Limiarização de imagens utilizando OpenCV. | |
* | |
* Este programa implementa uma função genérica para limiarização de | |
* imagens em escala de cinza. | |
* | |
* Utilização: thresh file threshold below above | |
* *file - Nome do arquivo da imagem de entrada. | |
* *threshold - Valor real do limiar, no intervalo [0, 1]. | |
* *below - Valor real que deve substituir valores abaixo do (ou iguais ao) | |
* limiar, no intervalo [0, 1]. | |
* Se for desejado que os valores abaixo do (ou iguais ao) limiar não sejam | |
* alterados, deve-se utilizar a string "=". | |
* *above - Valor real que deve substituir valores acima do limiar, | |
* no intervalo [0, 1]. | |
* Se for desejado que os valores acima do limiar não sejam alterados, | |
* deve-se utilizar a string "=". | |
* | |
* Exemplos: | |
* *limiarização binária: thresh file 0.5 0.0 1.0 | |
* *limiarização binária invertida: thresh file 0.5 1.0 0.0 | |
* *saturação: thresh file 0.75 = 1.0 | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <opencv/cv.h> | |
#include <opencv/highgui.h> | |
#define UNCHANGED -1.0 | |
/* Limiariza uma imagem em escala de cinza, trocando os valores menores ou | |
* iguais a 'threshold' para 'below', e os demais valores para 'above'. | |
* A macro UNCHANGED pode ser utilizada para 'below' ou 'above', caso seja | |
* desejado que os valores não sejam alterados abaixo ou acima do limiar. | |
*/ | |
void | |
thresh(IplImage *image, float threshold, float below, float above) | |
{ | |
int x, y; | |
int offset; | |
unsigned char uc_threshold, uc_below, uc_above; | |
uc_threshold = (unsigned char) (threshold * 255.0); | |
uc_below = (unsigned char) (below * 255.0); | |
uc_above = (unsigned char) (above * 255.0); | |
for (y = 0; y < image->height; y++) { | |
for (x = 0; x < image->width; x++) { | |
offset = y * image->widthStep + x; | |
if ((unsigned char) image->imageData[offset] <= uc_threshold) { | |
if (below != UNCHANGED) | |
image->imageData[offset] = uc_below; | |
} | |
else { | |
if (above != UNCHANGED) | |
image->imageData[offset] = uc_above; | |
} | |
} | |
} | |
} | |
int | |
main(int argc, char *argv[]) | |
{ | |
IplImage *image; | |
size_t len; | |
char *prefix, *infile, *outfile; | |
float threshold, below, above; | |
int64 t0, t1; | |
double tps, deltatime; | |
if (argc != 5) { | |
puts("Usage: thresh file threshold below above"); | |
return EXIT_FAILURE; | |
} | |
infile = argv[1]; | |
image = cvLoadImage(infile, CV_LOAD_IMAGE_GRAYSCALE); | |
if (image == NULL) { | |
puts("Could not load image file."); | |
return EXIT_FAILURE; | |
} | |
prefix = "thresh-"; | |
len = strlen(prefix) + strlen(infile); | |
outfile = (char *) malloc((len + 1) * sizeof(char)); | |
strcpy(outfile, prefix); | |
strcat(outfile, infile); | |
threshold = strtof(argv[2], NULL); | |
if (!strcmp(argv[3], "=")) | |
below = UNCHANGED; | |
else | |
below = strtof(argv[3], NULL); | |
if (!strcmp(argv[4], "=")) | |
above = UNCHANGED; | |
else | |
above = strtof(argv[4], NULL); | |
t0 = cvGetTickCount(); | |
thresh(image, threshold, below, above); | |
t1 = cvGetTickCount(); | |
tps = cvGetTickFrequency() * 1.0e6; | |
deltatime = (double) (t1 - t0) / tps; | |
cvSaveImage(outfile, image, 0); | |
printf("%dx%d image processed in %.3f seconds.", | |
image->width, image->height, deltatime); | |
cvReleaseImage(&image); | |
free(outfile); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment