Complete the code to import the library used for image segmentation.
import [1] as cv
We use cv2 (OpenCV) for image processing tasks including segmentation.
Complete the code to read a medical image from a file.
image = cv.[1]('scan.png', cv.IMREAD_GRAYSCALE)
imshow which displays images but does not read files.imwrite which saves images instead of reading.imread reads an image file into memory. We use grayscale mode for medical scans.
Fix the error in the thresholding step to segment the image.
_, segmented = cv.threshold(image, [1], 255, cv.THRESH_BINARY)
The threshold value must be between 0 and 255 for 8-bit images. 128 is a common middle value.
Fill both blanks to create a mask and apply it to the image.
mask = segmented [1] 255 result = cv.bitwise_and(image, image, mask=[2])
The mask is created by checking where segmented equals 255. Then we apply this mask to the original image.
Fill all three blanks to calculate Dice coefficient for segmentation evaluation.
intersection = (predicted_mask [1] true_mask).sum() dice = (2 * intersection) / (predicted_mask.sum() [2] true_mask.sum()) print('Dice coefficient:', [3])
Dice coefficient uses intersection with bitwise AND (&), sums added (+), and prints the calculated dice value.