亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 .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 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號