#!/usr/bin/env python
#******************************************************************************
# $Id$
#
# Project: GDAL Python Interface
# Purpose: Script to merge greyscale as intensity into an RGB(A) image, for
# instance to apply hillshading to a dem colour relief.
# Author: Frank Warmerdam, warmerdam@pobox.com
# Trent Hare (USGS)
#
#******************************************************************************
# Copyright (c) 2009, Frank Warmerdam
# Copyright (c) 2010, Even Rouault <even dot rouault at mines-paris dot org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#****************************************************************************** from osgeo import gdal
import numpy
import sys # =============================================================================
# rgb_to_hsv()
#
# rgb comes in as [r,g,b] with values in the range [0,255]. The returned
# hsv values will be with hue and saturation in the range [0,1] and value
# in the range [0,255]
#
def rgb_to_hsv( r,g,b ): maxc = numpy.maximum(r,numpy.maximum(g,b))
minc = numpy.minimum(r,numpy.minimum(g,b)) v = maxc minc_eq_maxc = numpy.equal(minc,maxc) # compute the difference, but reset zeros to ones to avoid divide by zeros later.
ones = numpy.ones((r.shape[0],r.shape[1]))
maxc_minus_minc = numpy.choose( minc_eq_maxc, (maxc-minc,ones) ) s = (maxc-minc) / numpy.maximum(ones,maxc)
rc = (maxc-r) / maxc_minus_minc
gc = (maxc-g) / maxc_minus_minc
bc = (maxc-b) / maxc_minus_minc maxc_is_r = numpy.equal(maxc,r)
maxc_is_g = numpy.equal(maxc,g)
maxc_is_b = numpy.equal(maxc,b) h = numpy.zeros((r.shape[0],r.shape[1]))
h = numpy.choose( maxc_is_b, (h,4.0+gc-rc) )
h = numpy.choose( maxc_is_g, (h,2.0+rc-bc) )
h = numpy.choose( maxc_is_r, (h,bc-gc) ) h = numpy.mod(h/6.0,1.0) hsv = numpy.asarray([h,s,v]) return hsv # =============================================================================
# hsv_to_rgb()
#
# hsv comes in as [h,s,v] with hue and saturation in the range [0,1],
# but value in the range [0,255]. def hsv_to_rgb( hsv ): h = hsv[0]
s = hsv[1]
v = hsv[2] #if s == 0.0: return v, v, v
i = (h*6.0).astype(int)
f = (h*6.0) - i
p = v*(1.0 - s)
q = v*(1.0 - s*f)
t = v*(1.0 - s*(1.0-f)) r = i.choose( v, q, p, p, t, v )
g = i.choose( t, v, v, q, p, p )
b = i.choose( p, p, t, v, v, q ) rgb = numpy.asarray([r,g,b]).astype(numpy.uint8) return rgb # =============================================================================
# Usage() def Usage():
print("""Usage: hsv_merge.py [-q] [-of format] src_color src_greyscale dst_color where src_color is a RGB or RGBA dataset,
src_greyscale is a greyscale dataset (e.g. the result of gdaldem hillshade)
dst_color will be a RGB or RGBA dataset using the greyscale as the
intensity for the color dataset.
""")
sys.exit(1) # =============================================================================
# Mainline
# ============================================================================= argv = gdal.GeneralCmdLineProcessor( sys.argv )
if argv is None:
sys.exit( 0 ) format = 'GTiff'
src_color_filename = None
src_greyscale_filename = None
dst_color_filename = None
quiet = False # Parse command line arguments.
i = 1
while i < len(argv):
arg = argv[i] if arg == '-of':
i = i + 1
format = argv[i] elif arg == '-q' or arg == '-quiet':
quiet = True elif src_color_filename is None:
src_color_filename = argv[i] elif src_greyscale_filename is None:
src_greyscale_filename = argv[i] elif dst_color_filename is None:
dst_color_filename = argv[i]
else:
Usage() i = i + 1 if dst_color_filename is None:
Usage() datatype = gdal.GDT_Byte hilldataset = gdal.Open( src_greyscale_filename, gdal.GA_ReadOnly )
colordataset = gdal.Open( src_color_filename, gdal.GA_ReadOnly ) #check for 3 or 4 bands in the color file
if (colordataset.RasterCount != 3 and colordataset.RasterCount != 4):
print('Source image does not appear to have three or four bands as required.')
sys.exit(1) #define output format, name, size, type and set projection
out_driver = gdal.GetDriverByName(format)
outdataset = out_driver.Create(dst_color_filename, colordataset.RasterXSize, \
colordataset.RasterYSize, colordataset.RasterCount, datatype)
outdataset.SetProjection(hilldataset.GetProjection())
outdataset.SetGeoTransform(hilldataset.GetGeoTransform()) #assign RGB and hillshade bands
rBand = colordataset.GetRasterBand(1)
gBand = colordataset.GetRasterBand(2)
bBand = colordataset.GetRasterBand(3)
if colordataset.RasterCount == 4:
aBand = colordataset.GetRasterBand(4)
else:
aBand = None hillband = hilldataset.GetRasterBand(1)
hillbandnodatavalue = hillband.GetNoDataValue() #check for same file size
if ((rBand.YSize != hillband.YSize) or (rBand.XSize != hillband.XSize)):
print('Color and hilshade must be the same size in pixels.')
sys.exit(1) #loop over lines to apply hillshade
for i in range(hillband.YSize):
#load RGB and Hillshade arrays
rScanline = rBand.ReadAsArray(0, i, hillband.XSize, 1, hillband.XSize, 1)
gScanline = gBand.ReadAsArray(0, i, hillband.XSize, 1, hillband.XSize, 1)
bScanline = bBand.ReadAsArray(0, i, hillband.XSize, 1, hillband.XSize, 1)
hillScanline = hillband.ReadAsArray(0, i, hillband.XSize, 1, hillband.XSize, 1) #convert to HSV
hsv = rgb_to_hsv( rScanline, gScanline, bScanline ) # if there's nodata on the hillband, use the v value from the color
# dataset instead of the hillshade value.
if hillbandnodatavalue is not None:
equal_to_nodata = numpy.equal(hillScanline, hillbandnodatavalue)
v = numpy.choose(equal_to_nodata,(hillScanline,hsv[2]))
else:
v = hillScanline #replace v with hillshade
hsv_adjusted = numpy.asarray( [hsv[0], hsv[1], v] ) #convert back to RGB
dst_color = hsv_to_rgb( hsv_adjusted ) #write out new RGB bands to output one band at a time
outband = outdataset.GetRasterBand(1)
outband.WriteArray(dst_color[0], 0, i)
outband = outdataset.GetRasterBand(2)
outband.WriteArray(dst_color[1], 0, i)
outband = outdataset.GetRasterBand(3)
outband.WriteArray(dst_color[2], 0, i)
if aBand is not None:
aScanline = aBand.ReadAsArray(0, i, hillband.XSize, 1, hillband.XSize, 1)
outband = outdataset.GetRasterBand(4)
outband.WriteArray(aScanline, 0, i) #update progress line
if not quiet:
gdal.TermProgress_nocb( (float(i+1) / hillband.YSize) )

