Reputation: 349
I want to see the number of channels for thermal images, RGB images, grayscale images and binary images.
So I write this program:
import cv2
import numpy
img = cv2.imread("B2DBy.jpg")
print('No of Channel is: ' + str(img.ndim))
cv2.imshow("Channel", img)
cv2.waitKey()
But it gives the same three channel results for all types of images? I've read this question but it gives an error:
img = cv2.imread("B2DBy.jpg", CV_LOAD_IMAGE_UNCHANGED)
NameError: name 'CV_LOAD_IMAGE_UNCHANGED' is not defined
So my question is: Is it is the right way to see the number of channels? Or, somehow, I entered three channel images all the time and thus it gives three channel output?
My inputs:
Upvotes: 1
Views: 14553
Reputation: 18925
The correct parameter in your cv2.imread
should be:
img = cv2.imread('path/to/your/image', cv2.IMREAD_UNCHANGED)
Let's have a look at your images now. I use ImageJ's Show Info...
command as well as the following Python code with OpenCV and Pillow:
import cv2
from PIL import Image
img_pil = Image.open('path/to/your/image')
print('Pillow: ', img_pil.mode, img_pil.size)
img = cv2.imread('path/to/your/image', cv2.IMREAD_UNCHANGED)
print('OpenCV: ', img.shape)
Pillow: RGB (640, 512)
OpenCV: (512, 640, 3)
ImageJ also says, that's a RGB image. So, most likely, your depth map was just saved as a RGB png
.
Pillow: RGB (332, 300)
OpenCV: (300, 332, 3)
Interestingly, ImageJ says, that's an grayscale jpg
! I assume, OpenCV and Pillow just don't support grayscale jpg
, although there seems to be a grayscale jpg
format.
Pillow: 1 (200, 140)
OpenCV: (140, 200)
Both, Pillow and OpenCV say, that's a grayscale image, which is also supported by ImageJ. Furthermore, Pillow uses mode '1'
here, which is reflected by the dithered look of the image.
Pillow: RGB (500, 333)
OpenCV: (333, 500, 3)
That's just some RGB image; ImageJ also says this.
Yes, most likely, most of your images may just be RGB images. Nevertheless, using cv2.IMREAD_UNCHANGED
at least will properly identify grayscale png
files. It's questionable, if grayscale jpg
files are properly supported.
Hope that helps!
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.1
OpenCV: 4.2.0
Pillow: 7.0.0
----------------------------------------
Upvotes: 6
Reputation: 99
If image is grayscale you will need to set a flag, 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.
image = cv2.imread('gray.jpg', cv2.IMREAD_GRAYSCALE)
image.shape
If len(img.shape)
gives you three, third element gives you number of channels.
Upvotes: 1
Reputation: 111
I'm not sure if it'll work, but documentation says this: cv.LoadImage(filename, iscolor=flag) with flags given. There's a flag "<0" that stands for "Return the loaded image as is (with alpha channel)". I would try this:
img = cv2.imread("B2DBy.jpg",iscolor=<0)
or this
img = cv2.imread("B2DBy.jpg",iscolor=CV_LOAD_IMAGE_ANYDEPTH)
Upvotes: 0