Personal tools
Document Actions

opencv 2 camera hough xform

by aullmj — last modified 2009-03-03 15:06

opens two cameras with cvcam and runs hough transform on both images

Click here to get the file

Size 5.5 kB - File type text/x-c++src

File contents

//Program demonstrates use of openCV's cvcam library to get images from two color cameras.
//By Mark Aull, 2009

#include "cvcam.h"
#include "cv.h"
#include "highgui.h"
#include <conio.h>
#include <iostream>
using namespace std;

void RunCams(void);
void stereocallback(IplImage* imagel, IplImage* imager);

int ct =0;
int edge_thresh = 50;
IplImage *cedgel = 0, *grayl = 0, *edgel = 0,*himagel;
IplImage *cedger = 0, *grayr = 0, *edger = 0,*himager;
CvMemStorage* storage;

int main(int argc, char *argv[])
{
    // Create the output images
    cedgel = cvCreateImage(cvSize(320,240/*imagel->width,imagel->height*/), IPL_DEPTH_8U, 3);
	cedger = cvCreateImage(cvSize(320,240/*imagel->width,imagel->height*/), IPL_DEPTH_8U, 3);
    grayl = cvCreateImage(cvSize(320,240/*imagel->width,imagel->height*/), IPL_DEPTH_8U, 1);
    edgel = cvCreateImage(cvSize(320,240/*imagel->width,imagel->height*/), IPL_DEPTH_8U, 1);
    grayr = cvCreateImage(cvSize(320,240/*imagel->width,imagel->height*/), IPL_DEPTH_8U, 1);
    edger = cvCreateImage(cvSize(320,240/*imagel->width,imagel->height*/), IPL_DEPTH_8U, 1);
	himagel=cvCreateImage(cvSize(320,240/*imagel->width,imagel->height*/), IPL_DEPTH_8U, 3);
	himager=cvCreateImage(cvSize(320,240/*imagel->width,imagel->height*/), IPL_DEPTH_8U, 3);
	storage = cvCreateMemStorage(0);

	RunCams();
	RunCams();

	return 0;
}

void RunCams(void)
{
	//get camera count
	int ncams = cvcamGetCamerasCount();
	cout <<ncams<<endl;
	int cam_1 = 0;
	int cam_2 = 1;

	//make output windows
	cvNamedWindow("Camera 1", CV_WINDOW_AUTOSIZE);
	HWND MyWin_1 = (HWND)cvGetWindowHandle("Camera 1");
	cvNamedWindow("Camera 2", CV_WINDOW_AUTOSIZE);
	HWND MyWin_2 = (HWND)cvGetWindowHandle("Camera 2");

    //cvNamedWindow("left gray image", 1);
    cvNamedWindow("left edge", 1);
    cvNamedWindow("left hough", 1);
    //cvNamedWindow("right gray image", 1);
    cvNamedWindow("right edge", 1);
    cvNamedWindow("right hough", 1);

	//set properties
	cvcamSetProperty(cam_1, CVCAM_PROP_ENABLE, CVCAMTRUE);
	cvcamSetProperty(cam_1, CVCAM_PROP_RENDER, CVCAMTRUE);
	cvcamSetProperty(cam_2, CVCAM_PROP_ENABLE, CVCAMTRUE);
	cvcamSetProperty(cam_2, CVCAM_PROP_RENDER, CVCAMTRUE);

	cvcamSetProperty(cam_1, CVCAM_PROP_WINDOW, &MyWin_1);
	cvcamSetProperty(cam_2, CVCAM_PROP_WINDOW, &MyWin_2);

	cvcamSetProperty(cam_2, CVCAM_STEREO_CALLBACK, (void*)stereocallback);
	// this line must be replaced if using a single camera:
	// Replace the stereocallback by callback when using only 1 webcam
	//cvcamSetProperty(0, CVCAM_PROP_CALLBACK, (void*)callback);
	//this callback will treat each frame seperately

	//cvWaitKey(0);
	cvcamInit();
	if (cvcamStart()>0)
	{
		//while (getch()!=27){Sleep(100);}
		cvWaitKey(0);
		cvcamStop();
		cvcamExit();
	}

	cvDestroyWindow("Camera 1");
	cvDestroyWindow("Camera 2");
	cvDestroyWindow("left edge");
    cvDestroyWindow("left hough");
    cvDestroyWindow("right edge");
    cvDestroyWindow("right hough");
}