颜色空间变换(RGB-HSV)的更多相关文章

  1. 色彩空间-- RGB\HSV

    颜色空间 标签(空格分隔): 计算机视觉 颜色通常用三个独立的属性来描述,三个独立变量综合作用,自然就构成一个空间坐标,这就是颜色空间. RGB和CMY颜色模型都是面向硬件的,而HSV(Hue Sat ...

  2. 色彩转换——RGB & HSV

    RGB to HSV The R,G,B values are divided by 255 to change the range from 0..255 to 0..1: R' = R/255 G ...

  3. RGB HSV HLS三种色彩模式转换(C语言实现)

    Android项目上处理图像的代码(注释全部去掉) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  4. 【转载】颜色空间-RGB、HSI、HSV、YUV、YCbCr的简介

    转载自缘佳荟的博客. 颜色通常用三个相对独立的属性来描述,三个独立变量综合作用,自然就构成一个空间坐标,这就是颜色空间.而颜色可以由不同的角度,用三个一组的不同属性加以描述,就产生了不同的颜色空间.但 ...

  5. RGB颜色空间、色调、饱和度、亮度,HSV颜色空间详解

    本文章会详细的介绍RGB颜色空间与RGB三色中色调.饱和度.亮度之间的关系,最后会介绍HSV颜色空间! RGB颜色空间 概述 RGB颜色空间以R(Red:红).G(Green:绿).B(Blue:蓝) ...

  6. 由RGB到HSV颜色空间的理解

    1. RGB模型 2. HSV模型 3. 如何理解RGB与HSV的联系 4. HSV在图像处理中的应用 5. opencv中RGB-->HSV实现 在图像处理中,最常用的颜色空间是RGB模型,常 ...

  7. RGB和HSV颜色空间

    转载:http://blog.csdn.net/carson2005/article/details/6243892 RGB颜色空间: RGB(red,green,blue)颜色空间最常用的用途就是显 ...

  8. LCD LED OLED区别 以及RGB、YUV和HSV颜色空间模型

    led 液晶本身不发光,而是有背光作为灯源,白色是由红绿蓝三色组成,黑色是,液晶挡住了led灯光穿过显示器. lcd比led更薄. oled:显示黑色时,灯是灭的,所以显示黑色更深,效果更好. 这就不 ...

  9. RGB、YUV和HSV颜色空间模型

    一.概述 颜色通常用三个独立的属性来描述,三个独立变量综合作用,自然就构成一个空间坐标,这就是颜色空间.但被描述的颜色对象本身是客观的,不同颜色空间只是从不同的角度去衡量同一个对象.颜色空间按照基本机 ...

随机推荐

  1. Gumby – 基于 Sass 的灵活的,响应式 CSS 框架

    Gumby 框架是一个基于 SASS 的灵活的,响应式的 CSS 框架.可以借助其灵活,响应式的网格系统和 UI 套件快速创建逻辑的页面布局和应用程序原型. 您可能感兴趣的相关文章 35个让人惊讶的 ...

  2. Winform应用程序实现通用遮罩层

    在WEB上,我们在需要进行大数据或复杂逻辑处理时,由于耗时较长,一般我们会在处理过程中的页面上显示一个半透明的遮罩层,上面放个图标或提示:正在处理中...等字样,这样用户体验就比较好了,然而如果在Wi ...

  3. CocoaPods安装使用以及常见问题

    什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理.开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和第三方 ...

  4. iOS滤镜实现之Nashville【instagram】

    Nashville是Instagram众多滤镜中最惊艳的一款,独特的奶昔色调赋予照片童话般的唯美感觉.适用范围:营造浪漫唯美的感觉.的确如此啊有2张输入图像 顶点着色有2组坐标NSString *co ...

  5. Visual Studio开发Cordova应用示例

    作者:Grey 原文地址:http://www.cnblogs.com/greyzeng/p/5455728.html 本文的GIF动画均使用ScreenToGif进行录制. Cordova是什么? ...

  6. 用Qt写软件系列五:一个安全防护软件的制作(1)

    引言 又有许久没有更新了.Qt,我心爱的Qt,为了找工作不得不抛弃一段时间,业余时间来学一学了.本来计划要写一系列关于Qt组件美化的博文,但是写了几篇之后就没坚持下去了.技术上倒是问题不大,主要是时间 ...

  7. c# 中基类变量指向派生类对象的实例化

    这一篇文章转载自:http://www.xuebuyuan.com/390279.html 我对这篇文章进行了一一的验证,确实是这样子的,也明白了很多东西,觉得很有用,转载过来希望能够帮助大家. 1. ...

  8. Dapper学习 - Dapper的基本用法(一) - 查询

    上一篇, 提到Query<Test>查询的时候, 如果Test中包含自定义class, Dapper不会给自定义class完成映射, 而是直接给null, 其实是可以实现的, 答案就在下面 ...

  9. Emit学习(2) - IL - 常用指令介绍

    学习Emit必不可少的, 会使用到IL中间代码. 初见IL代码, 让我有一种汇编的感觉, 让我想起了, 大学时, 学习8051的汇编语言. 多的就不扯了, 直接进入正题, OpCodes指令集是不是有 ...

  10. C语言学习007:重定向标准输入和输出

    先来完成一个将输入数据转换成json格式输出的小任务 #include <stdio.h> int main(){ float latitude; float longtitude; ]; ...