Question

Write a function “removeRedEye” that reads the image “lab12_image.jpg” and removes the red eye. Y...

Write a function “removeRedEye” that reads the image “lab12_image.jpg” and removes the red eye. You can replace the red color with the black color.

You have to replace it using the imports numpy and cv2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code without comments:

***refer to the attached screenshot for indentation***

========================================

import numpy as np
import cv2

img = cv2.imread('image.jp
g')
shape=img.shape
for i in range(shape[0]):
for j in range(shape[1]):
if(img[i][j][0]>=0 and img[i][j][1]>=0 and img[i][j][2]>=125
and img[i][j][0]<=220 and img[i][j][1]<=100 and img[i][j][2]<=255):
img[i][j]=[0,0,0]
cv2.imwrite("image2.jpg", img)

==================================================

Snapshot

import numpy as np import Cv2 img cv2.imread (image.jp shape-img.shape for i in range (shape[0]) for j in range (shape[1]) i

Code with comments:

========================================================

import numpy as np
import cv2

#read the image as numpy array
#it will be a 3D array
img = cv2.imread('image.jpg')

#image.shape returns an array having width as the first
#element, height as 2nd as number of color channels as 3rd
shape=img.shape

#loop over every pixel of the image and check if it falls
#under the category of red

#first loop for looping over every pixel row in the image
for i in range(shape[0]):
#inner loop for looping over every pixel column
for j in range(shape[1]):
#now we will decide the criteria for a pixel to be red
#in the numpy array of the image the pixel color values
#are in the order b,g,r(blue,green,red)

#below condition implies that if the blue content in the
#pixel is in the range(0,220) and green in range(0,100) and
#red in range(125,255) then we will change the color of that
#pixel to red

#I decided these ranges by hit and try for best results

#img[i][j] will have an array with 3 values [b,g,r]
#we have to alter these values for changing the color
if(img[i][j][0]>=0 and img[i][j][1]>=0 and img[i][j][2]>=125
and img[i][j][0]<=220 and img[i][j][1]<=100 and img[i][j][2]<=255):
#change color to black
img[i][j]=[0,0,0]
#save the edited image as new image
cv2.imwrite("image2.jpg", img)

=======================================================

Sample OUTPUT

image.jpg

image2.jpg(updated image)

import numpy as np import Cv2 img cv2.imread ('image.jp shape-img.shape for i in range (shape[0]) for j in range (shape[1]) if (img[i] [j1 01>-0 and img[i] [ 10 and img [i] [[21> 125 and img[i] [j]0]<-220 and img[i] [j1 [1<-100 and img [il j] [21<-255) img [i] [51 [0,0,0] cv2.imwrite("image2.jpg", img)

We were unable to transcribe this image

We were unable to transcribe this image

Add a comment
Know the answer?
Add Answer to:
Write a function “removeRedEye” that reads the image “lab12_image.jpg” and removes the red eye. Y...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Resampling: Do not use any in-built functions from opencv and numpy. In general, you can use...

    Resampling: Do not use any in-built functions from opencv and numpy. In general, you can use function from math library. Functions allowed for this part are: np.array(), np.matrix(), np.zeros(), np.ones(), cv2.imread(), cv2.namedWindow(), cv2.waitKey(). Write code for zooming and shrinking an image using the nearest neighbor and bilinear interpolation. The input to your program is: (i) image, (ii) transformation parameters, and (iii) interpolation method. (Can I get more of an explanation and puesdocode of how you use nearest neighbor and bilinear...

  • In c++ The iron-puzzle.ppm image is a puzzle; it contains an image of something famous, howeve...

    In c++ The iron-puzzle.ppm image is a puzzle; it contains an image of something famous, however the image has been distorted. The famous object is in the red values, however the red values have all been divided by 10, so they are too small by a factor of 10. The blue and green values are all just meaningless random values ("noise") added to obscure the real image. If you were to create a grayscale image out of just the red...

  • USE MATLAB TO SOLVE 1. Snap a close-up color picture of a person with your phone...

    USE MATLAB TO SOLVE 1. Snap a close-up color picture of a person with your phone or download a picture of your preference in JPG format, transfer/save it to a preferred location on your computer and import the picture into MATLAB. 2. Display the Red, Green and Blue components of the imported pictures in three subplots arranged horizontally; 3. Calculate the minimum and maximum values of the R, G, B components and display them in the titles of each subplots...

  • Python: Write a function "contrast_adjust", which takes a 2D NumPy array A and an integer c,...

    Python: Write a function "contrast_adjust", which takes a 2D NumPy array A and an integer c, and returns another NumPy array of the same size, representing the contrast-adjusted image. You can assume that −127 ≤ c ≤ 127.

  • wmlwm White eye Miniature wing w m/YO Red eye Full wing W W m wmlwm Red...

    wmlwm White eye Miniature wing w m/YO Red eye Full wing W W m wmlwm Red eye Full wing wm/Yd White eye Miniature wing Phenotypes and genotypes of the F Females Males Number observed w m w*m/wm Red eye Full wing w m /Y Red eye Full wing W m wm GP 750 W m wmlwm White eye Miniature wing wm/Y White eye Miniature wing W m w mlwm Red eye Miniature wing w m/Y Red eye Miniature wing W...

  • Write a function in Python that solves the linear system ??=? using Gaussian Elimination, taking ?,?...

    Write a function in Python that solves the linear system ??=? using Gaussian Elimination, taking ?,? as input. The function should have two phases: the elimination phase, and the back substitution phase. You can use numpy library.

  • Activity 2. Suppose you have a random sequence of black, red and white marbles and want...

    Activity 2. Suppose you have a random sequence of black, red and white marbles and want to rearrange it such that the marbles in each color are grouped together, with the order black, white and then red. Write an algorithm using pseudocode to solve this problem. Do your best to optimize your initial solution.

  • #PYTHON# In this exercise you will write code which loads a collection of images (which are...

    #PYTHON# In this exercise you will write code which loads a collection of images (which are all the same size), computes a pixelwise average of the images, and displays the resulting average. The images below give some examples that were generated by averaging "100 unique commemorative photographs culled from the internet" by Jason Salavon. Your program will do something similar. Write a function in the cell below that loads in one of the sets of images and computes their average....

  • - Use the COUNTIF function in cell I10 to determine the number of students with black...

    - Use the COUNTIF function in cell I10 to determine the number of students with black hair. Be sure to build a formula can be reused by copying down. - Copy your function in cell I10 and paste it down to complete the "Count" column of the "Hair Color Summary" table. - Use the COUNTIF function in cell I17 to determine the number of students with brown eyes.  Be sure to build a formula can be reused by copying down. -...

  • Write a function to "smooth" a black-and-white image by replacing each pixel by the average of...

    Write a function to "smooth" a black-and-white image by replacing each pixel by the average of itself and its neighbors. In MATLAB a black-and-white image is just a matrix of 1s and 0s - 1 represents white, and 0 represents black. To keep the algorithm simple, ignore the edges of the image - that is, do not change the first and last rows and columns. function name = smooth_image() input argument = input matrix output argument = output matrix The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT