为了账号安全,请及时绑定邮箱和手机立即绑定

如何从 .las 文件的 16 位值中过滤颜色?

如何从 .las 文件的 16 位值中过滤颜色?

倚天杖 2022-10-25 16:10:23
我有一个.las包含内容的文件,如下所示:   array([( 860297,  472942, 67187, 11051, 73, 0, 0, 0, 1, 12079, 11051,  9252),           ( 859318,  473132, 67336,  8995, 73, 0, 0, 0, 1,  9252,  8995,  9509),           ( 859941,  473665, 67550, 12079, 73, 0, 0, 0, 1, 12079, 12850, 10023),           ...,           (1057593, 1184341, 75212, 19018, 73, 0, 0, 0, 1, 20303, 19275, 16191),           (1057764, 1184161, 74734, 14906, 73, 0, 0, 0, 1, 15934, 14906, 13878),           (1057548, 1184058, 74881, 26214, 73, 0, 0, 0, 1, 28784, 25957, 21074)],          dtype=[('X', '<i4'), ('Y', '<i4'), ('Z', '<i4'), ('intensity', '<u2'), ('bit_fields', 'u1'),                 ('raw_classification', 'u1'), ('scan_angle_rank', 'i1'), ('user_data', 'u1'),                 ('point_source_id', '<u2'), ('red', '<u2'), ('green', '<u2'), ('blue', '<u2')])正如我在 .las 文件中发现的那样,RGB 中的值以 16 位颜色存储。我写了 2 个函数 - 将颜色转换为 8 位:opened_file = open("my.las") #examplecolors = opened_file.points[["red", "green", 'blue']]>>> array([(12079, 11051,  9252), (9252,  8995,  9509), (12079, 12850, 10023), ...])def eightbitify(colour):    return colour/256接下来我尝试将颜色转换为HSV,因此我将有可能过滤特定颜色:def to_hsv(rgb: np.ndarray):    empty_image = np.zeros((1, 1, 3), dtype=np.uint8)    empty_image[0, 0] = list(reversed(rgb))    hsv = cv2.cvtColor(empty_image, cv2.COLOR_RGB2HSV)    return list(reversed(hsv[0, 0]))我没有找到更好的解决方案,因为 OpenCV 需要图像 - 所以我正在为 1 个像素创建图像并将其转换(太丑了:/)。问题是我收到的值与任何互联网 RGB-HSV 计算器结果都不匹配。我以这种方式使用这些功能:def convert_color(rgb_16: np.ndarray)->np.ndarray:    converted_color = np.array([int(eightbitify(rgb_16[0])), int(eightbitify(rgb_16[1])), int(eightbitify(rgb_16[2]))])    converted_hsv = to_hsv(converted_color)    return converted_hsvfor color in colors:    converted = convert_color(color)所以我的问题是:1.将 RGB 值从我的opened_file.points初始数组转换为 HSV 的最佳方法是什么?2.如何从我opened_file.points只使用 numpy 中过滤特定颜色?可能我可以将一些转换功能应用于 RGB 值等?不幸的是,我对 numpy 的经验非常少,所以我需要帮助。谢谢!
查看完整描述

1 回答

?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

我不熟悉您的图像类型,但希望可以在 Numpy 和 OpenCV 方面帮助您。


#!/usr/bin/env python3


import pylas

import cv2

import numpy as np


# Open file

las = pylas.read('W2.las')


# Extract all the red values into a Numpy array, likewise green and blue

R = las.points["red"]

G = las.points["green"]

B = las.points["blue"]


# Make a Numpy image by stacking the RGB values and converting to uint8

BGR = (np.dstack((B,G,R))>>8).astype(np.uint8)


# Convert to HSV

HSV = cv2.cvtColor(BGR, cv2.COLOR_BGR2HSV)


# Define lower and uppper limits of what we call "green"

range_lo = np.array([70,30,30])

range_hi = np.array([90,255,255])


# Mask image to only select greens

mask = cv.inRange(HSV,range_lo,range_hi)


# Change image to red where we found green

image[mask>0]=(0,0,255)


cv.imwrite("result.png",image)

目前,我不知道图像的形状,所以它是一条 4400 万像素的长线,但它只需要 areshape()即可使其正确。请注意,每个 True/False 值都mask将成为像素列表中的索引,并指示该像素是否为绿色。

您可能必须使用参数的下限值和上限值- 请参阅我range答案。

Stack Overflow 用户 @nathancy 在这里制作了一个相当简洁的 HSV 颜色选择器,带有滑块。


查看完整回答
反对 回复 2022-10-25
  • 1 回答
  • 0 关注
  • 115 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信