Answered step by step
Verified Expert Solution
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 TomatoesRipe.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 cv
import numpy as np
from matplotlib import pyplot as plt
from google.colab.patches import cvimshow
import os
# Use some exception handling to observe best practices
try:
# Open and resize image to height width
widthheight
img cvimreadcontentdriveMyDriveTomatoesRipe.jpg
img cvresizeimg widthheight, interpolationcvINTERAREA
# Convert BGR to HSV
hsv cvcvtColorimg cvCOLORBGRHSV
originalimg cvcvtColorimg cvCOLORBGRRGB
# Define HSV ranges for 'red'
# Adjust for desired red huesaturation
# H S V
lowerred nparray
upperred nparray
# H S V
# Use minmax of Hue in the HSV image to get the 'red mask'
redmask cvinRangehsv lowerred, upperred
# Use mask to draw contours
contours, cvfindContoursredmask, cvRETREXTERNAL, cvCHAINAPPROXSIMPLE
# Overlay contours on a copy of the original image
blueoutlinecolor
# Copy the image so we can display an original copy of it at the end
blueoutline img.copy
# This takes the image copy and draws the contours with a line weight of
# Change the last number and the outline color variable to see how it changes the output
# The 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
cvdrawContoursblueoutline, contours, blueoutlinecolor,
outlinereds cvcvtColorblueoutline, cvCOLORBGRRGB
# Show original image, mask, and outline of red images in x subplot
fig pltfigurefigsize
ax fig.addsubplot
ax fig.addsubplot
ax fig.addsubplot
aximshoworiginalimg axsettitleOriginal Image' axaxisoff
aximshowredmask, cmap'gray' axsettitleHSV Mask' axaxisoff
aximshowoutlinereds axsettitleOutline of Detected Reds axaxisoff
plttightlayout
pltshow
# Just in case image isnt processed correctly tell us why
except Exception as e:
printfError processing img
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