std::cout<<"Capturing frame"<<std::endl;frame=camera.capture2D3D(settings);constautoframe2D=frame.frame2D();if(!frame2D.has_value()){throwstd::runtime_error("Captured frame does not contain a 2D image.");}std::cout<<"Copying colors with Zivid API from GPU to CPU"<<std::endl;autocolors=frame2D->imageBGRA_SRGB();std::cout<<"Casting the data pointer as a void*, since this is what the OpenCV matrix constructor requires."<<std::endl;auto*dataPtrZividAllocated=const_cast<void*>(static_cast<constvoid*>(colors.data()));std::cout<<"Wrapping this block of data in an OpenCV matrix. This is possible since the layout of \n"<<"Zivid::ColorBGRA_SRGB exactly matches the layout of CV_8UC4. No copying occurs in this step."<<std::endl;constcv::MatbgraZividAllocated(colors.height(),colors.width(),CV_8UC4,dataPtrZividAllocated);std::cout<<"Displaying image"<<std::endl;cv::imshow("BGRA image Zivid Allocated",bgraZividAllocated);cv::waitKey(CI_WAITKEY_TIMEOUT_IN_MS);
std::cout<<"Allocating the necessary storage with OpenCV API based on resolution info before any capturing"<<std::endl;autobgraUserAllocated=cv::Mat(resolution.height(),resolution.width(),CV_8UC4);std::cout<<"Capturing frame"<<std::endl;autoframe=camera.capture2D3D(settings);autopointCloud=frame.pointCloud();std::cout<<"Copying data with Zivid API from the GPU into the memory location allocated by OpenCV"<<std::endl;pointCloud.copyData(&(*bgraUserAllocated.begin<Zivid::ColorBGRA_SRGB>()));std::cout<<"Displaying image"<<std::endl;cv::imshow("BGRA image User Allocated",bgraUserAllocated);cv::waitKey(CI_WAITKEY_TIMEOUT_IN_MS);
You can mask the point cloud to keep only a specific region of interest.
A typical use case is to capture a 2D image, run it through a segmentation algorithm to produce a binary mask, and apply it to the 3D point cloud.
Non-zero values in the mask invalidate the corresponding points (set to NaN), while zero values preserve them.
The mask can be applied using PointCloud::mask() (in-place) or PointCloud::masked() (returns a new copy), on both PointCloud and Frame.
// Create a ones-filled maskZivid::Maskmask(resolution);// Calculate rectangle boundsconstintheight=static_cast<int>(resolution.height());constintwidth=static_cast<int>(resolution.width());constintheightMin=(height-pixelsToDisplay)/2;constintheightMax=(height+pixelsToDisplay)/2;constintwidthMin=(width-pixelsToDisplay)/2;constintwidthMax=(width+pixelsToDisplay)/2;// Create OpenCV Mat wrapper for the mask datacv::MatmaskMat(height,width,CV_8UC1,mask.data());// Draw filled rectangle on the mask to unmask the central regioncv::rectangle(maskMat,cv::Point(widthMin,heightMin),cv::Point(widthMax,heightMax),cv::Scalar(0),cv::FILLED);returnmask;automaskedPointCloud=pointCloud.masked(mask);
// Create a ones-filled maskvarmask=newZivid.NET.Mask(resolution);// Calculate rectangle boundsintheight=(int)resolution.Height;intwidth=(int)resolution.Width;intheightMin=(height-pixelsToDisplay)/2;intheightMax=(height+pixelsToDisplay)/2;intwidthMin=(width-pixelsToDisplay)/2;intwidthMax=(width+pixelsToDisplay)/2;// Set pixels inside the rectangle to zerofor(inty=heightMin;y<heightMax;++y){for(intx=widthMin;x<widthMax;++x){mask[x,y]=0;}}returnmask;varmaskedPointCloud=pointCloud.Masked(mask);
pixels_to_display=300print(f"Generating binary mask of central {pixels_to_display} x {pixels_to_display} pixels")height=frame.point_cloud().heightwidth=frame.point_cloud().widthmask=np.ones((height,width),bool)h_min=(height-pixels_to_display)//2h_max=(height+pixels_to_display)//2w_min=(width-pixels_to_display)//2w_max=(width+pixels_to_display)//2mask[h_min:h_max,w_min:w_max]=0print("Masking point cloud")point_cloud.mask(mask)
// Create a circular mask in OpenCVconstintcenterX=static_cast<int>(resolution.width())/2;constintcenterY=static_cast<int>(resolution.height())/2;constintradius=pixelsToDisplay;cv::circle(opencvMask,cv::Point(centerX,centerY),radius,cv::Scalar(0),cv::FILLED);// Convert OpenCV mask to Zivid::MaskconstZivid::MaskcircularMask(resolution,opencvMask.datastart,opencvMask.dataend);
std::cout<<"Setting up visualization"<<std::endl;Zivid::Visualization::Visualizervisualizer;std::cout<<"Visualizing point cloud"<<std::endl;visualizer.showMaximized();visualizer.show(frame);visualizer.resetToFit();std::cout<<"Running visualizer. Blocking until window closes."<<std::endl;visualizer.run();
Console.WriteLine("Setting up visualization");using(varvisualizer=newZivid.NET.Visualization.Visualizer()){Console.WriteLine("Visualizing point cloud");visualizer.Show(frame);visualizer.ShowMaximized();visualizer.ResetToFit();Console.WriteLine("Running visualizer. Blocking until window closes.");visualizer.Run();}
print("Visualizing point cloud")withzivid.visualization.Visualizer()asvisualizer:visualizer.set_window_title("Zivid Point Cloud Visualizer")visualizer.colors_enabled=Truevisualizer.axis_indicator_enabled=Truevisualizer.show(frame)visualizer.reset_to_fit()visualizer.run()
std::cout<<"Getting point cloud from frame"<<std::endl;autopointCloud=frame.pointCloud();std::cout<<"Setting up visualization"<<std::endl;Zivid::Visualization::Visualizervisualizer;std::cout<<"Visualizing point cloud"<<std::endl;visualizer.showMaximized();visualizer.show(pointCloud);visualizer.resetToFit();std::cout<<"Running visualizer. Blocking until window closes."<<std::endl;visualizer.run();
Console.WriteLine("Getting point cloud from frame");varpointCloud=frame.PointCloud;Console.WriteLine("Setting up visualization");varvisualizer=newZivid.NET.Visualization.Visualizer();Console.WriteLine("Visualizing point cloud");visualizer.Show(pointCloud);visualizer.ShowMaximized();visualizer.ResetToFit();Console.WriteLine("Running visualizer. Blocking until window closes.");visualizer.Run();
point_cloud=frame.point_cloud()withzivid.visualization.Visualizer()asvisualizer:visualizer.set_window_title("Zivid Point Cloud Visualizer")visualizer.colors_enabled=Truevisualizer.axis_indicator_enabled=Truevisualizer.show(point_cloud)visualizer.reset_to_fit()visualizer.run()