Question
NOTE: It MUST also pass the Assertion at the end. Add to the function `get_images_bb` in the area that says # your code here that
NOTE: It MUST also pass the Assertion at the end.
Add to the function `get_images_bb` in the area that says "# your code here" that applies the "Bounding Box" pre-processing step and takes the following arguments:
* `images`: A numpy array with the shape `(N,height,width)`, where * `N` is the number of samples and could be anything, * `height` is each individual image's height in pixels (i.e., number of rows in each image), * and `width` is each individual image's width in pixels (i.e., number of columns in each image). Do not assume anything about `images`'s dtype or number of samples. * `bb_size`: A scalar with the default value of 20, and represents the desired bounding box size.
and returns the following:
* `images_bb`: A numpy array with the shape `(N,bb_size,bb_size)`, and the same dtype as `images`.
We have provided a template function that uses the previous functions and only requires you to fill in the missing parts. It also handles the input shapes in an agnostic way.
**Important Note**: Make sure that you use the `np.roll` function for this implementation.
def get_images_bb(images, bb_size=20): if len(images.shape)==2: # In case a 2d image was given as input, we'll add a dummy dimension to be consistent images_ = images.reshape(1,*images.shape) else: # Otherwise, we'll just work with what's given images_ = images is_row_inky = get_is_row_inky(images_) is_col_inky = get_is_col_inky(images_)
first_ink_rows = get_first_ink_row_index(is_row_inky) last_ink_rows = get_last_ink_row_index(is_row_inky) first_ink_cols = get_first_ink_col_index(is_col_inky) last_ink_cols = get_last_ink_col_index(is_col_inky)
# your code here N = images_.shape[0] images_bb = np.zeros((N, bb_size, bb_size), dtype=images_.dtype) for i in range(N): img_height = last_ink_rows[i] - first_ink_rows[i] + 1 img_width = last_ink_cols[i] - first_ink_cols[i] + 1 mid_row = first_ink_rows[i] + img_height // 2 mid_col = first_ink_cols[i] + img_width // 2 if img_height < bb_size or img_width < bb_size: continue row_start = mid_row - bb_size // 2 row_end = mid_row + bb_size // 2 + (bb_size % 2 == 0) col_start = mid_col - bb_size // 2 col_end = mid_col + bb_size // 2 + (bb_size % 2 == 0) images_bb[i, :, :] = images_[i, row_start:row_end, col_start:col_end] if len(images.shape)==2: # In case a 2d image was given as input, we'll get rid of the dummy dimension return images_bb[0] else: # Otherwise, we'll just work with what's given return images_bb
Assertion check:
(orig_image, ef_image, test_im, success_bb) = show_test_cases(get_images_bb, task_id='6_V')
assert success_bb
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started