void stereocallback(IplImage* imagel, IplImage* imager)
{
    // Convert to grayscale
    cvCvtColor(imagel, grayl, CV_BGR2GRAY);
    cvCvtColor(imager, grayr, CV_BGR2GRAY);
    cvFlip(grayl);
    cvFlip(grayr);
    //cvShowImage("left gray image", grayl);
    //cvShowImage("right gray image", grayr);

	//smooth
	cvSmooth( grayl, edgel, CV_BLUR, 3, 3, 0, 0 );
    cvNot( grayl, edgel );
    cvSmooth( grayr, edger, CV_BLUR, 3, 3, 0, 0 );
    cvNot( grayr, edger );

    // Run the edge detector on grayscale
    cvCanny(grayl, edgel, (float)edge_thresh, (float)edge_thresh*3, 3);
    cvCanny(grayr, edger, (float)edge_thresh, (float)edge_thresh*3, 3);

    // copy edge points
    cvZero( cedgel );
    cvZero( cedger );
    cvCopy( imagel, cedgel, edgel );
    cvCopy( imager, cedger, edger );

    cvShowImage("left edge", cedgel);
    cvShowImage("right edge", cedger);

    cvCopy( imagel, himagel);
    cvCopy( imager, himager);
    CvSeq* lines = 0;
	
	lines = cvHoughLines2(edgel, storage, CV_HOUGH_STANDARD, 1, CV_PI/180, 100, 0, 0 );
	cvFlip(himagel);
    for(int i = 0; i < MIN(lines->total,100); i++ )
    {
        float* line = (float*)cvGetSeqElem(lines,i);
        float rho = line[0];
        float theta = line[1];
        CvPoint pt1, pt2;
        double a = cos(theta), b = sin(theta);
        double x0 = a*rho, y0 = b*rho;
        pt1.x = cvRound(x0 + 1000*(-b));
        pt1.y = cvRound(y0 + 1000*(a));
        pt2.x = cvRound(x0 - 1000*(-b));
        pt2.y = cvRound(y0 - 1000*(a));
        cvLine(himagel, pt1, pt2, CV_RGB(255,0,0), 3, 8 );
    }	
	lines = cvHoughLines2(edger, storage, CV_HOUGH_STANDARD, 1, CV_PI/180, 100, 0, 0 );
    cvFlip(himager);
    for(int i = 0; i < MIN(lines->total,100); i++ )
    {
        float* line = (float*)cvGetSeqElem(lines,i);
        float rho = line[0];
        float theta = line[1];
        CvPoint pt1, pt2;
        double a = cos(theta), b = sin(theta);
        double x0 = a*rho, y0 = b*rho;
        pt1.x = cvRound(x0 + 1000*(-b));
        pt1.y = cvRound(y0 + 1000*(a));
        pt2.x = cvRound(x0 - 1000*(-b));
        pt2.y = cvRound(y0 - 1000*(a));
        cvLine(himager, pt1, pt2, CV_RGB(255,0,0), 3, 8 );
    }
    cvShowImage("left hough", himagel);
    cvShowImage("right hough", himager);
}
/*void stereocallback(IplImage* image1, IplImage* image2)
{
int i,j;
char file[255];
sprintf(file, "../images/Lefti%i.bmp",ct);
cvSaveImage(file,image1);
sprintf(file, "../images/righti%i.bmp",ct);
cvSaveImage(file,image2);
ct++;

}*/