Created
March 16, 2019 17:15
-
-
Save johnnyferreiradev/e7fb50b94c41a329065ea49b302906de to your computer and use it in GitHub Desktop.
Sistema de processamento de imagens digitais na aplicação de filtros passa alta (Laplaciano, Operadores de Robert, Prewitt e Sabel). O sistema compila 4 imagens. A duas dessas imagens são aplicados um filtro gaussiano para remoção de ruído. Foi utilizado a biblioteca OpenCV.
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
#include <iostream> | |
#include <opencv2/opencv.hpp> | |
#include <math.h> | |
using namespace cv; | |
using namespace std; | |
String dirSaida = "/home/johnny/Documentos/Sistemas de Informacao/S4/PDI/Projetos/PDI_Experimento_PassaAltas/Saidas/"; | |
String imagensEntrada[] = {"/home/johnny/Documentos/Sistemas de Informacao/S4/PDI/Projetos/PDI_Experimento_PassaAltas/Entradas/crianca.jpg", | |
"/home/johnny/Documentos/Sistemas de Informacao/S4/PDI/Projetos/PDI_Experimento_PassaAltas/Entradas/lenna.jpg", | |
"/home/johnny/Documentos/Sistemas de Informacao/S4/PDI/Projetos/PDI_Experimento_PassaAltas/Entradas/desenho.jpg", | |
"/home/johnny/Documentos/Sistemas de Informacao/S4/PDI/Projetos/PDI_Experimento_PassaAltas/Entradas/formas.jpg"}; | |
Mat criarImagem(int i){ | |
Mat img = imread(imagensEntrada[i], 0); | |
return img; | |
} | |
Mat convoluirGenerica(Mat& img, float **masc, int M, int N){ | |
float output=0; | |
Mat resp = img.clone(); | |
int m=(M-1)/2; | |
int n=(N-1)/2; | |
for (int x=0; x<img.rows;x++){ | |
for (int y=0; y<img.cols; y++){ | |
resp.at<uchar>(x,y) = 0; | |
} | |
} | |
int i=0,j=0; | |
for (int x=m; x<img.rows-m;x++){ | |
for (int y=n; y<img.cols-n; y++) { | |
output=0; | |
for (int ml=-m; ml<=m; ml++){ | |
for (int mc=-n; mc<=n; mc++) { | |
output += img.at<uchar>(x-mc,y-ml) * masc[mc+ m][ml+n]; | |
if(output>255) | |
output=255; | |
if(output<0) | |
output=0; | |
} | |
} | |
resp.at<uchar>(x,y) = (int)output; | |
} | |
} | |
return (resp); | |
} | |
Mat filtroLaplaciano(Mat img){ | |
float **Masc = (float **) malloc (3 *sizeof (float*)); | |
for(int i=0; i<3; i++){ | |
Masc[i] = (float *) malloc (3 *sizeof (float)); | |
} | |
Masc[0][0] = 0; Masc[0][1] = -1; Masc[0][2] = 0; | |
Masc[1][0] = -1; Masc[1][1] = 4; Masc[1][2] = -1; | |
Masc[2][0] = 0; Masc[2][1] = -1; Masc[2][2] = 0; | |
return convoluirGenerica(img, Masc, 3, 3); | |
} | |
void criarGaussiano(float **mascara, int largura, int altura){ | |
// adotando desvio padrão = 1,0 | |
float sigma = 1.0; | |
float r, s = 2.0 * sigma * sigma; | |
// variável para somatório | |
float soma = 0.0; | |
// delimitadores para máscara simétrica com média em (0,0) | |
int m = (largura-1)/2; | |
int n = (altura-1)/2; | |
// geração dos valores da máscara | |
for (int x = -m; x <= m; x++) { | |
for (int y = -n; y <= n; y++) { | |
r = sqrt(x * x + y * y); | |
mascara[x + m][y + n] = (exp(-(r * r) / s)) / (M_PI * s); | |
soma += mascara[x + m][y + n]; | |
} | |
} | |
// normalizando a máscara | |
for (int i = 0; i < largura; ++i) | |
for (int j = 0; j < altura; ++j) | |
mascara[i][j] /= soma; | |
} | |
Mat filtroGauss(Mat img, int x, int y){ | |
Mat processada = img.clone(); | |
// Cria dinamicamente uma matriz com as dimenções passadas por parametro | |
float **Kernel; | |
Kernel = (float**) malloc (x * sizeof(float*) ); | |
for (int i=0; i<x; i++){ | |
Kernel[i] = (float*) malloc (y * sizeof(float) ); | |
} | |
criarGaussiano(Kernel, x, y); // Atribuição de valores a mascara | |
processada = convoluirGenerica(img, Kernel, x, y); // Processamento da imagem | |
return processada; | |
} | |
Mat operadoresRobert(Mat img){ | |
Mat imgResultante = img.clone(); | |
int gx,gy; | |
for (int i=0; i < img.rows; i++) { | |
for(int j=0; j < img.cols; j++){ | |
gx = img.at<uchar>(i,j) - img.at<uchar>(i+1,j+1); | |
gy = img.at<uchar>(i+1,j) - img.at<uchar>(i,j+1); | |
imgResultante.at<uchar>(i,j) = sqrt(pow(gx,2) + pow(gy,2)); | |
} | |
} | |
return imgResultante; | |
} | |
Mat operadoresPrewitt(Mat img){ | |
Mat imgResultante = img.clone(); | |
int gx,gy; | |
for(int i=0; i< img.rows; i++){ | |
for(int j=0; j < img.cols; j++){ | |
gx = (img.at<uchar>(i-1,j+1) + img.at<uchar>(i,j+1) + img.at<uchar>(i+1,j+1)) - | |
(img.at<uchar>(i-1, j-1) + img.at<uchar>(i, j-1) + img.at<uchar>(i+1, j-1)); | |
gy = (img.at<uchar>(i+1,j-1) + img.at<uchar>(i+1,j) + img.at<uchar>(i+1,j+1)) - | |
(img.at<uchar>(i-1, j-1) + img.at<uchar>(i-1, j) + img.at<uchar>(i-1, j+1)); | |
imgResultante.at<uchar>(i,j) = sqrt(pow(gx, 2) + pow(gy, 2)); | |
} | |
} | |
return imgResultante; | |
} | |
Mat operadoresSabel(Mat img){ | |
Mat imgResultante = img.clone(); | |
int gx,gy; | |
for(int i=0; i< img.rows; i++){ | |
for(int j=0; j < img.cols; j++){ | |
gx = (img.at<uchar>(i+1,j-1) + 2*(img.at<uchar>(i+1,j)) + img.at<uchar>(i+1,j+1)) - | |
(img.at<uchar>(i-1, j-1) + 2*(img.at<uchar>(i-1, j)) + img.at<uchar>(i-1, j+1)); | |
gy = (img.at<uchar>(i-1,j+1) + 2*(img.at<uchar>(i,j+1)) + img.at<uchar>(i+1,j+1)) - | |
(img.at<uchar>(i-1, j-1) + 2*(img.at<uchar>(i, j-1)) + img.at<uchar>(i+1, j-1)); | |
imgResultante.at<uchar>(i,j) = sqrt(pow(gx, 2) + pow(gy, 2)); | |
} | |
} | |
return imgResultante; | |
} | |
void processarImagens(){ | |
Mat img, imgLaplaciano, imgRobert, imgPrewitt, imgSabel; | |
for(int i=0; i<4; i++){ | |
String dirAtual = "Imagem" + to_string(i+1); | |
imgLaplaciano = filtroLaplaciano(criarImagem(i)); | |
imgRobert = operadoresRobert(criarImagem(i)); | |
imgPrewitt = operadoresPrewitt(criarImagem(i)); | |
imgSabel = operadoresSabel(criarImagem(i)); | |
imwrite(dirSaida + dirAtual + "/Laplaciano/" + "img.jpg", imgLaplaciano); | |
imwrite(dirSaida + dirAtual + "/Robert/" + "img.jpg", imgRobert); | |
imwrite(dirSaida + dirAtual + "/Prewitt/" + "img.jpg", imgPrewitt); | |
imwrite(dirSaida + dirAtual + "/Sabel/" + "img.jpg", imgSabel); | |
} | |
// Aplicando preprocessamento com filtro gaussiano antes da utilizacao dos filtros passa altas | |
for(int k=0; k<2; k++){ | |
String dirAtual = "Imagem" + to_string(k+1); | |
imgLaplaciano = filtroLaplaciano(filtroGauss(criarImagem(k), 9, 9)); | |
imgRobert = operadoresRobert(filtroGauss(criarImagem(k), 9, 9)); | |
imgPrewitt = operadoresPrewitt(filtroGauss(criarImagem(k), 9, 9)); | |
imgSabel = operadoresSabel(filtroGauss(criarImagem(k), 9, 9)); | |
imwrite(dirSaida + dirAtual + "/Laplaciano/" + "imgPreProcess.jpg", imgLaplaciano); | |
imwrite(dirSaida + dirAtual + "/Robert/" + "imgPreProcess.jpg", imgRobert); | |
imwrite(dirSaida + dirAtual + "/Prewitt/" + "imgPreProcess.jpg", imgPrewitt); | |
imwrite(dirSaida + dirAtual + "/Sabel/" + "imgPreProcess.jpg", imgSabel); | |
} | |
} | |
int main(){ | |
processarImagens(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Código bacana!!!