labelImg
labelImg copied to clipboard
support 16bit tif image
Can it support 16 bits tif image? image size almost 70Mb
@gavincoder2020 Does it? I want to load 16 bit images too.
You can change the loadFile function to support loading of other types of images, I think.
I am using pyqt5. the file size is 578kb. tif image is not opening in my case as well. @YashRunwal @tzutalin can you tell me what to edit?
@SrutiBh I hope this helps a bit:
- Install opencv:
pip install opencv-python
- import opencv in labelImg.py:
import cv2
- go to function
read()
in labelImg.py - replace the contents of the try block with the following (untested, let me know if it works for you):
image = cv2.imread(filename, cv2.IMREAD_ANYDEPTH)
return QImage(image, image.shape[1], image.shape[0], QImage.Format_RGB888)
The parameter cv2.IMREAD_ANYDEPTH will give you the image in actual 16 bit pixels. I have not tested what Qt will do to it when you load a 16 bit image into a QImage. It's possible that you have to do some adaptations after loading the image to optimize the contrast, but in my book that's an advantage of using 16 bit images in the first place, since you have the full intensity range and can dynamically adapt it as you need.
Since Qt does not seem to support 16 bit images, I guess it will fail, so there are two options:
- Let opencv do the conversion to 8 bit:
image = cv2.imread(filename)
(leave out the second parameter) - Do the conversion yourself:
image = cv2.imread(filename, cv2.IMREAD_ANYDEPTH)
image = (image - image.min()) / (image.max() - image.min()) * 255
image = image.astype('uint8')
@Cerno-b it is still coming the same. if you are free can you come in email now? that will be helpful. right now whenever I am trying to open the tif file the output window is getting off
Solving this via e-mail will not be helpful to anyone else, so I'd prefer if we could try to solve this here.
Could you post a screenshot of what it looks like? Also could you upload your image somewhere? You mentioned 70MB, so maybe dropbox or something similar might be useful.
@Cerno-b sure. I am unable to upload the tif images. It's saying not supported here. I have changed the code as you suggested. Whenever I am trying to open the image the kernel is dying and the output window is getting off.
Okay, turns out it's a little bit more complicated. This works for me. It now supports RGB, RGBA and grayscale in 8 and 16 bit.
I'll put this on a branch. Would you be so nice and test whether it works with your 16 bit images and whether they look okay?
def read(filename, default=None):
try:
image = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
if image.max() > 255:
image = (image - image.min()) / (image.max() - image.min()) * 255
image = image.astype('uint8')
if len(image.shape) == 2:
image = np.tile(image, (3, 1, 1)).transpose((1, 2, 0))
height, width, channels = image.shape
if channels == 3:
# convert BGR to RGB
image[:, :, [0, 1, 2]] = image[:, :, [2, 1, 0]]
image = image.copy()
return QImage(image, width, height, image.strides[0], QImage.Format_RGB888)
elif channels == 4:
# convert BGRA to RGBA
image[:, :, [0, 1, 2, 3]] = image[:, :, [2, 1, 0, 3]]
image = image.copy()
return QImage(image, width, height, image.strides[0], QImage.Format_RGBA8888)
else:
assert False
except:
return default
@Cerno-b Thank you so much. This works well for me now
@SrutiBh If you run into any incompatibility problems with other image formats, please let me know.
@Cerno-b sure, I will
Okay, turns out it's a little bit more complicated. This works for me. It now supports RGB, RGBA and grayscale in 8 and 16 bit.
I'll put this on a branch. Would you be so nice and test whether it works with your 16 bit images and whether they look okay?
def read(filename, default=None): try: image = cv2.imread(filename, cv2.IMREAD_UNCHANGED) if image.max() > 255: image = (image - image.min()) / (image.max() - image.min()) * 255 image = image.astype('uint8') if len(image.shape) == 2: image = np.tile(image, (3, 1, 1)).transpose((1, 2, 0)) height, width, channels = image.shape if channels == 3: # convert BGR to RGB image[:, :, [0, 1, 2]] = image[:, :, [2, 1, 0]] image = image.copy() return QImage(image, width, height, image.strides[0], QImage.Format_RGB888) elif channels == 4: # convert BGRA to RGBA image[:, :, [0, 1, 2, 3]] = image[:, :, [2, 1, 0, 3]] image = image.copy() return QImage(image, width, height, image.strides[0], QImage.Format_RGBA8888) else: assert False except: return default
Thank you @Cerno-b . It's very helpful. It's saved me considerable time😊