Skip to content

Commit

Permalink
Implement Frame.frame2d
Browse files Browse the repository at this point in the history
  • Loading branch information
johningve committed Nov 28, 2024
1 parent 34c46f0 commit 2f8225f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
31 changes: 31 additions & 0 deletions modules/zivid/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from zivid.camera_state import _to_camera_state
from zivid.frame_info import _to_frame_info
from zivid.point_cloud import PointCloud
from zivid.frame_2d import Frame2D


class Frame:
Expand Down Expand Up @@ -52,6 +53,36 @@ def point_cloud(self):
"""
return PointCloud(self.__impl.point_cloud())

@property
def frame2d(self):
"""Get 2D frame from 2D+3D frame.
If the frame is the result of a 2D+3D capture, this method returns the 2D frame contained in the 2D+3D frame. In
the case of a 3D-only capture, this method returns None.
If the frame was captured by an SDK version prior to 2.14.0, then this method will return None.
If the 2D frame is not yet available because the capture is still in-progress, then this method will block until
acquisition of the entire 2D+3D capture is done. If you need to access the 2D frame before the 3D acquisition
has finished, then it is required to do separate 2D and 3D captures.
In a 2D+3D capture, the 2D color image and 3D point cloud may have different resolutions depending on the pixel
sampling settings used. The 2D pixel sampling setting determines the resolution of the 2D color image whereas
the 3D pixel sampling setting and the resampling setting determines the resolution of the 3D point cloud. The 2D
color image returned in this 2D frame will always have the same resolution as the 2D color image that was
captured. On the other hand, the point cloud colors will be sampled from the 2D color image to match the
resolution of the 3D point cloud. The point cloud colors will always have a 1:1 correspondence with the 3D point
cloud resolution. See `PointCloud` for more information.
Returns:
A Frame instance containing the 2D frame, or None if the frame was captured without 2D color or by an SDK
version prior to 2.14.0.
"""
return (
Frame2D(self.__impl.frame2d())
if self.__impl.frame2d() is not None
else None
)

def save(self, file_path):
"""Save the frame to file. The file type is determined from the file extension.
Expand Down
3 changes: 2 additions & 1 deletion src/ReleasableFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace ZividPython
.def_property_readonly("state", &ReleasableFrame::state)
.def_property_readonly("info", &ReleasableFrame::info)
.def_property_readonly("camera_info", &ReleasableFrame::cameraInfo)
.def("point_cloud", &ReleasableFrame::pointCloud);
.def("point_cloud", &ReleasableFrame::pointCloud)
.def("frame2d", &ReleasableFrame::frame2D);
}
} // namespace ZividPython
10 changes: 10 additions & 0 deletions src/include/ZividPython/ReleasableFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Zivid/Frame.h>
#include <ZividPython/Releasable.h>
#include <ZividPython/ReleasablePointCloud.h>
#include <ZividPython/ReleasableFrame2D.h>
#include <ZividPython/Wrappers.h>

namespace ZividPython
Expand All @@ -15,6 +16,15 @@ namespace ZividPython
ZIVID_PYTHON_FORWARD_1_ARGS(save, const std::string &, fileName)
ZIVID_PYTHON_FORWARD_1_ARGS(load, const std::string &, fileName)
ZIVID_PYTHON_FORWARD_0_ARGS_WRAP_RETURN(ReleasablePointCloud, pointCloud)
std::optional<ReleasableFrame2D> frame2D()
{
auto frame = impl().frame2D();
if(!frame.has_value())
{
return std::nullopt;
}
return ReleasableFrame2D{ std::move(frame.value()) };
}
ZIVID_PYTHON_FORWARD_0_ARGS(settings)
ZIVID_PYTHON_FORWARD_0_ARGS(state)
ZIVID_PYTHON_FORWARD_0_ARGS(info)
Expand Down
9 changes: 9 additions & 0 deletions test/test_camera_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def test_capture2d3d_one_2d_and_one_3d(shared_file_camera):
with shared_file_camera.capture2d3d(settings) as frame:
assert frame
assert isinstance(frame, zivid.frame.Frame)
assert frame.frame2d
assert isinstance(frame.frame2d, zivid.Frame2D)


def test_capture2d3d_two_2d_and_one_3d(shared_file_camera):
Expand All @@ -27,6 +29,8 @@ def test_capture2d3d_two_2d_and_one_3d(shared_file_camera):
with shared_file_camera.capture2d3d(settings) as frame:
assert frame
assert isinstance(frame, zivid.frame.Frame)
assert frame.frame2d
assert isinstance(frame.frame2d, zivid.Frame2D)


def test_capture2d3d_one_2d_and_two_3d(shared_file_camera):
Expand All @@ -41,6 +45,8 @@ def test_capture2d3d_one_2d_and_two_3d(shared_file_camera):
with shared_file_camera.capture2d3d(settings) as frame:
assert frame
assert isinstance(frame, zivid.frame.Frame)
assert frame.frame2d
assert isinstance(frame.frame2d, zivid.Frame2D)


def test_capture2d3d_two_2d_and_two_3d(shared_file_camera):
Expand All @@ -55,6 +61,8 @@ def test_capture2d3d_two_2d_and_two_3d(shared_file_camera):
with shared_file_camera.capture2d3d(settings) as frame:
assert frame
assert isinstance(frame, zivid.frame.Frame)
assert frame.frame2d
assert isinstance(frame.frame2d, zivid.Frame2D)


def test_capture3d_one_acquisition(shared_file_camera):
Expand All @@ -65,6 +73,7 @@ def test_capture3d_one_acquisition(shared_file_camera):
with shared_file_camera.capture3d(settings) as frame:
assert frame
assert isinstance(frame, zivid.frame.Frame)
assert frame.frame2d is None


def test_capture2d_with_settings2d(shared_file_camera):
Expand Down
8 changes: 8 additions & 0 deletions test/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def test_point_cloud(frame):
assert isinstance(point_cloud, zivid.PointCloud)


def test_frame2d(frame):
import zivid

frame2d = frame.frame2d
assert frame2d
assert isinstance(frame2d, zivid.Frame2D)


def test_path_init(application, frame_file):
from pathlib import Path
import zivid
Expand Down

0 comments on commit 2f8225f

Please sign in to comment.