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.
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 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++; }
Hello! Um, I don't understand the how to use the IP address and the subnet mask...
Hello! Um, I don't understand the how to use the IP address and the subnet mask to find Via/Next hop, Metric, or the interface aspects of the question.
convert differential equation to a matrix differential equation? can put an example and solve it.
convert differential equation to a matrix differential equation? can put an example and solve it.
I have already created the journal entries for the following information but I need the t-accounts,...
I have already created the journal entries for the following information but I need the t-accounts, income statement, statement of retained earning and balance sheet: The following transactions occurred during 2018 (the company uses a perpetual inventory system with FIFO): 1)      Jan 4 Stockholders invested an additional $10,000 cash in the business in exchange for common stock 2)      Jan 4 Purchased 20 rabbits at $50 each on account from Jelly Bean Farms. 3)      Jan 4 Established a $200 petty change...
Hi! How I can convert from xml file to xlsx(excel ) sheet using Aspose.Cells, in C#...
Hi! How I can convert from xml file to xlsx(excel ) sheet using Aspose.Cells, in C# I Try with this code but I need loop , so  every element is one row with elements id (name )in column 1, and texten in column 2. Workbook wb = new workbook(); worksheet ws = wb.worksheets[0]; ws.Cells[0,0].putValue("name"); wb.Save(path, "xml_to_exce.xlsxl"); Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT