Question

In: Computer Science

How can I convert inRange in cv2 to PIL? For example, I already have mask =...

How can I convert inRange in cv2 to PIL? For example, I already have mask = cv2.inRange(img, array1, array2) but I'm trying to keep everything in PIL. Any ideas would be appreciated!

Solutions

Expert Solution

We generally use inRange() to apply a mask over an image(which is in np parray) and give the lower and upper arrays to the function to get the mask. Inorder to convert it into PIL we have a method called fromarray in PIL.Image. It will convert into PIL_Image. A very important note is that, when we use cv2, the image will be in BGR color convention. So to convert the inRange to PIL, we have to convert the image from BGR to RGB first since PIL follows RGB color convention. I suggest you go through the following code to get a clear understanding.

import cv2
import numpy as np
from PIL import Image   #Import required libraries

img=cv2.imread('/content/test2.png')  #We take a test image
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #Convert the BGR image to RGB color convention
lower = np.array([170,50,50]) #lower array
upper = np.array([180,255,255]) #upper array
mask=cv2.inRange(img,lower,upper) #using inRange to get the mask

PIL_Image=Image.fromarray(mask) #convert the mask to PIL_Image
PIL_Image.save('final.jpg') #Save the PIL_Image

Related Solutions

Can someone please explain how to do the steps for this? I already have part a...
Can someone please explain how to do the steps for this? I already have part a completed but I need help with b-e. Thank you. Problem 1: (a) What is spurious regression? Explain. (b) Assuming , randomly generate 1000 observations of variables X and Y using the following equations: Report the graphs of X and Y. (c) Run the regression: and report the estimated results. (d) What did you expect about the magnitudes and R-square? How are the estimated values...
I have the following decimal value 17.25. How can i convert this to IEEE 754 32...
I have the following decimal value 17.25. How can i convert this to IEEE 754 32 bit single precision floating point and double precision?. I need to use this as a matlab function block in Simulink, therefore i cannot use the conversion of hexadecimal to IEEE 754 32 bit single precision floating point matlab script.
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
how can i convert this to work in visual basic ? what are the formulas for...
how can i convert this to work in visual basic ? what are the formulas for that? You must create a Windows Forms application as the Conversion project including a picture of money on your form.  Create a textbox for the user to entry a dollar amount. Determine the number of quarters, dimes, nickels, and pennies using label controls to display the results.  In your calculation, you should use constant variables. ex) Enter dollar amount for conversion of 4.69 Number...
Here is some additional information on this, I can also provide what I have already, what...
Here is some additional information on this, I can also provide what I have already, what is in bold is what needs to be taken care of. Gentry Inc. is a mid-sized tech firm (200 employees and $300 million in revenue) and has been privately held since the firm’s inception ten years ago. The organization’s board of directors is keen on expanding the operations globally to take advantage of a growing market. Based on reports from the research and development...
(learning about sockets) can I have an example of how to use child threads in a...
(learning about sockets) can I have an example of how to use child threads in a client.cpp to send info to a server.cpp. and how a server.cpp uses child processes to send back info to the client
I need to take the code i already have and change it to have at least...
I need to take the code i already have and change it to have at least one function in it. it has to include one function and one loop. I already have the loop but cant figure out how to add a function. I thought i could create a funciton to call to the totalCost but not sure how to do it. Help please. #include #include //main function int main(void) {    char userName [20];    char yesOrNo [10];   ...
(I already have part A but still still include it anyways so you can use its...
(I already have part A but still still include it anyways so you can use its data to solve for parts B and C. Chemical energy is released or absorbed from reactions in various forms. The most easily measurable form of energy comes in the form of heat, or enthalpy. The enthalpy of a reaction can be calculated from the heats of formation of the substances involved in the reaction: ΔH∘rxn=ΔH∘f(products)−ΔH∘f(reactants) Entropy change, ΔS∘, is a measure of the number...
This is for C++ Assuming the value of a is already defined elsewhere, convert the following...
This is for C++ Assuming the value of a is already defined elsewhere, convert the following code snippet from using a WHILE loop to using a DO-WHILE loop. Please paste in the modified code in the text area below. int k = 1; while( k <= 8 ) { a = a * k; k++; }
I have a programming project due tonight that I have already done once but my professor...
I have a programming project due tonight that I have already done once but my professor sent back because he said I did it wrong. He wants the following using linked lists but i only know how to do it with Arrays. Please try to explain your answer to me and not just give it to me. For this assignment you must write a program that implements a stack of integers. The stack should be implemented using classes and must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT