9 inline CvSize GetImageSize( IplImage* img )
11 CvSize size = { 0, 0 };
12 if( img ) cvGetImageRawData( img, 0, 0, &size );
17 inline IplImage* CreateIsometricImage( IplImage* src, IplImage* dst,
18 int desired_depth, int desired_num_channels )
20 CvSize src_size = GetImageSize( src ),
23 if( dst ) dst_size = GetImageSize( dst );
25 if( !dst || dst->depth != desired_depth ||
26 dst->nChannels != desired_num_channels ||
27 dst_size.width != src_size.width ||
28 dst_size.height != dst_size.height )
30 cvReleaseImage( &dst );
31 dst = cvCreateImage( src_size, desired_depth, desired_num_channels );
37 // Basic class for ViewMorphing
43 IplImage* m_right_img;
44 IplImage* m_virtual_img;
45 IplImage* m_disparity_img;
48 bool m_isVirtualImageReady;
50 bool m_isDisparityReady;
54 m_left_img = m_right_img = m_virtual_img = m_disparity_img = 0;
56 m_isVirtualImageReady = m_isStereoReady =
57 m_isDisparityReady = false;
60 virtual void ResetStereo()
62 m_isVirtualImageReady = m_isStereoReady =
63 m_isDisparityReady = false;
66 virtual void Release()
68 cvReleaseImage( &m_left_img );
69 cvReleaseImage( &m_right_img );
70 cvReleaseImage( &m_virtual_img );
71 cvReleaseImage( &m_disparity_img );
75 virtual bool OnCalculateStereo()
80 virtual bool OnCalculateVirtualImage()
85 virtual bool CalculateStereo()
87 CvSize left_size = GetImageSize(m_left_img),
88 right_size = GetImageSize(m_right_img);
90 if( !m_left_img || !m_right_img ||
91 m_left_img->depth != m_right_img->depth ||
92 m_left_img->depth != IPL_DEPTH_8U ||
93 m_left_img->nChannels != m_right_img->nChannels ||
94 m_left_img->nChannels != 3 ||
95 left_size.width != right_size.width ||
96 left_size.height != right_size.height ) return false;
98 return OnCalculateStereo();
101 virtual bool CalculateVirtualImage()
103 m_virtual_img = CreateIsometricImage( m_left_img,
106 m_left_img -> nChannels
108 return OnCalculateVirtualImage();
111 virtual bool OnCalculateDisparity()
116 virtual bool CalculateDisparity()
118 if( !m_isStereoReady )
120 m_isStereoReady = CalculateStereo();
122 m_isDisparityReady = m_isStereoReady && OnCalculateDisparity();
124 return m_isDisparityReady;
127 virtual bool OnCalculateDisparityImage()
132 virtual bool CalculateDisparityImage()
134 m_disparity_img = CreateIsometricImage( m_left_img,
137 m_left_img -> nChannels
139 if( !m_isDisparityReady ) {
140 m_isDisparityReady = CalculateDisparity();
142 OnCalculateDisparityImage();
147 virtual void SetImage( IplImage* src, IplImage*& dst,
148 bool substitute_pointer = false )
152 if( substitute_pointer )
154 cvReleaseImage( &dst );
159 dst = CreateIsometricImage( src, dst, src->depth, src->nChannels );
173 virtual ~CCvMorpher()
178 virtual void SetLeftImage( IplImage* img, bool substitute_pointer = false )
180 SetImage( img, m_left_img, substitute_pointer );
183 virtual void SetRightImage( IplImage* img, bool substitute_pointer = false )
185 SetImage( img, m_right_img, substitute_pointer );
188 virtual IplImage* GetLeftImage() { return m_left_img; }
189 virtual IplImage* GetRightImage() { return m_right_img; }
191 virtual IplImage* GetVirtualImage()
193 if( !m_isVirtualImageReady )
195 if( !m_isStereoReady )
197 m_isStereoReady = CalculateStereo();
199 m_isVirtualImageReady = m_isStereoReady && CalculateVirtualImage();
201 return m_virtual_img;
204 virtual IplImage* GetDisparityImage()
206 if( !m_isStereoReady ) {
207 m_isStereoReady = CalculateStereo();
209 CalculateDisparityImage();
211 return m_disparity_img;
214 virtual void SetPan( float pan )
216 assert( 0.f <= pan && pan <= 1.f );
219 m_isVirtualImageReady = false;
222 float GetPan() { return m_pan; };
226 // Subdivision + LK - based viewmorphing
227 class CCvSubdivMorpher : public CCvMorpher
234 CvSubdiv2D* m_subdiv;
235 CvPoint2D32f* m_left_points;
236 CvPoint2D32f* m_right_points;
237 IplImage* m_left_gray_img;
238 IplImage* m_right_gray_img;
239 IplImage* m_left_pyr;
240 IplImage* m_right_pyr;
243 CvTermCriteria m_criteria;
247 m_left_points = m_right_points = 0;
248 m_count = m_maxcount = 0;
251 virtual void ReleasePoints()
253 cvClearGraph( (CvGraph*)m_subdiv );
254 delete m_left_points;
255 delete m_right_points;
263 m_left_gray_img = m_right_gray_img = 0;
264 m_left_pyr = m_right_pyr = 0;
268 virtual void Release()
270 CCvMorpher::Release();
272 cvReleaseImage( &m_left_gray_img );
273 cvReleaseImage( &m_right_gray_img );
274 cvReleaseImage( &m_left_pyr );
275 cvReleaseImage( &m_right_pyr );
279 virtual bool OnCalculateStereo();
280 virtual bool OnCalculateVirtualImage();
281 virtual bool GeneratePoints();
282 virtual void MorphFacet( CvSubdiv2DEdge edge );
288 int GetPointCount() { return m_count; }
289 void SetPointCount( int count )
291 if( count > m_maxcount )
294 m_left_points = new CvPoint2D32f[count];
295 m_right_points = new CvPoint2D32f[count];
303 // Pixel to pixel morphing
304 class CCvPixelToPixelMorpher : public CCvMorpher
307 int m_maxPixelDifference;
327 morphNormalImage = 0,
332 int* corrAlloc( int width, int height, corrArrayType type, int maxPixelDifference = 0 );
333 void FindFullCorr( unsigned char* _leftImage,
335 unsigned char* _rightImage,
341 int maxPixelDifference
343 void FindCorr( unsigned char* _leftLine,
344 unsigned char* _rightLine,
352 void Morph( unsigned char* _leftImage,
354 unsigned char* _rightImage,
356 unsigned char* _resultImage,
363 morphImageType imageNeed = morphNormalImage,
364 int maxDifference = 0
367 int distance( unsigned char* _leftLine,
368 unsigned char* _rightLine,
377 m_maxPixelDifference = 10;
378 m_corr = m_corrNum = 0;
381 virtual void Release()
383 CCvMorpher::Release();
393 virtual bool OnCalculateStereo();
394 virtual bool OnCalculateVirtualImage();
395 virtual bool OnCalculateDisparity();
396 virtual bool OnCalculateDisparityImage();
400 CCvPixelToPixelMorpher();