我正在研究一个涉及 3D 重建和 3D 场景渲染的项目,我已经完成了视差图像并使用 opencv 2.1 生成了 3D 坐标,之后我尝试使用点云库渲染 3D 场景我在这里导致代码崩溃。我正在使用 Windows 7 32 位。
我不明白为什么会出现这个错误,请帮助我。
这是我的代码
......
Mat imgDisparity16S ;
IplImage stub, *dst_img;
Mat imgDisparity8U ;
int ndisparities = 16*5;
int SADWindowSize = 5;
StereoBM sbm(StereoBM::BASIC_PRESET,ndisparities,SADWindowSize);
sbm( object, image, imgDisparity16S, CV_16S );
double minVal;
double maxVal;
minMaxLoc( imgDisparity16S, &minVal, &maxVal );
printf("Min disp: %f Max value: %f \n", minVal, maxVal);
imgDisparity16S.convertTo( imgDisparity8U, CV_8UC1, 255/(maxVal - minVal));
IplImage ipl_from_mat((IplImage)imgDisparity8U);
cvNamedWindow("window", CV_WINDOW_AUTOSIZE);
cvShowImage("window", &ipl_from_mat);
cvSaveImage("disparity.jpg",&ipl_from_mat);
cvWaitKey(0);
Mat xyz ;
cout<<"Here"<<endl;
cv::Mat img_disparity = cv::imread("disparity.jpg", CV_LOAD_IMAGE_GRAYSCALE);
double q[] =
{
1, 0, 0, -2.9615028381347656e+02,
0, 1, 0, -2.3373317337036133e+02,
0, 0, 0, 5.6446880931501073e+02,
0, 0, -1.1340974198400260e-01, 4.1658568844268817e+00,
};
cv::Mat Q = Mat(4, 4, CV_64F, q);
cv::Mat recons3D(img_disparity.size(), CV_32FC3);
cv::reprojectImageTo3D(imgDisparity8U,recons3D,Q,true);
cout<<"Here1"<<endl;
cout<<"Here2"<<endl;
saveXYZ("clouds.txt", recons3D);
std::cout<<"Creating point cloud"<<std::endl;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
double px, py, pz;
uchar pr, pg, pb;
double Q03, Q13, Q23, Q32, Q33;
uint32_t rgb1;
#ifdef CUSTOM_REPROJECT
Q03 = Q.at<double>(0,3);
Q13 = Q.at<double>(1,3);
Q23 = Q.at<double>(2,3);
Q32 = Q.at<double>(3,2);
Q33 = Q.at<double>(3,3);
#endif
cout<<"Before"<<endl;
cv::Mat img_rgb = Mat(imgDisparity8U.size(),CV_32FC3);
CvMat* img_rgb1 = cvLoadImageM("frame1_0.jpg",1);
cout<<object_filename<<endl;
img_rgb = img_rgb1;
for(int i=0;i<img_rgb.rows ;i++)
{
uchar* rgb_ptr = img_rgb.ptr<uchar>(i);
uchar* disp_ptr = imgDisparity8U.ptr<uchar>(i);
double* recons_ptr = recons3D.ptr<double>(i);
for(int j = 0; j < img_rgb.cols; j++)
{
uchar d = disp_ptr[j];
if ( d == 0 ) continue;
double pw = -1.0 * static_cast<double>(d) * Q32 + Q33;
px = static_cast<double>(j) + Q03;
py = static_cast<double>(i) + Q13;
pz = Q23;
px = px/pw;
py = py/pw;
pz = pz/pw;
px = recons_ptr[3*j];
py = recons_ptr[3*j+1];
pz = recons_ptr[3*j+2];
pb = rgb_ptr[3*j];
pg = rgb_ptr[3*j+1];
pr = rgb_ptr[3*j+2];
pcl::PointXYZRGB point;
point.x = px;
point.y = py;
point.z = pz;
rgb1 = (static_cast<uint32_t>(pr) << 16 || static_cast<uint32_t>(pg) << 8 || static_cast<uint32_t>(pb));
point.rgb = *reinterpret_cast<float*>(&rgb1);
point_cloud_ptr->points.push_back(point);
}
}
point_cloud_ptr->width = (int)point_cloud_ptr->points.size();
point_cloud_ptr->height = 1;
cout<<"outside loop"<<endl;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
cout<<"outside loop11"<<endl;
viewer = createVisualizer(point_cloud_ptr); //Error Here
while(!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep (boost::posix_time::microseconds (100000));
}
return 0;