在Python中创建用于DNG和处理的图像掩码

2024-06-09 00:13:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个原始图像,从手机摄像头保存为.dng。我想用Python中的OpenCV库分割颜色。图片主要是黑色和绿色的,我想得到图像中绿色部分的值。我从未以这种方式处理过图像,我完全不懂。我下面的tutorial是说将图像转换为H.S.V.颜色空间并使用遮罩,但是如果不是在其他步骤中,我会遇到遮罩的问题。我用的是谷歌Colabs

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

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

enter image description here

我提供了所有这些信息,试图描述原始图像和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, 2988, 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,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还是相当陌生的


Tags: and图像importpltgreenrgbcv2hsv
1条回答
网友
1楼 · 发布于 2024-06-09 00:13:13

我不确定你认为问题出在哪里。基本上,图像的红色和蓝色通道是空的(查看下面输出中的平均值),您可以丢弃它们,只使用绿色通道作为遮罩

#!/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格式

相关问题 更多 >