OpenCV: What is the dimension of the array representing a BGR image? - python

According to documentation, a BGR image is represented this way in OpenCV:
(source: opencv.org)
My question: what is the dimension of the array displayed by this picture ?

From the docs
Image properties include number of rows, columns and channels, type of image data, number of pixels etc.
Shape of image is accessed by img.shape. It returns a tuple of number of rows, columns and channels (if image is color):
>>> print img.shape
(342, 548, 3)
Note
If image is grayscale, tuple returned contains only number of rows and columns. So it is a good method to check if loaded image is grayscale or color image.

Related

Scale array values after resizing an image in python

I have a 480x640 image and I am cropping to 195x195.
The original 480x640 has a corresponding (68,2) array which can be used to further process the image. Since I cropped the image, how am can I rescale the (68,2) array values to correspond to the new cropped image?
I tried to do something along the line:
shape = shape*((195*195)/(480*640))
but it was no use. N.B. shape is the (68,2) array

How to change opacity of a region in an RGB image (numpy, cv2)

I have a RGB image which as a given shape like (200, 200, 3) as an np.array with values between 0 and 255.
I also have a boolean np.array of shape (200, 200), which I'll call mask.
What I want is to reduce the opacity of the image in the mask while leaving the rest unchanged, by a given amount.
I know I can convert the RGB image to RGBA with cv2.cvtColor(img, cv2.COLOR_RGB2RGBA)
Edit:
So I could fix the problem of changing the image at the mask but then I get a RGBA image, but I need it to be RGB. By using cv2.cvtColor(img_rgba, cv2.COLOR_RGBA2RGB) it just looses the A channel and I get back the original image. Any ideas on how to convert RGBA to RGB without losing the A channel?

How does cv2.imencode works? Output different shapes of two images of the same shape

I have two images with same shape and using cv2.imencode I got two array with different shapes, why this? How can I get encoded images of the same shape?
print(img1.shape)
OUTPUT: (720, 1280, 3)
print(img2.shape)
OUTPUT: (720, 1280, 3)
img1_encoded = cv2.imencode('.png', img1)
img2_encoded = cv2.imencode('.png', img2)
print(img1_encoded)
OUTPUT: (927851, 1)
print(img2_encoded)
OUTPUT: (73513, 1)
The function imencode compresses an image and stores it in the memory buffer that is resized to fit the result.
img.shape returns the dimensions of the image and the number of channels in the image, in this case both your images have 3 channels indicating that they are colour images.
In laymen terms, image compression is dependant upon the frequency of a particular colour component within an image.
Given that you are encoding different images, they will always have a different output size.
http://www.libpng.org/pub/png/book/chapter09.html - Here is a link into how png compression works.

Converting an image into a vector of pixels

I am trying to convert an image into an array of pixels.
Here is my current code.
im = Image.open("beeleg.png")
pixels = im.load()
im.getdata() # doesn't work
print(pixels # doesn't work
Ideally, my end goal is to convert the image into a vector of just pixels, so for instance if I have an image of dimensions 100x100, then I want a vector of dimensions 1x10000, where each value is between [0, 255]. Then, divide each of the values in the array by 256 and add a bias of 1 in the front of the vector. However, I am not able to proceed with all this without being able to obtain an array. How to proceed?
Scipy's ndimage library is generally the go-to library for working with pixels as data (arrays). You can load an image from file (most common formats supported) using scipy.ndimage.imread into a numpy array which can be easily reshaped and mathematically operated on. The mode keyword can be used to specify a colorspace transformation upon load (convert an RGB image to black and white). In your case you asked for single color pixels from 0-255 (8bit grayscale) so you would use mode='L'. See The Documentation for usage / more useful functions.
If use OpenCV, gray=cv2.imread(image,0) will return a grayscale image with n rows x m cols single channel numpy array. rows, cols = gray.shape will return the height and width of the image.

Python - get white pixels of image

I'm would like to go from an image filename to a list of coordinates of the white pixels in the image.
I know it involves PIL. I have tried using Image.load() but this doesn't help because the output is not indexable (to use in a for loop).
You can dump an image as a numpy array and manipulate the pixel values that way.
from PIL import Image
import numpy as np
im=Image.open("someimage.png")
pixels=np.asarray(im.getdata())
npixels,bpp=pixels.shape
This will give you an array whose dimensions will depend on how many bands you have per pixel (bpp above) and the number of rows times the number of columns in the image -- shape will give you the size of the resulting array. Once you have the pixel values, it ought to be straightforward to filter out those whose values are 255
To convert a numpy array back to an image use:
im=Image.fromarray(pixels)

Categories

Resources