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...
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++; }
How can I convert the following subroutine into MIPS assembly: search(Array, start, end, searchValue) { if...
How can I convert the following subroutine into MIPS assembly: search(Array, start, end, searchValue) { if (start > end) return false mid = start + (end - start)/2; if (array[mid] == searchValue) return true; if (array[mid] > searchValue) return search(array, start, mid-1, searchValue); return search(array, mid+1, end, searchValue); }
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT