Struggling with subplot errors and formatting in scikit-image thresholding code? Learn the step-by-step fix to resolve loop issues and correct string/colormap handling for Computer Vision certification exams.
Table of Contents
Question
You are working on the following code snippet that performs a thresholding operation on a greyscale image:
from skimage import data
from skimage import filters
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
welcome = data.welcome()
g_welcome = rgb2gray(welcome)
plt.figure(figsize=(5, 5))
for i in range(15):
b_gray = (g_welcome > i*0.1)*1
plt.subplot(5,2,i+1)
plt.title(“Threshold: >”+int(round(i*0.1,1)))
plt.imshow(b_gray)
plt.tight_layout()
When you run the code, it displays errors such as looping beyond the slots and incorrect formatting. How would you fix this?
A.
welcome = data.welcome()
g_welcome = rgb2gray(welcome)
plt.figure(figsize=(15, 5))
for i in range(15):
b_gray = (g_welcome > i*0.1)*0.1
plt.subplot(5,2,i+1)
plt.intitle(“Threshold: >”+float(round(i*0.1,1)))
plt.imshow(b_gray)
B.
welcome = data.welcome()
g_welcome = rgb2hsv(welcome)
plt.infigure(figsize=(5, 5))
for i in range(15):
b_gray = (g_welcome > i*0.1)*1
plt.subplot(5,2,i+1)
plt.puttitle(“Threshold: >”+float(round(i*0.1,1)))
plt.imshow(b_gray)
C.
welcome = data.welcome()
g_welcome = rgb2gray(welcome)
plt.figure(figsize=(5, 5))
for i in range(15):
b_gray = (g_welcome > i*0.1)/1
plt.subplot(5,2,i-1)
plt.title(“Threshold: >”+int(round(i*5,1)))
plt.imshow(b_gray, cmap=’grey’)
D.
welcome = data.welcome()
g_welcome = rgb2gray(welcome)
plt.figure(figsize=(10, 8))
for i in range(10):
b_gray = (g_welcome > i*0.1)*1
plt.subplot(5,2,i+1)
plt.title(“Threshold: >”+str(round(i*0.1,1)))
plt.imshow(b_gray, cmap=’gray’)
Answer
D.
welcome = data.welcome()
g_welcome = rgb2gray(welcome)
plt.figure(figsize=(10, 8))
for i in range(10):
b_gray = (g_welcome > i*0.1)*1
plt.subplot(5,2,i+1)
plt.title(“Threshold: >”+str(round(i*0.1,1)))
plt.imshow(b_gray, cmap=’gray’)
Explanation
The code errors stem from subplot indexing mismatches, incorrect string formatting, and colormap configuration. Here’s the breakdown:
Subplot Grid Capacity Error
Original code uses range(15) with plt.subplot(5,2,i+1), creating 15 subplots in a 5×2 grid (10 slots max).
Fix: Reduce iterations to range(10) (Option D) to match the 5×2 grid capacity.
Title String Conversion
int(round(i*0.1,1)) truncates decimal values (e.g., 0.1 → 0).
Fix: Use str(round(i*0.1,1)) (Option D) to preserve threshold values like “0.1” instead of “0”.
Figure Size Adjustment
Original figsize=(5,5) causes overlapping.
Fix: Expand to figsize=(10,8) (Option D) for clearer subplot spacing.
Colormap Specification
Missing cmap=’gray’ in plt.imshow() displays incorrect pseudo-coloring.
Fix: Add cmap=’gray’ (Option D) for proper greyscale rendering.
# Corrected Code (Option D) welcome = data.welcome() g_welcome = rgb2gray(welcome) plt.figure(figsize=(10, 8)) for i in range(10): b_gray = (g_welcome > i*0.1)*1 # Binary thresholding plt.subplot(5,2,i+1) plt.title("Threshold: >" + str(round(i*0.1,1))) # String conversion plt.imshow(b_gray, cmap='gray') # Correct colormap
Why Other Options Fail
A: Uses *0.1 instead of *1, scaling pixel values incorrectly.
B: Uses rgb2hsv, deviating from greyscale processing.
C: Invalid subplot index (i-1) and wrong threshold calculation (i*5).
This fix ensures proper visualization of 10 thresholded images with accurate labels and layout.
Computer Vision for Developers skill assessment practice question and answer (Q&A) dump including multiple choice questions (MCQ) and objective type questions, with detail explanation and reference available free, helpful to pass the Computer Vision for Developers exam and earn Computer Vision for Developers certification.