2016-08-26 6 views
1

VIPS (및 Python)의 다양한 색조 범위의 16 비트 tiff 파일에 다양한 변환을 적용해야합니다. 나는 그럭저럭 그렇게 할 수 있었다. 그러나 나는 VIPS에 초보적이다. 그리고 나는 효율적인 방법으로 이것을하고있다라고 확신하지 않는다. 이 이미지는 각각 수백 메가 바이트이며, 초과 단계를 자르면 이미지 당 몇 초를 절약 할 수 있습니다.VIPS/Python에서 특정 색조 범위로 변환을 적용하는 방법

아래 코드에서 얻은 것과 동일한 결과를 얻는보다 효율적인 방법이 있는지 궁금합니다. 예를 들어 조회 테이블을 사용하는 경우 (실제로 VIPS에서 어떻게 작동하는지 알 수 없습니다). 이 코드는 빨간색 채널의 그림자를 분리하고 변환을 통해 전달합니다.

im = Vips.Image.new_from_file("test.tiff") 

# Separate the red channel 
band = im[0] 

# Find the tone limit for the bottom 5% 
lim = band.percent(5) 

# Create a mask using the tone limit 
mask = (band <= lim) 

# Convert the mask to 16 bits 
mask = mask.cast(band.BandFmt, shift = True) 

# Run the transformation on the image and keep only the shadow areas 
new_shadows = (65535 * (shadows/lim * 0.1)) & mask 

각 색조 범위 (하이라이트, 그림자, 중간 톤, 내가 함께 원래 밴드 재구성하는 모든 결과 이미지를 추가하기위한 더 많거나 적은 유사한 코드를 실행 한 후 :

new_band = (new_shadows.add(new_highlights).add(new_midtones)).cast(band.BandFmt) 
+0

예, 당신은 꽤 빨리 그것을 만들 수 있어야합니다. 예를 들어, 각 밴드에 대해 한 번, 세 번 ('추측? ')'퍼센트 '를 실행 중입니다. 대신, 처음에 이미지의 히스토그램을 찾아 분석하여 각 밴드의 비율을 얻으십시오. 우리가 시도해 볼 수있는 작지만 완전한 예를 게시하십시오. 편집 : 당신이 말한대로 물론 또 다른 큰 스피드 업을 줄 수있는 LUT를 사용할 수 있습니다. – user894763

답변

2

을 나는 당신에게 데모를했다

#!/usr/bin/python 

import sys 

import gi 
gi.require_version('Vips', '8.0') 
from gi.repository import Vips 

im = Vips.Image.new_from_file(sys.argv[1]) 

# find the image histogram 
# 
# we'll get a uint image, one pixel high and 256 or 
# 65536 pixels across, it'll have three bands for an RGB image source 
hist = im.hist_find() 

# find the normalised cumulative histogram 
# 
# for a 16-bit source, we'll have 65535 as the right-most element in each band 
norm = hist.hist_cum().hist_norm() 

# search from the left for the first pixel > 5%: the position of this pixel 
# will give us the pixel value that 5% of pixels fall below 
# 
# .profile() gives back a pair of [column-profile, row-profile], we want index 1 
# one. .getpoint() reads out a pixel as a Python array, so for an RGB Image 
# we'll have something like [19.0, 16.0, 15.0] in shadows 
shadows = (norm > 5.0/100.0 * norm.width).profile()[1].getpoint(0, 0) 

# Now make an identity LUT that matches our original image 
lut = Vips.Image.identity(bands = im.bands, 
          ushort = im.format == Vips.BandFormat.USHORT) 

# do something to the shadows ... here we just brighten them a lot 
lut = (lut < shadows).ifthenelse(lut * 100, lut) 

# make sure our lut is back in the original format, then map the image through 
# it 
im = im.maplut(lut.cast(im.format)) 

im.write_to_file(sys.argv[2]) 

그것은 소스 이미지에서 하나의 발견 - 히스토그램 작업을 수행 한 후 단일지도 히스토그램 : VIP에 히스토그램 기능이 그런 짓을하는 방법을 보여주는 프로그램 작동하므로 빠르다.

그림자를 조정하는 것만으로 중간 색조와 하이라이트를 약간 늘려야하지만 단일 초기 히스토그램에서 세 가지 수정을 모두 수행 할 수 있으므로 더 느려서는 안됩니다. 당신은 더 이상 질문이 있으면

는 libvips 추적기에 대한 문제를여십시오 :

https://github.com/jcupitt/libvips/issues