John
John

Reputation: 155

Opencv Python open dng format

I can't figure out how to open a dng file in opencv. The file was created when using the pro options of the Samsung Galaxy S7. The images that are created when using those options are a dng file as well as a jpg of size 3024 x 4032 (I believe that is the dimensions of the dng file as well).

I tried using the answer from here (except with 3 colors instead of grayscale) like so:

import numpy as np
fd = open("image.dng", 'rb')
rows = 4032
cols = 3024
colors = 3
f = np.fromfile(fd, dtype=np.uint8,count=rows*cols*colors)
im = f.reshape((rows, cols,colors)) #notice row, column format
fd.close()

However, i got the following error:

 cannot reshape array of size 24411648 into shape (4032,3024,3)

Any help would be appreciated

Upvotes: 7

Views: 15347

Answers (2)

Yang
Yang

Reputation: 309

process_raw supports both read and write .dng format raw image. Here is a python example:

import cv2
from process_raw import DngFile

# Download raw.dng for test:
# wget https://github.com/yl-data/yl-data.github.io/raw/master/2201.process_raw/raw-12bit-GBRG.dng
dng_path = "./raw-12bit-GBRG.dng"

dng = DngFile.read(dng_path)
rgb1 = dng.postprocess()  # demosaicing by rawpy
cv2.imwrite("rgb1.jpg", rgb1[:, :, ::-1])
rgb2 = dng.demosaicing(poww=0.3)  # demosaicing with gamma correction 0.3
cv2.imwrite("rgb2.jpg", rgb2[:, :, ::-1])
DngFile.save(dng_path + "-save.dng", dng.raw, bit=dng.bit, pattern=dng.pattern)

Upvotes: 0

Dmitrii Z.
Dmitrii Z.

Reputation: 2357

As far as i know it is possible that DNG files can be compressed (even though it is lossless format), so you will need to decode your dng image first. https://www.libraw.org/ is capable of doing that.

There is python wrapper for that library (https://pypi.python.org/pypi/rawpy)

import rawpy
import imageio

path = 'image.dng'
with rawpy.imread(path) as raw:
    rgb = raw.postprocess()

Upvotes: 6

Related Questions