在 Python 中为 DNG 和处理创建图像蒙版

问题描述

我有一张从手机相机保存为 .dng 的 RAW 图像。我想用 Python 中的 OpenCV 库分割颜色。图片主要是黑色和绿色,我想获取图像绿色部分的值。我没有以这种方式处理图像,而且完全一无所知。我正在关注的 tutorial 说要将图像转换为 H.S.V.颜色空间并使用蒙版,但如果不是在其他步骤中,我会遇到蒙版问题。我正在使用 Google Colab。

import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
from google.colab import drive
import imageio
import scipy.misc
import skimage.filters
import skimage.metrics
from PIL import Image

# Colabs...
!pip install rawpy

import rawpy

# Colabs...
!pip install ExifRead

import exifread

#image
plate = rawpy.imread('/content/drive/MyDrive/Colab Notebooks/copy of 0724201706a.dng')

#EXIF
plate_x = open('/content/drive/MyDrive/Colab Notebooks/copy of 0724201706a.dng','rb')

#There are several lines returned. I've left this out for Now...
plate_tags = exifread.process_file(plate_x)
plate_tags

plt.imshow(plate.raw_image)

picture of raw image as imported

plate_rgb = plate.postprocess( use_camera_wb=True)

plt.imshow(plate_rgb)

RBG version of image

plate_rgb.shape
(5312,2988,3)

这些是经过轻微编辑的 RGB、RGB 图像的绿色通道和蓝色通道。

enter image description here

RGB 中每个通道的值的直方图。图像。其他通道为 0,但绿色有不同的值。

enter image description here

我提供了所有这些信息来尝试描述 RAW 图像和 R.G.B.

教程说要转换为 H.S.V.色彩空间。我在某处看到图像以 B.G.R. 的形式出现,所以我尝试了两种方法

plateRGB_hsv = cv2.cvtColor(plate_rgb,cv2.COLOR_RGB2HSV)
plateBGR_hsv = cv2.cvtColor(plate_rgb,cv2.COLOR_BGR2HSV)

two versions of HSV

# A lower and upper threshold for mask
hsv_green_lo = (59,100,135) #h = 50,s = 100,v = 135)
hsv_green_hi = (75,250,255) #h = 75,s = 250,v = 255)

plateRGB_hsv.shape
(5312,3)

# Create mask
green_thr = cv2.inRange(plateRGB_hsv,hsv_green_lo,hsv_green_hi)

# Apply mask
img_msk = cv2.bitwise_and(plateRGB_hsv,plateRGB_hsv,green_thr)

plt.subplot(1,2,1)
plt.imshow(green_thr)
plt.subplot(1,2)
plt.imshow(img_msk)
plt.show()

inRange(遮罩层创建)和 bitwise_and(遮罩应用)的输出

enter image description here

rgb_out = cv2.bitwise_and(plate_rgb,plate_rgb,green_thr)

plt.imshow(rgb_out)
plt.plot()

应用掩码,这是输出

rgb after mask

所以我似乎没有正确创建面具?使用糟糕的掩码,bitwise_and 运行时没有变化,看起来像?我不知道为什么面具失败了。事实是 R.G.B.或 H.S.V.是不是在三个渠道使面膜和面膜应用复杂化?

图片here

评论和提交答案后编辑:

我不清楚我希望我的输出是什么样的。我说的是“绿色”,但我真的希望它看起来像这样:

enter image description here

我按照建议制作了一个只有绿色通道的新阵列。 green_c = plate_rgb[...,1]

但是现在,我对如何创建蒙版感到困惑。由于阵列只是一层,我认为它是一个“层”,就像在 G.I.S.或 GIMP,如何将不需要的值更改为黑色?对不起,如果这很明显。我对 Python 还是很陌生。

解决方法

我不太确定您认为问题是什么。基本上,图像的红色和蓝色通道是空的(在下面的输出中查看它们的均值值),您也可以丢弃它们并仅使用绿色通道作为您的蒙版。

#!/usr/bin/env python3

import rawpy
import matplotlib.pyplot as plt
import numpy as np

# Load and process raw DNG
plate = rawpy.imread('raw.dng')
rgb = plate.postprocess()

# Show what we've got
print(f'Dimensions: {rgb.shape},dtype: {rgb.dtype}')
R = rgb[...,0]
print(f'R channel: min={R.min()},mean={R.mean()},max={R.max()}')
G = rgb[...,1]
print(f'G channel: min={G.min()},mean={G.mean()},max={G.max()}')
B = rgb[...,2]
print(f'B channel: min={B.min()},mean={B.mean()},max={B.max()}')

# Display green channel
plt.imshow(G,cmap='gray')
plt.show()

enter image description here

图像输出

Dimensions: (5312,2988,3),dtype: uint8
R channel: min=0,mean=0.013673103558813567,max=255
G channel: min=0,mean=69.00267554908389,max=255
B channel: min=0,mean=0.017269189710649828,max=255

关键字:Python、图像处理、rawpy、DNG、Adobe DNG 格式。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...