Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Add 16 bit image support #718

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions labelImg.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
from PyQt4.QtGui import *
from PyQt4.QtCore import *

import cv2 # pip install opencv-python
import numpy as np

from libs.combobox import ComboBox
from libs.resources import *
from libs.constants import *
Expand Down Expand Up @@ -1564,7 +1567,40 @@ def inverted(color):
return QColor(*[255 - v for v in color.getRgb()])


def read_opencv(path):
"""Load an image via opencv and convert it to a QImage.
Supports all image formats supported by opencv, including 16 bit grayscale and color images."""
image = cv2.imread(path, 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


def read(filename, default=None):
# try loading image using opencv (for 16 bit image support)
try:
return read_opencv(filename)
except:
pass

# if opencv fails for any reason (e.g. no svg support), try the Qt image reader instead.
try:
reader = QImageReader(filename)
reader.setAutoTransform(True)
Expand Down
2 changes: 2 additions & 0 deletions requirements/requirements-linux-python3.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pyqt5==5.10.1
lxml==4.6.2
opencv-python>=4.2.0.0
numpy>=1.11.3