Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Download the Tomatoes _ Ripe.jpg file from the assignment page. Run the code below a few times and play with some of the variable values

Download the Tomatoes_Ripe.jpg file from the assignment page.
Run the code below a few times and play with some of the variable values and see what happens to the output! When you have a good idea of how the program works, complete the final part of this project.
import cv2
import numpy as np
from matplotlib import pyplot as plt
from google.colab.patches import cv2_imshow
import os
# Use some exception handling to observe best practices
try:
# Open and resize image to height =1000 width =1000
width_height =(1000,1000)
img = cv2.imread('/content/drive/MyDrive/Tomatoes_Ripe.jpg')
img = cv2.resize(img, width_height, interpolation=cv2.INTER_AREA)
# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
original_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Define HSV ranges for 'red'
# Adjust for desired red hue/saturation
# ([ H S V ])
lower_red = np.array([0,100,100])
upper_red = np.array([10,255,255])
# ([ H S V ])
# Use min/max of Hue in the HSV image to get the 'red mask'
red_mask = cv2.inRange(hsv, lower_red, upper_red)
# Use mask to draw contours
contours, _= cv2.findContours(red_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Overlay contours on a copy of the original image
blue_outline_color =(255,0,0)
# Copy the image so we can display an original copy of it at the end
blue_outline = img.copy()
# This takes the image copy and draws the contours with a line weight of 2.
# Change the last number and the outline color variable to see how it changes the output
# The -1 indicates that all of the contours will be drawn, other funtions of this argument
# can be used to draw contours at specific locations on the image
cv2.drawContours(blue_outline, contours, -1, blue_outline_color, 2)
outline_reds = cv2.cvtColor(blue_outline, cv2.COLOR_BGR2RGB)
# Show original image, mask, and outline of red images 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(red_mask, cmap='gray'), ax2.set_title('HSV Mask'), ax2.axis('off')
ax3.imshow(outline_reds), ax3.set_title('Outline of Detected Red\'s'), ax3.axis('off')
plt.tight_layout()
plt.show()
# Just in case image isnt processed correctly tell us why
except Exception as e:
print(f'Error processing {img}')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Strategic Database Technology Management For The Year 2000

Authors: Alan Simon

1st Edition

155860264X, 978-1558602649

More Books

Students also viewed these Databases questions

Question

What is the relationship between humans?

Answered: 1 week ago

Question

What is the orientation toward time?

Answered: 1 week ago