Skip to content

Instantly share code, notes, and snippets.

@embs
Created April 10, 2014 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save embs/10406829 to your computer and use it in GitHub Desktop.
Save embs/10406829 to your computer and use it in GitHub Desktop.
Realidade Virtual
/****************************************************
OpenCV Base
Voxar Labs
Informatics Center - CIn
Federal University of Pernambuco - UFPE
http://www.cin.ufpe.br/voxarlabs
@author Rafael Roberto (rar3@cin.ufpe.br)
*****************************************************/
// Basic C++ includes
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <windows.h>
// Project includes
#include "util\Win32Timer.h"
// OpenCV includes
#include "opencv2\core\core.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\highgui\highgui.hpp"
// Declare the namespaces used
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
// Initialize the timers
Win32Timer timer = *( new Win32Timer() );
// Start the timer
timer.reset();
// Utilização básica da câmera
//VideoCapture capture(0);
//if(!capture.isOpened()) {
// cout << "Erro ao abrir camera" << endl;
// return -1;
//}
//capture.set(CV_CAP_PROP_FRAME_WIDTH, 800);
//double width = capture.get(CV_CAP_PROP_FRAME_WIDTH);
//Mat image;
//capture >> image;
// Abre imagem do disco
string path = "../resource/image.jpg";
Mat image_load = imread(path);
if(!image_load.data) {
cout << "Erro ao ler a imagem" << path << endl;
exit(-1);
}
// Transforma imagem para tom de cinza
Size image_size = image_load.size();
Mat gray_image(image_size, CV_8U);
cvtColor(image_load, gray_image, CV_RGB2GRAY);
// Transforma imagem para tons de cinza (usando apenas canal Red)
Mat red_image(image_size, CV_8U);
for(int x = 0; x < image_size.width; x++) {
for(int y = 0; y < image_size.height; y++) {
red_image.at<byte>(y, x) = image_load.at<Vec3b>(y, x)[0];
}
}
// Transforma imagem para versão binária
Mat bin_image(image_size, CV_8U);
threshold(gray_image, bin_image, 128, 255, CV_THRESH_BINARY);
// Extrai arestas com Sobel
Mat sobel_edge_image(image_size, CV_8U);
Sobel(bin_image, sobel_edge_image, CV_8U, 1, 1);
// Extrai arestas com Canny
Mat canny_edge_image(image_size, CV_8U);
Canny(bin_image, canny_edge_image, 500, 200);
// Encontra contornos
vector< vector<Point> > contours;
findContours(canny_edge_image, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
// Pinta contornos numa imagem
Mat final_image = image_load.clone();
Scalar color = Scalar(0, 0, 255);
for(int i = 0; i < 484; i++) {
drawContours(final_image, contours, i, color, 2);
}
// Exibe imagens
//imshow("First OpenCV img", image_load);
//imshow("Gray img OpenCV conversion", gray_image);
//imshow("Gray img OpenCV conversion Red channel only", red_image);
//imshow("Binary img OpenCV conversion", bin_image);
//imshow("Edge img OpenCV Sobel strategy", sobel_edge_image);
//imshow("Edge img OpenCV Canny strategy", canny_edge_image);
imshow("Contous colored img OpenCV", final_image);
waitKey();
// Print the experience time
cout << "Mean execution time: " << timer.getMilliseconds() << " ms." << endl;
cout << "Mean execution FPS: " << ( 1000.0 / timer.getMilliseconds() ) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment