Question: The previous code showed a method for identifying red pixels in an image. For the final section of the project, modify the code below to:

The previous code showed a method for identifying red pixels in an image.

For the final section of the project, modify the code below to:

1. Read all of the images in the folder

2. Outline all BLUE objects in the image

3. Saves all analyzed images in the working directory folder

Common HSV Color Map:

Hue (H): 0-179 (Represents color position on the color wheel)

  • Red: 0-10
  • Yellow: 20-30
  • Green: 60-70
  • Cyan: 90-100
  • Blue: 120-130
  • Magenta: 150-160

Saturation (S): 0-255 (Represents color intensity/purity)

  • 0: Grayscale
  • 255: Full color intensity

Value (V): 0-255 (Represents brightness)

  • 0: Black
  • 255: Maximum brightness

import cv2

import numpy as np

from matplotlib import pyplot as plt

from google.colab.patches import cv2_imshow

import os

image_folder = '/content/drive/MyDrive/OOO_BLUUUE'

# Resize image to height = 1000 width = 1000

width_height = (1000, 1000)

# Look through folder and analyze each image for blue colors

for image_name in os.listdir(image_folder):

# Make image path

image_path = os.path.join(image_folder, image_name)

# Use some exception handling to observe best practices

try:

# Open image

img = '''FIX ME!!!'''

img = cv2.resize(img, width_height, interpolation=cv2.INTER_AREA)

original_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Convert BGR to HSV

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Define HSV ranges for 'blue (adjusted to catch 'blue-ish' colors as well here)'

# Adjust for desired blue hue/saturation

# ([ H S V ])

lower_blue = np.array([ '''FIX ME!!!''' ])

upper_blue = np.array([ '''FIX ME!!!''' ])

# ([ H S V ])

# Threshold the HSV image to get the 'blue' mask

blue_mask = '''FIX ME!!!'''

# Draw contours on a copy of the original image

contours, _ = cv2.findContours(blue_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw contours on a copy of the original image

green_outline_color = (0, 255, 0)

green_outline = ('''FIX ME!!!''')

cv2.drawContours('''FIX ME!!!''')

outline_blues = cv2.cvtColor(green_outline, cv2.COLOR_BGR2RGB)

# Show original image, mask, and result in 3 x 3 subplot

fig = plt.figure(figsize=(14, 8))

ax1 = fig.add_subplot(1, 3, 1)

ax2 = fig.add_subplot(1, 3, 2)

ax3 = fig.add_subplot(1, 3, 3)

ax1.imshow(original_img), ax1.set_title('Original Image'), ax1.axis('off')

ax2.imshow(blue_mask, cmap='gray'), ax2.set_title('HSV Mask'), ax2.axis('off')

ax3.imshow(outline_blues), ax3.set_title('Outline of Detected Blue\'s'), ax3.axis('off')

# Create filename for analyzed image by extracting filename without extension

analyzed_image = image_name.split(".")

analyzed_image = f'{analyzed_image[0]}_Analysis.jpg'

# Save the plot to the same folder as the image

plt.savefig(os.path.join('''FIX ME!!!''', '''FIX ME!!!'''))

plt.close()

# Just in case image isnt processed correctly tell us why

except Exception as e:

print(f'Error processing {'''FIX ME!!!'''}')

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!