Skip to content

Commit fcbfa34

Browse files
committed
Adding Python script to generate SystemC TB data
- Adding installation script of Python OpenCV in Linux. - Adding script to read an image, convert it to gray scale, apply sobel in x and y, and then combine them in a single one. Script also generates output images for each of the stage. - Adding a JupyterNotebook version of the script that can be run also in Collab tool.
1 parent 66f78e0 commit fcbfa34

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

tools/datagen/datagen.ipynb

Lines changed: 149 additions & 0 deletions
Large diffs are not rendered by default.

tools/datagen/datagen.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/python3
2+
"""
3+
Script to generate the data required for SystemC testbench.
4+
"""
5+
from typing import Tuple
6+
7+
import cv2
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
11+
12+
def apply_sobel_filter(image_path: str
13+
)-> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
14+
"""Applies the Sobel filter in X and Y to a input image and returns each
15+
step.
16+
17+
Args:
18+
image_path (str): Path to the input image
19+
20+
Returns:
21+
Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: A tuple
22+
containing the gray image, the sobel in x, the sobel in y, and the
23+
sobel combining x and y.
24+
"""
25+
# Read the image
26+
image: np.ndarray = cv2.imread(image_path)
27+
28+
# BGR to grayscale
29+
gray_img: np.ndarray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
30+
31+
# Apply Sobel in x direction
32+
sobel_x_img: np.ndarray = cv2.Sobel(gray_img, cv2.CV_64F, 1, 0, ksize=3)
33+
sobel_x_uint8: np.ndarray = np.uint8(np.absolute(sobel_x_img))
34+
35+
# Apply Sobel in y direction
36+
sobel_y_img: np.ndarray = cv2.Sobel(gray_img, cv2.CV_64F, 0, 1, ksize=3)
37+
sobel_y_uint8: np.ndarray = np.uint8(np.absolute(sobel_y_img))
38+
39+
# Combine sobel
40+
sobel_combined_img: np.ndarray = cv2.magnitude(sobel_x_img, sobel_y_img)
41+
42+
# Convert to 8-bit image
43+
sobel_combined_uint8: np.ndarray = np.uint8(np.absolute(sobel_combined_img))
44+
45+
# Display the results
46+
plt.figure(figsize=(10, 8))
47+
48+
plt.subplot(2, 2, 1)
49+
plt.title('Original Image')
50+
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
51+
plt.axis('off')
52+
53+
plt.subplot(2, 2, 2)
54+
plt.title('Grayscale Image')
55+
plt.imshow(gray_img, cmap='gray')
56+
plt.axis('off')
57+
58+
plt.subplot(2, 2, 3)
59+
plt.title('Sobel X')
60+
plt.imshow(np.uint8(np.absolute(sobel_x_img)), cmap='gray')
61+
plt.axis('off')
62+
63+
plt.subplot(2, 2, 4)
64+
plt.title('Sobel Y')
65+
plt.imshow(np.uint8(np.absolute(sobel_y_img)), cmap='gray')
66+
plt.axis('off')
67+
68+
plt.figure(figsize=(5, 5))
69+
plt.title('Sobel Combined')
70+
plt.imshow(sobel_combined_uint8, cmap='gray')
71+
plt.axis('off')
72+
73+
plt.show()
74+
75+
return (gray_img, sobel_x_uint8, sobel_y_uint8, sobel_combined_uint8)
76+
77+
78+
if __name__ == '__main__':
79+
img_name: str = 'car'
80+
img_ext: str = 'jpg'
81+
img_path: str = f'src/imgs/{img_name}.{img_ext}'
82+
gray, sobel_x, sobel_y, sobel_combined = apply_sobel_filter(img_path)
83+
84+
cv2.imwrite(f'src/imgs/{img_name}_grayscale_image.{img_ext}', gray)
85+
cv2.imwrite(f'src/imgs/{img_name}_sobel_x_result.{img_ext}', np.uint8(np.absolute(sobel_x)))
86+
cv2.imwrite(f'src/imgs/{img_name}_sobel_y_result.{img_ext}', np.uint8(np.absolute(sobel_y)))
87+
cv2.imwrite(f'src/imgs/{img_name}_sobel_combined_result.{img_ext}', sobel_combined)
88+

tools/datagen/install_pyopencv.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
sudo pip3 install opencv-python
4+
5+
python3 -c "import cv2; print(f'OpenCV: {cv2.__version__}')"

0 commit comments

Comments
 (0)