Commit 43b13816 authored by Aylen Ricca's avatar Aylen Ricca
Browse files

test working -untidy

parent c9003902
all: prueba0
prueba0: prueba0.cpp
g++ `pkg-config opencv cvblob --cflags --libs` prueba0.cpp -o prueba0
prueba1: prueba1.cpp
g++ `pkg-config opencv cvblob --cflags --libs` prueba1.cpp -o prueba1
all: prueba1.cpp functions.cpp
g++ `pkg-config opencv cvblob --cflags --libs` prueba1.cpp functions.cpp -o prueba1
// Aylen Ricca <aricca@fing.edu.uy>
// Nicolas Furquez
// prueba 1 - understanding cvBlob functionalities
#include <iostream>
#include <fstream>
#include <iomanip>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <cvblob.h>
using namespace cv;
using namespace cvb;
void getOneColorBlobs (const IplImage *hsv, IplImage * labelImg, CvBlobs &blobs, CvSize imgSize, cv::Scalar min, cv::Scalar max) {
IplConvKernel* morphKernel = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT, NULL);
IplImage *segmentated = cvCreateImage(imgSize, 8, 1);
cvInRangeS(hsv,min,max,segmentated);
cvErode(segmentated, segmentated, morphKernel, 1);
cvDilate(segmentated, segmentated, morphKernel, 3);
// cvShowImage(".::.ERODE+DILATE.::.", segmentated);
// obtiene blobs, devuelve img etiquetada y el map (etiq, blob)
unsigned int result = cvLabel(segmentated, labelImg, blobs);
// filtro por area para quedarme con los mas grandes
cvFilterByArea(blobs, 100, 1000000);
cvReleaseImage(&segmentated);
cvReleaseStructuringElement(&morphKernel);
}
void saveBlobs (unsigned int frameNumber, CvBlobs blobs) {
std::stringstream textfile, line;
std::ofstream myfile;
textfile << "blobs_" << std::setw(5) << std::setfill('0') << frameNumber;
myfile.open (textfile.str().c_str(),std::ios_base::app);
// LINE TO SAVE FOR EACH BLOB:
//label|area|centroidX,Y|minX,maxX,minY,maxY|moment00,01,10,11,20,02|centroidMoment11,20,02|
//normalaizedCentralMoment11,20,02|HuMoment1,2
for (CvBlobs::const_iterator it=blobs.begin(); it!=blobs.end(); ++it)
{
CvBlob* blob = (*it).second;
line << (*blob).label << "|" << (*blob).area << "|" << (*blob).centroid.x << "," << (*blob).centroid.y <<"|" << (*blob).minx << "," << (*blob).maxx << "," << (*blob).miny << "," << (*blob).maxy << ","<< "|" << (*blob).m00 << "," << (*blob).m01 << "," << (*blob).m10 << "," << (*blob).m11 << "," << (*blob).m20 << "," << (*blob).m02 << "|" << (*blob).u11 << "," << (*blob).u20 << "," << (*blob).u02 << "|" << (*blob).n11 << "|" << (*blob).n20 << "|" << (*blob).n02 << "|" << (*blob).p1 << "|" << (*blob).p2 << std::endl;
myfile << line.str();
line.clear();
line.str(std::string());
/*// SAVE EACH BLOB AS AN IMAGE
std::stringstream filename;
filename << "blob_" << std::setw(5) << std::setfill('0') << blobNumber << ".png";
cvSaveImageBlob(filename.str().c_str(), img, it->second);
blobNumber++;
std::cout << filename.str() << " saved!" << std::endl;
*/
}
myfile.close();
}
// Aylen Ricca <aricca@fing.edu.uy>
// Nicolas Furquez
// prueba 1 - understanding cvBlob functionalities
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <cvblob.h>
using namespace cv;
using namespace cvb;
void getOneColorBlobs (const IplImage *hsv, IplImage * labelImg, CvBlobs &blobs, CvSize imgSize, cv::Scalar min, cv::Scalar max);
void saveBlobs (unsigned int frameNumber, CvBlobs blobs);
#endif
......@@ -77,8 +77,10 @@ def thresholdImage(img_hsv, minHue = 0 , maxHue = 180, minSaturation = 0 , maxSa
cv.NamedWindow("camera", 0)
if captureFromCam:
capture = cv.CreateCameraCapture(cameraId)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
print cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT)
print cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH)
#capture = cv.CaptureFromCAM(cameraId)
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 1, 0, 2, 8)
cv.SetMouseCallback("camera",on_mouse, 0);
......@@ -110,7 +112,8 @@ while True:
cv.ShowImage("camera", src)
key = cv.WaitKey(33)
if key == 537919515:
#print key
if key == 1048603:
break
# Clean up everything before leaving
......
......@@ -8,6 +8,7 @@
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <cvblob.h>
#include "functions.h"
using namespace cv;
using namespace cvb;
......@@ -22,6 +23,11 @@ int main()
std::cout << "== ERROR 0 - revisar conexion a camara." << std::endl;
return -1;
}
cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH,640);
cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT,480);
cvSetCaptureProperty(capture,CV_CAP_PROP_FPS,5);
std::cout << "WIDTH " << cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH) << " HEIGHT " << cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT) << std::endl;
// get capture from camera - img should not be released by us
if (!cvGrabFrame(capture)) {
......@@ -35,11 +41,12 @@ int main()
IplImage *frame = cvCreateImage(imgSize, img->depth, img->nChannels);
// morphological kernel for opening
IplConvKernel* morphKernel = cvCreateStructuringElementEx(5, 5, 1, 1, CV_SHAPE_RECT, NULL);
//IplConvKernel* morphKernel = cvCreateStructuringElementEx(5, 5, 1, 1, CV_SHAPE_RECT, NULL);
//IplConvKernel* morphKernel = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT, NULL);
unsigned int frameNumber = 0;
unsigned int blobNumber = 0;
unsigned int trie = 1;
bool quit = false;
while (!quit && cvGrabFrame(capture)){
......@@ -47,8 +54,15 @@ int main()
IplImage *img = cvRetrieveFrame(capture);
cvConvertScale(img, frame, 1, 0);
if (trie == 1){
std::stringstream name;
name << "blobs_original" << std::setw(5) << std::setfill('0') << frameNumber << ".png";
cvSaveImage(name.str().c_str(),img);
trie ++;
}
// SEGMENTACION
IplImage *segmentated = cvCreateImage(imgSize, 8, 1);
// IplImage *segmentated = cvCreateImage(imgSize, 8, 1);
IplImage *hsv = cvCreateImage(imgSize, 8, 3);
// transformo a espacio HSV
......@@ -56,32 +70,39 @@ int main()
// umbralizo para obtener img binaria con regiones de interes
// cvInRangeS(hsv,cv::Scalar(21, 97, 24),cv::Scalar(28, 255, 201),segmentated);
cvInRangeS(hsv,cv::Scalar(0, 126, 106),cv::Scalar(179, 239, 249),segmentated);
// cvInRangeS(hsv,cv::Scalar(21, 161, 126),cv::Scalar(27, 255, 226),segmentated); //poste amarillo
//oper apertura - morphology
IplImage *segmentated2 = cvCreateImage(imgSize, 8, 1);
cvMorphologyEx(segmentated, segmentated2, NULL, morphKernel, CV_MOP_OPEN, 1);
cvErode(segmentated, segmentated, morphKernel, 1);
cvDilate(segmentated, segmentated, morphKernel, 3);
//IplImage *segmentated2 = cvCreateImage(imgSize, 8, 1);
//cvMorphologyEx(segmentated, segmentated2, NULL, morphKernel, CV_MOP_OPEN, 1);
// cvErode(segmentated, segmentated, morphKernel, 1);
// cvDilate(segmentated, segmentated, morphKernel, 3);
cvShowImage(".::.segmentated.::.", segmentated);
cvShowImage(".::.OPEN.::.", segmentated2);
// cvShowImage(".::.ERODE+DILATE.::.", segmentated);
//cvShowImage(".::.OPEN.::.", segmentated2);
// OBTENER BLOBS
IplImage *labelImg = cvCreateImage(cvGetSize(frame), IPL_DEPTH_LABEL, 1);
CvBlobs blobs;
IplImage *labelImg1 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_LABEL, 1);
CvBlobs blobs1;
// obtiene blobs, devuelve img etiquetada y el map (etiq, blob)
unsigned int result = cvLabel(segmentated, labelImg, blobs);
// unsigned int result = cvLabel(segmentated, labelImg, blobs);
// filtro por area para quedarme con los mas grandes
cvFilterByArea(blobs, 100, 1000000);
// cvFilterByArea(blobs, 100, 1000000);
getOneColorBlobs(hsv, labelImg, blobs, imgSize, cv::Scalar(21, 161, 126),cv::Scalar(27, 255, 226) );
// VISUALIZACION
cvRenderBlobs(labelImg, blobs, frame, frame, CV_BLOB_RENDER_BOUNDING_BOX);
//cvUpdateTracks(blobs, tracks, 200., 5);
//cvRenderTracks(tracks, frame, frame, CV_TRACK_RENDER_ID|CV_TRACK_RENDER_BOUNDING_BOX);
getOneColorBlobs(hsv, labelImg1, blobs1, imgSize, cv::Scalar(95, 106, 45),cv::Scalar(114, 221, 112) );
cvRenderBlobs(labelImg1, blobs1, frame, frame, CV_BLOB_RENDER_BOUNDING_BOX);
cvShowImage(".::.futbot.::.", frame);
char k = cvWaitKey(10)&0xff;
......@@ -92,48 +113,29 @@ int main()
case 'Q':
quit = true;
break;
case 'a':
case 'A':
break;
case 's':
case 'S':
std::stringstream filename, textfile, line;
filename << "blobs_" << std::setw(5) << std::setfill('0') << frameNumber << ".png";
textfile << "blobs_" << std::setw(5) << std::setfill('0') << frameNumber;
cvSaveImage(filename.str().c_str(),frame);
std::ofstream myfile;
myfile.open (textfile.str().c_str());
// LINE TO SAVE FOR EACH BLOB:
//label|area|centroidX,Y|minX,maxX,minY,maxY|moment00,01,10,11,20,02|centroidMoment11,20,02|
//normalaizedCentralMoment11,20,02|HuMoment1,2
for (CvBlobs::const_iterator it=blobs.begin(); it!=blobs.end(); ++it)
{
CvBlob* blob = (*it).second;
line << (*blob).label << "|" << (*blob).area << "|" << (*blob).centroid.x << "," << (*blob).centroid.y <<"|" << (*blob).minx << "," << (*blob).maxx << "," << (*blob).miny << "," << (*blob).maxy << ","<< "|" << (*blob).m00 << "," << (*blob).m01 << "," << (*blob).m10 << "," << (*blob).m11 << "," << (*blob).m20 << "," << (*blob).m02 << "|" << (*blob).u11 << "," << (*blob).u20 << "," << (*blob).u02 << "|" << (*blob).n11 << "|" << (*blob).n20 << "|" << (*blob).n02 << "|" << (*blob).p1 << "|" << (*blob).p2 << std::endl;
myfile << line.str();
line.clear();
line.str(std::string());
/*// SAVE EACH BLOB AS AN IMAGE
std::stringstream filename;
filename << "blob_" << std::setw(5) << std::setfill('0') << blobNumber << ".png";
cvSaveImageBlob(filename.str().c_str(), img, it->second);
blobNumber++;
filename << "blobs_" << std::setw(5) << std::setfill('0') << frameNumber << ".png";
cvSaveImage(filename.str().c_str(),frame);
saveBlobs(frameNumber,blobs);
saveBlobs(frameNumber,blobs);
std::cout << filename.str() << " saved!" << std::endl;
*/
}
std::cout << filename.str() << " saved!" << std::endl;
frameNumber++;
myfile.close();
frameNumber++;
break;
}
cvReleaseImage(&labelImg);
cvReleaseImage(&segmentated);
// cvReleaseImage(&segmentated);
cvReleaseImage(&hsv);
//cvReleaseImage(&img);
cvReleaseBlobs(blobs);
}
cvReleaseStructuringElement(&morphKernel);
// cvReleaseStructuringElement(&morphKernel);
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment