mcv.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
27 /***
28  * \file mcv.cc
29  * \author Mohamed Aly <malaa@caltech.edu>
30  * \date 11/29/2006
31  */
32 
33 #include "mcv.hh"
34 
35 #include <iostream>
36 #include <math.h>
37 #include <assert.h>
38 
39 using namespace std;
40 using namespace cv;
41 
42 #include <cv.h>
43 #include <highgui.h>
44 
45 namespace LaneDetector
46 {
47 
48 //some helper functions for debugging
49 
50 //print the matrix passed to it
51 void SHOW_MAT(const CvMat *pmat, char str[])
52 {
53  cerr << str << "\n";
54  for(int i=0; i<pmat->rows; i++)
55  {
56  for(int j=0; j<pmat->cols; j++)
57  cerr << cvGetReal2D(pmat, i, j) << " ";
58  cerr << "\n";
59  }
60 }
61 
62 void SHOT_MAT_TYPE(const CvMat *pmat)
63 {
64  cout << CV_MAT_TYPE(pmat->type) << "\n";
65 }
66 
67 void SHOW_IMAGE(const CvMat *pmat, const char str[], int wait)
68 {
69  //cout << "channels:" << CV_MAT_CN(pmat->type) << "\n";
70  //scale it
71  //CvMat *mat = cvCreateMat(pmat->height, pmat->width, pmat->type);
72  //cvCopy(pmat, mat);
73  CvMat *mat = cvCloneMat(pmat);//->rows, pmat->cols, INT_MAT_TYPE);//cvCloneMat(pmat);
74  assert(mat);
75  //convert to int type
76  //cvConvert(pmat, mat);
77  if(CV_MAT_CN(mat->type) == 1)//FLOAT_MAT_TYPE)
78  mcvScaleMat(mat, mat);
79  //show it
80  //cout << "in\n";
81  cvNamedWindow(str, CV_WINDOW_AUTOSIZE); //0 1
82  cvShowImage(str, mat);
83  cvWaitKey(wait);
84  //cvDestroyWindow(str);
85  //clear
86  cvReleaseMat(&mat);
87  //cout << "out\n";
88 }
89 
90 void SHOW_IMAGE(const IplImage *pmat, char str[])
91 {
92  //cout << "channels:" << CV_MAT_CN(pmat->type) << "\n";
93  //scale it
94  //CvMat *mat = cvCreateMat(pmat->height, pmat->width, pmat->type);
95  //cvCopy(pmat, mat);
96  //CvMat *mat = cvCloneMat(pmat);
97  //assert(mat);
98  // mcvScaleMat(mat, mat);
99  //show it
100  //cout << "in\n";
101  cvNamedWindow(str, 1);
102  cvShowImage(str, pmat);
103  cvWaitKey(0);
104  //cvDestroyWindow(str);
105  //clear
106  //cvReleaseMat(&mat);
107  //cout << "out\n";
108 }
109 
110 void SHOW_IMAGE(Mat pmat, char str[])
111 {
112  //cout << "channels:" << CV_MAT_CN(pmat->type) << "\n";
113  //scale it
114  //CvMat *mat = cvCreateMat(pmat->height, pmat->width, pmat->type);
115  //cvCopy(pmat, mat);
116  //CvMat *mat = cvCloneMat(pmat);
117  //assert(mat);
118  // mcvScaleMat(mat, mat);
119  //show it
120  //cout << "in\n";
121 // cvNamedWindow(str, 1);
122  imshow(str, pmat);
123  waitKey(0);
124  //cvDestroyWindow(str);
125  //clear
126  //cvReleaseMat(&mat);
127  //cout << "out\n";
128 }
129 
130 void SHOW_POINT(const FLOAT_POINT2D pt, char str[])
131 {
132  cerr << str << "(" << pt.x << "," << pt.y << ")\n";
133  cerr.flush();
134 }
135 
136 
137 void SHOW_RECT(const CvRect rect, char str[])
138 {
139  cerr << str << "(x=" << rect.x << ", y=" << rect.y
140  << ", width=" << rect.width << ", height="
141  << rect.height << ")" << endl;
142  cerr.flush();
143 }
144 
154 void mcvLoadImage(const char *filename, CvMat **clrImage, CvMat** channelImage)
155 {
156  // load the image
157  IplImage* im;
158  im = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
159  // convert to mat and get first channel
160  CvMat temp;
161  cvGetMat(im, &temp);
162  *clrImage = cvCloneMat(&temp);
163  // convert to single channel
164  CvMat *schannel_mat;
165  CvMat* tchannelImage = cvCreateMat(im->height, im->width, INT_MAT_TYPE);
166  cvSplit(*clrImage, tchannelImage, NULL, NULL, NULL);
167  // convert to float
168  *channelImage = cvCreateMat(im->height, im->width, FLOAT_MAT_TYPE);
169  cvConvertScale(tchannelImage, *channelImage, 1./255);
170  // destroy
171  cvReleaseMat(&tchannelImage);
172  cvReleaseImage(&im);
173 }
174 
181 void mcvScaleMat(const CvMat *inMat, CvMat *outMat)
182 {
183  //convert inMat type to outMat type
184  cvConvert(inMat, outMat);
185  //if (CV_MAT_DEPTH(inMat->type)
186  //get the min and subtract it
187  double min;
188  cvMinMaxLoc(inMat, &min, 0, 0, 0, 0);
189  cvSubS(inMat, cvRealScalar(min), outMat);
190 
191  //get max and divide by it
192  double max;
193  cvMinMaxLoc(outMat, 0, &max, 0, 0, 0);
194  if(CV_MAT_TYPE(outMat->type) == FLOAT_MAT_TYPE)
195  cvConvertScale(outMat, outMat, 1/max);
196  else if(CV_MAT_TYPE(outMat->type) == INT_MAT_TYPE)
197  cvConvertScale(outMat, outMat, 255/max);
198 }
199 
207 template <class T>
208 CvMat* mcvVector2Mat(const vector<T> &vec)
209 {
210  CvMat *mat = 0;
211 
212  if (vec.size()>0)
213  {
214  //create the matrix
215  mat = cvCreateMat(vec.size(), 1, CV_64FC1);
216  //loop and get values
217  for (int i=0; i<(int)vec.size(); i++)
218  CV_MAT_ELEM(*mat, double, i, 0) =(double) vec[i];
219  }
220 
221  //return
222  return mat;
223 }
224 
225 // template CvMat* mcvVector2Mat(const vector<double> &vec);
226 // template CvMat* mcvVector2Mat(const vector<int> &vec);
227 
228 } // namespace LaneDetector
#define FLOAT_POINT2D
Definition: mcv.hh:33
void SHOW_RECT(const CvRect rect, char str[])
Definition: mcv.cpp:137
CvMat * mcvVector2Mat(const vector< T > &vec)
Definition: mcv.cpp:208
void SHOT_MAT_TYPE(const CvMat *pmat)
Definition: mcv.cpp:62
#define FLOAT_MAT_TYPE
Definition: mcv.hh:21
void mcvScaleMat(const CvMat *inMat, CvMat *outMat)
Definition: mcv.cpp:181
void mcvLoadImage(const char *filename, CvMat **clrImage, CvMat **channelImage)
Definition: mcv.cpp:154
void SHOW_MAT(const CvMat *pmat, char str[])
Definition: mcv.cpp:51
#define INT_MAT_TYPE
Definition: mcv.hh:24
void SHOW_POINT(const FLOAT_POINT2D pt, char str[])
Definition: mcv.cpp:130
void SHOW_IMAGE(Mat pmat, char str[])
Definition: mcv.cpp:110


caltech_lanes
Author(s): Ricardo Morais
autogenerated on Mon Mar 2 2015 01:31:31