Skip to content

Commit

Permalink
update simple_demosaicing
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYer22 committed Mar 11, 2023
1 parent d35ffb6 commit bdd26d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ For document, please see example code of `DngFile.test()` at [`process_raw/proce
**Python example:**
```Python
import cv2
import numpy as np
from process_raw import DngFile

# Download raw.dng for test:
Expand Down
2 changes: 1 addition & 1 deletion process_raw/__info__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.2.0"
__version__ = "0.2.1"
__description__ = "A python package to process raw and dng file, supporting demosaicing with gamma correction."
__license__ = "MIT"
__author__ = "DIYer22"
Expand Down
33 changes: 24 additions & 9 deletions process_raw/process_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,30 @@ def pow_func_for_uint8(self,):
)

@staticmethod
def simple_demosaicing(raw, pattern=None):
return np.concatenate(
[
raw[1::2, ::2, None],
(raw[::2, ::2, None] / 2 + raw[::2, ::2, None] / 2).astype(raw.dtype),
raw[::2, 1::2, None],
],
def simple_demosaicing(raw, pattern):
"""
Perform a simple demosaicing of a raw image by merage four pixels to one pixel.
Args:
raw (ndarray): A 2D numpy array representing a single-channel raw image.
pattern (str): A string indicating the Bayer pattern of the raw image, which can be "RGGB", "BGGR", "GBRG", or "GRBG".
Returns:
ndarray: (h/2, w/2, 3) RGB image obtained by averaging
the values of each color channel in the raw image according to the Bayer pattern.
"""

d = {}
for color, channel in zip(pattern, [raw[::2, ::2, None], raw[::2, 1::2, None], raw[1::2, ::2, None], raw[1::2, 1::2, None]]):
d[color] = d.get(color, []) + [channel]


RGB = np.concatenate(
[np.mean(d[color], 0) for color in "RGB"]
,
-1,
)
return RGB

@staticmethod
def test(raw=None):
Expand All @@ -86,7 +101,7 @@ def test(raw=None):
}
boxx.tree(imgs)
boxx.shows(imgs, png=True)
boxx.g()
boxx.g() # make all locals() to Python console for debug


class DngFile:
Expand Down Expand Up @@ -180,7 +195,7 @@ def test():
boxx.tree(dict(raw=raw, rgb1=rgb1, rgb2=rgb2))

DngFile.save(dngp + "-save.dng", raw, bit=dng.bit, pattern=dng.pattern)
boxx.g()
boxx.g() # make all locals() to Python console for debug
return raw

read_dng = read
Expand Down

0 comments on commit bdd26d2

Please sign in to comment.