Skip to content

Commit 48c5d3a

Browse files
csu-bot-zividchrisasc
authored andcommitted
Samples: Update bgra() method
This commit updates all relevant samples to use hte Zivid API for extracting `bgr` color information, instead of using third-part library.
1 parent 841004e commit 48c5d3a

File tree

7 files changed

+53
-50
lines changed

7 files changed

+53
-50
lines changed

README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,30 +134,34 @@ from the camera can be used.
134134

135135
## Installation
136136

137+
-----
138+
139+
Note:
140+
141+
The recommended Python version for these samples is 3.6 - 3.9.
142+
143+
-----
144+
137145
1. [Install Zivid
138146
Software](https://support.zivid.com/latest//getting-started/software-installation.html).
139147

140-
2. [Install Zivid
141-
Python](https://github.com/zivid/zivid-python#installation). Note:
142-
The recommended Python version for these samples is 3.6 - 3.9.
143-
144-
3. [Download Zivid Sample
148+
2. [Download Zivid Sample
145149
Data](https://support.zivid.com/latest//api-reference/samples/sample-data.html).
146150

147-
4. Install the runtime requirements using IDE or command line:
151+
3. Install the runtime requirements using IDE or command line:
148152

149-
``` sourceCode
153+
``` sourceCode bash
150154
pip install -r requirements.txt
151155
```
152156
153-
5. Add the directory source to PYTHONPATH. Navigate to the root of the
157+
4. Add the directory source to PYTHONPATH. Navigate to the root of the
154158
repository and run:
159+
160+
- PowerShell: `$env:PYTHONPATH=$env:PYTHONPATH + ";$PWD\source"`
161+
- cmd: `set PYTHONPATH="$PYTHONPATH;$PWD\source"`
162+
- bash: `export PYTHONPATH="$PYTHONPATH:$PWD/source"`
155163
156-
> - PowerShell: `$env:PYTHONPATH=$env:PYTHONPATH + ";$PWD\source"`
157-
> - cmd: `set PYTHONPATH="$PYTHONPATH;$PWD\source"`
158-
> - bash: `export PYTHONPATH="$PYTHONPATH:$PWD/source"`
159-
160-
6. Open and run one of the samples.
164+
5. Open and run one of the samples.
161165
162166
## Support
163167

continuous-integration/setup.sh

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,37 @@
33
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
44
ROOT_DIR=$(realpath "$SCRIPT_DIR/..")
55

6+
ZIVID_SDK_EXACT_VERSION=2.9.0+4dbba385-1
7+
ZIVID_TELICAM_EXACT_VERSION=3.0.1.1-3
8+
69
export DEBIAN_FRONTEND=noninteractive
10+
source /etc/os-release || exit $?
711

812
function apt-yes {
913
apt-get --assume-yes "$@"
1014
}
1115

16+
function install_www_deb {
17+
TMP_DIR=$(mktemp --tmpdir --directory zivid-python-install-www-deb-XXXX) || exit $?
18+
pushd $TMP_DIR || exit $?
19+
wget -nv "$@" || exit $?
20+
apt-yes install --fix-broken ./*deb || exit $?
21+
popd || exit $?
22+
rm -r $TMP_DIR || exit $?
23+
}
24+
1225
apt-yes update || exit
1326
apt-yes dist-upgrade || exit
1427

1528
apt-yes install \
16-
python3-pip ||
29+
python3-pip \
30+
wget ||
1731
exit $?
1832

19-
python3 -m pip install --upgrade pip || exit
33+
install_www_deb "https://downloads.zivid.com/sdk/releases/${ZIVID_SDK_EXACT_VERSION}/u${VERSION_ID:0:2}/zivid-telicam-driver_${ZIVID_TELICAM_EXACT_VERSION}_amd64.deb" || exit $?
34+
install_www_deb "https://downloads.zivid.com/sdk/releases/${ZIVID_SDK_EXACT_VERSION}/u${VERSION_ID:0:2}/zivid_${ZIVID_SDK_EXACT_VERSION}_amd64.deb" || exit $?
2035

36+
python3 -m pip install --upgrade pip || exit
2137
python3 -m pip install --requirement "$ROOT_DIR/requirements.txt" || exit
2238

2339
echo Success! ["$(basename $0)"]

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ open3d
55
pyyaml
66
scipy
77
robodk
8+
zivid

source/applications/advanced/create_depth_map.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import cv2
99
import numpy as np
1010
import zivid
11+
from sample_utils.display import display_bgr
1112
from sample_utils.paths import get_sample_data_path
1213

1314

@@ -44,27 +45,21 @@ def _point_cloud_to_cv_bgr(point_cloud: zivid.PointCloud) -> np.ndarray:
4445
bgr: BGR image (HxWx3 ndarray)
4546
4647
"""
47-
rgba = point_cloud.copy_data("rgba")
48-
# Applying color map
49-
bgr = cv2.cvtColor(rgba, cv2.COLOR_RGBA2BGR)
48+
bgra = point_cloud.copy_data("bgra")
5049

51-
return bgr
50+
return bgra[:, :, :3]
5251

5352

54-
def _visualize_and_save_image(image: np.ndarray, image_file: str, window_name: str) -> None:
53+
def _visualize_and_save_image(image: np.ndarray, image_file: str, title: str) -> None:
5554
"""Visualize and save image to file.
5655
5756
Args:
5857
image: BGR image (HxWx3 ndarray)
5958
image_file: File name
60-
window_name: OpenCV Window name
59+
title: OpenCV Window name
6160
6261
"""
63-
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
64-
cv2.imshow(window_name, image)
65-
print("Press any key to continue")
66-
cv2.waitKey(0)
67-
cv2.destroyWindow(window_name)
62+
display_bgr(image, title)
6863
cv2.imwrite(image_file, image)
6964

7065

source/applications/advanced/gamma_correction.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import cv2
99
import numpy as np
1010
import zivid
11+
from sample_utils.display import display_bgr
1112

1213

1314
def _options() -> argparse.Namespace:
@@ -46,10 +47,9 @@ def _capture_bgr_image(camera: zivid.Camera, gamma: float) -> np.ndarray:
4647

4748
print("Capturing 2D frame")
4849
with camera.capture(settings_2d) as frame_2d:
49-
image = frame_2d.image_rgba()
50-
rgba = image.copy_data()
51-
bgr = cv2.cvtColor(rgba, cv2.COLOR_RGBA2BGR)
52-
return bgr
50+
image = frame_2d.image_bgra()
51+
bgra = image.copy_data()
52+
return bgra[:, :, :3]
5353

5454

5555
def _combine_images(image_one: np.ndarray, image_two: np.ndarray) -> np.ndarray:
@@ -69,19 +69,6 @@ def _combine_images(image_one: np.ndarray, image_two: np.ndarray) -> np.ndarray:
6969
return combined_image
7070

7171

72-
def _display_bgr(image: np.ndarray, bgr_name: str) -> None:
73-
"""Display BGR image using OpenCV.
74-
75-
Args:
76-
image: BGR image to be displayed
77-
bgr_name: Name of the OpenCV window
78-
79-
"""
80-
cv2.imshow(bgr_name, image)
81-
print("Press any key to continue")
82-
cv2.waitKey(0)
83-
84-
8572
def _main() -> None:
8673
app = zivid.Application()
8774

@@ -100,7 +87,8 @@ def _main() -> None:
10087

10188
print(f"Displaying color image before and after gamma correction: {user_options.gamma}")
10289
combined_image = _combine_images(bgr_original, bgr_adjusted)
103-
_display_bgr(combined_image, "Original on left, adjusted on right")
90+
cv2.imwrite("combined_image.jpg", combined_image)
91+
display_bgr(combined_image[:, :, 0:3], title="Original on left, adjusted on right")
10492

10593

10694
if __name__ == "__main__":

source/applications/basic/file_formats/convert_zdf.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,8 @@ def _convert_2_2d(point_cloud: zivid.PointCloud, file_name: str) -> None:
100100
101101
"""
102102
print(f"Saving the frame to {file_name}")
103-
rgba = point_cloud.copy_data("rgba")
104-
bgr = cv2.cvtColor(rgba, cv2.COLOR_RGBA2BGR)
105-
cv2.imwrite(file_name, bgr)
103+
bgra = point_cloud.copy_data("bgra")
104+
cv2.imwrite(file_name, bgra[:, :, :3])
106105

107106

108107
def _main() -> None:

source/sample_utils/display.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ def display_rgb(rgb: np.ndarray, title: str = "RGB image", block: bool = True) -
2727
plt.show(block=block)
2828

2929

30-
def display_bgr(bgr: np.ndarray, bgr_name: str) -> None:
30+
def display_bgr(bgr: np.ndarray, title: str = "RGB image") -> None:
3131
"""Display BGR image using OpenCV.
3232
3333
Args:
3434
bgr: BGR image (HxWx3 ndarray)
35-
bgr_name: Name of the OpenCV window
35+
title: Name of the OpenCV window
3636
3737
"""
38-
cv2.imshow(bgr_name, bgr)
38+
cv2.imshow(title, bgr)
3939
print("Press any key to continue")
4040
cv2.waitKey(0)
4141

0 commit comments

Comments
 (0)