the following python script I want to convert using opencv python, how do I make it converted?
script: scipy.misc.imread(path, mode='RGB').astype(np.float)
I want to convert it using cv2 and what would be alternative for astype(np.float) with this?
import cv2
import scipy.misc
img = scipy.misc.imread(path, mode='RGB').astype(np.float)
You may use cv2.imread, convert color format from BGR to RGB, and convert to float:
path = 'chelsea.png'
ref_img = scipy.misc.imread(path, mode='RGB').astype(np.float)
img = cv2.imread(path) # Read input imgae
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert color format form BGR to RGB (OpenCV default is BGR).
img = img.astype(np.float) # Convert ot float
print(np.all(img == ref_img))
This should do the trick:
img = cv2.imread(path, -1).astype(np.float)
Related
I tried extract numbers from original image https://imgur.com/a/adMaKGy , but with no luck.
Output from pytesseract is: "[a ]:[4] G2):Go] [7 ):Ce J"
Thank you for advice,
My code:
import pytesseract
import cv2
pytesseract.pytesseract.tesseract_cmd = 'folder /tesseract.exe'
img = cv2.imread("folder /test_image.png")
text = pytesseract.image_to_string(img)
print(text)
The README says that OpenCV images are in BGR format and pytesseract assumes RGB format, so you need to convert it
import cv2
img_cv = cv2.imread(r'/<path_to_image>/digits.png')
# By default OpenCV stores images in BGR format and since pytesseract assumes RGB format,
# we need to convert from BGR to RGB format/mode:
img_rgb = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
print(pytesseract.image_to_string(img_rgb))
# OR
img_rgb = Image.frombytes('RGB', img_cv.shape[:2], img_cv, 'raw', 'BGR', 0, 0)
print(pytesseract.image_to_string(img_rgb))
Hi I'm trying to convert a series of images into an array and then convert the RGB to gray scale.
In my work folder I have x number of frames.png, I need to read all this frames in an array and then convert each frame (RGB) to Gray scale.
For one frame my code is:
import numpy as np
import cv2 as cv
from PIL import Image
# Read image
Image = cv.imread('frame0.png')
# RGB to Gray Scale
GS = cv.cvtColor(Image, cv.COLOR_BGR2GRAY)
th, Gray = cv.threshold(GS, 128, 192, cv.THRESH_OTSU)
Any idea?
You can use os to read files from a folder. With the "endswith" function, you can extract file formats and pull them all.
Here is a working code
import numpy as np
import cv2 as cv
import os
for file in os.listdir("/mydir"): # images folder path
if file.endswith((".png",".jpg")): # you can add formats
img = cv.imread(file)
GS = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
th, Gray = cv.threshold(GS, 128, 192, cv.THRESH_OTSU)
cv.imwrite("converted-"+file,Gray)
I am using OpenCV - 3.4.9.31
I was trying to run a very basic code to read an image, but to my surprise, the output was in RGB colorspace instead of BGR.
import cv2
folder = 'C:/Users/xxx/PycharmProjects/Images/'
picture = 'lena.png'
filename = folder + picture
Img = cv2.imread(filename)
cv2.imshow("Image", Img)
cv2.waitKey(0)
Why do I see RGB image instead of BGR ?
If you want to convert it to BGR, you can do this :
imgBGR = cv2.cvtColor(Img, cv2.COLOR_RGB2BGR)
I am doing some preprocessing things on pretrained data in OpenVino model.
It says it only uses the BGR format image.
Here ,
How do i check in python whether my image is in BGR format or RBG format?
my loaded image code is as
import cv2
import numpy as np
from PIL import Image
image = cv2.imread('29101878_988024658021087_5045014614769664000_o.jpg')
print(image.shape)
Gives output of
shape (973,772,3)
How do i check image is RBG or BGR format?
If it is in RBG format How do i convert it to BGR and viceversa?
When you use opencv (imread, VideoCapture), the images are loaded in the BGR color space.
Reference :
Note: In the case of color images, the decoded images will have the channels stored in B G R order.
Link : https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#imread)
To convert you can use
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
and vice versa.
To check if the image is in RGB or BGR format we can use:
import cv2
from PIL import Image
image = cv2.imread('image path')
img = Image.fromarray(image)
img.mode
I am able to convert an image from the RGB to HSV colorspace, but how can I then manipulate those values using the HSV scale as outlined in the PIL documentation?
img = Image.open("assets/image.png")
img = img.convert('HSV')
img.show()
You can convert the image to a NumPy array and manipulate it from there.
For example, to shift the hue:
import numpy as np
from PIL import image
def hue_shift(img, amount):
hsv_img = img.convert('HSV')
hsv = np.array(hsv_img)
hsv[..., 0] = (hsv[..., 0]+amount) % 360
new_img = Image.fromarray(hsv, 'HSV')
return new_img.convert('RGB')
If you get HSV images, you can use opencv library.
import argparse.
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i", "-image", required = True, help = "Path to the image");
args = vars(ap.parse_args());
image = cv2.imread(args["image"]);
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)