最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python庫skimage給灰度圖像染色的方法示例

 更新時(shí)間:2020年04月27日 14:08:18   作者:Ibelievesunshine  
這篇文章主要介紹了python庫skimage給灰度圖像染色的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

灰度圖像染成紅色和黃色

# 1.將灰度圖像轉(zhuǎn)換為RGB圖像
image = color.gray2rgb(grayscale_image)
# 2.保留紅色分量和黃色分量
red_multiplier = [1, 0, 0]
yellow_multiplier = [1, 1, 0]
# 3.顯示圖像
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4),
                sharex=True, sharey=True)
ax1.imshow(red_multiplier * image)
ax2.imshow(yellow_multiplier * image)

HSV圖像,H從0到1表示的顏色

hue_gradient = np.linspace(0, 1)
# print(hue_gradient.shape) # output:(50,)
hsv = np.ones(shape=(1, len(hue_gradient), 3), dtype=float)
hsv[:, :, 0] = hue_gradient

all_hues = color.hsv2rgb(hsv)

fig, ax = plt.subplots(figsize=(5, 2))
# Set image extent so hues go from 0 to 1 and the image is a nice aspect ratio.
ax.imshow(all_hues, extent=(0, 1, 0, 0.2))
ax.set_axis_off()

將灰度圖像染成不同的顏色

hue_rotations = np.linspace(0, 1, 6)

fig, axes = plt.subplots(nrows=2, ncols=3, sharex=True, sharey=True)

for ax, hue in zip(axes.flat, hue_rotations):
  # Turn down the saturation to give it that vintage look.
  tinted_image = colorize(image, hue, saturation=0.3)
  ax.imshow(tinted_image, vmin=0, vmax=1)
  ax.set_axis_off()
fig.tight_layout()

完整代碼

"""
=========================
Tinting gray-scale images
=========================

It can be useful to artificially tint an image with some color, either to
highlight particular regions of an image or maybe just to liven up a grayscale
image. This example demonstrates image-tinting by scaling RGB values and by
adjusting colors in the HSV color-space.

In 2D, color images are often represented in RGB---3 layers of 2D arrays, where
the 3 layers represent (R)ed, (G)reen and (B)lue channels of the image. The
simplest way of getting a tinted image is to set each RGB channel to the
grayscale image scaled by a different multiplier for each channel. For example,
multiplying the green and blue channels by 0 leaves only the red channel and
produces a bright red image. Similarly, zeroing-out the blue channel leaves
only the red and green channels, which combine to form yellow.
"""

import matplotlib.pyplot as plt
from skimage import data
from skimage import color
from skimage import img_as_float

grayscale_image = img_as_float(data.camera()[::2, ::2])
image = color.gray2rgb(grayscale_image)

red_multiplier = [1, 0, 0]
yellow_multiplier = [1, 1, 0]

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4),
                sharex=True, sharey=True)
ax1.imshow(red_multiplier * image)
ax2.imshow(yellow_multiplier * image)

######################################################################
# In many cases, dealing with RGB values may not be ideal. Because of that,
# there are many other `color spaces`_ in which you can represent a color
# image. One popular color space is called HSV, which represents hue (~the
# color), saturation (~colorfulness), and value (~brightness). For example, a
# color (hue) might be green, but its saturation is how intense that green is
# ---where olive is on the low end and neon on the high end.
#
# In some implementations, the hue in HSV goes from 0 to 360, since hues wrap
# around in a circle. In scikit-image, however, hues are float values from 0
# to 1, so that hue, saturation, and value all share the same scale.
#
# .. _color spaces:
#   https://en.wikipedia.org/wiki/List_of_color_spaces_and_their_uses
#
# Below, we plot a linear gradient in the hue, with the saturation and value
# turned all the way up:
import numpy as np

hue_gradient = np.linspace(0, 1)
# print(hue_gradient.shape) # output:(50,)
hsv = np.ones(shape=(1, len(hue_gradient), 3), dtype=float)
hsv[:, :, 0] = hue_gradient

all_hues = color.hsv2rgb(hsv)

fig, ax = plt.subplots(figsize=(5, 2))
# Set image extent so hues go from 0 to 1 and the image is a nice aspect ratio.
ax.imshow(all_hues, extent=(0, 1, 0, 0.2))
ax.set_axis_off()

######################################################################
# Notice how the colors at the far left and far right are the same. That
# reflects the fact that the hues wrap around like the color wheel (see HSV_
# for more info).
#
# .. _HSV: https://en.wikipedia.org/wiki/HSL_and_HSV
#
# Now, let's create a little utility function to take an RGB image and:
#
# 1. Transform the RGB image to HSV 2. Set the hue and saturation 3.
# Transform the HSV image back to RGB


def colorize(image, hue, saturation=1):
  """ Add color of the given hue to an RGB image.

  By default, set the saturation to 1 so that the colors pop!
  """
  hsv = color.rgb2hsv(image)
  hsv[:, :, 1] = saturation
  hsv[:, :, 0] = hue
  return color.hsv2rgb(hsv)


######################################################################
# Notice that we need to bump up the saturation; images with zero saturation
# are grayscale, so we need to a non-zero value to actually see the color
# we've set.
#
# Using the function above, we plot six images with a linear gradient in the
# hue and a non-zero saturation:

hue_rotations = np.linspace(0, 1, 6)

fig, axes = plt.subplots(nrows=2, ncols=3, sharex=True, sharey=True)

for ax, hue in zip(axes.flat, hue_rotations):
  # Turn down the saturation to give it that vintage look.
  tinted_image = colorize(image, hue, saturation=0.3)
  ax.imshow(tinted_image, vmin=0, vmax=1)
  ax.set_axis_off()
fig.tight_layout()

######################################################################
# You can combine this tinting effect with numpy slicing and fancy-indexing
# to selectively tint your images. In the example below, we set the hue of
# some rectangles using slicing and scale the RGB values of some pixels found
# by thresholding. In practice, you might want to define a region for tinting
# based on segmentation results or blob detection methods.

from skimage.filters import rank

# Square regions defined as slices over the first two dimensions.
top_left = (slice(100),) * 2
bottom_right = (slice(-100, None),) * 2

sliced_image = image.copy()
sliced_image[top_left] = colorize(image[top_left], 0.82, saturation=0.5)
sliced_image[bottom_right] = colorize(image[bottom_right], 0.5, saturation=0.5)

# Create a mask selecting regions with interesting texture.
noisy = rank.entropy(grayscale_image, np.ones((9, 9)))
textured_regions = noisy > 4
# Note that using `colorize` here is a bit more difficult, since `rgb2hsv`
# expects an RGB image (height x width x channel), but fancy-indexing returns
# a set of RGB pixels (# pixels x channel).
masked_image = image.copy()
masked_image[textured_regions, :] *= red_multiplier

fig, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(8, 4),
                sharex=True, sharey=True)
ax1.imshow(sliced_image)
ax2.imshow(masked_image)

plt.show()

######################################################################
# For coloring multiple regions, you may also be interested in
# `skimage.color.label2rgb http://scikit-
# image.org/docs/0.9.x/api/skimage.color.html#label2rgb`_.

到此這篇關(guān)于python庫skimage給灰度圖像染色的方法示例的文章就介紹到這了,更多相關(guān)python 灰度圖像染色內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python 調(diào)用 C++ 傳遞numpy 數(shù)據(jù)詳情

    Python 調(diào)用 C++ 傳遞numpy 數(shù)據(jù)詳情

    這篇文章主要介紹了Python 調(diào)用 C++ 傳遞numpy 數(shù)據(jù)詳情,文章主要分為兩部分,c++代碼和python代碼,代碼分享詳細(xì),需要的小伙伴可以參考一下,希望對(duì)你有所幫助
    2022-03-03
  • Python去除、替換字符串空格的處理方法

    Python去除、替換字符串空格的處理方法

    這篇文章主要介紹了Python去除、替換字符串空格的處理方法,去除字符串空格有兩種方法,一種是 .replace(' old ',' new '),第二種方法也很簡單,需要的朋友可以參考下
    2018-04-04
  • Python繪制地理圖表可視化神器pyecharts

    Python繪制地理圖表可視化神器pyecharts

    這篇文章主要介紹了Python繪制地理圖表可視化神器pyecharts,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • Python網(wǎng)絡(luò)編程詳解

    Python網(wǎng)絡(luò)編程詳解

    這篇文章主要介紹了Python網(wǎng)絡(luò)編程詳解,涉及具體代碼示例,還是挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。
    2017-10-10
  • python 三元運(yùn)算符使用解析

    python 三元運(yùn)算符使用解析

    這篇文章主要介紹了python 三元運(yùn)算符使用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • python使用Matplotlib繪制多種常見圖形

    python使用Matplotlib繪制多種常見圖形

    這篇文章主要介紹了python使用Matplotlib繪制多種常見圖形,文章圍繞主題展開詳細(xì)的用Matplotlib繪制內(nèi)容,需要的小伙伴可以參考一下
    2022-05-05
  • 快速進(jìn)修Python指南之函數(shù)進(jìn)階

    快速進(jìn)修Python指南之函數(shù)進(jìn)階

    這篇文章主要為大家介紹了Java開發(fā)者快速進(jìn)修Python指南之函數(shù)進(jìn)階示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Python實(shí)現(xiàn)批量修改指定目錄下圖片的大小

    Python實(shí)現(xiàn)批量修改指定目錄下圖片的大小

    批量修改指定目錄下圖片大小通常是在需要對(duì)大量圖片進(jìn)行統(tǒng)一處理的情況下使用的,本文主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)批量修改圖片大小,需要的可以參考下
    2023-10-10
  • 開源軟件包和環(huán)境管理系統(tǒng)Anaconda的安裝使用

    開源軟件包和環(huán)境管理系統(tǒng)Anaconda的安裝使用

    Anaconda是一個(gè)用于科學(xué)計(jì)算的Python發(fā)行版,支持 Linux, Mac, Windows系統(tǒng),提供了包管理與環(huán)境管理的功能,可以很方便地解決多版本python并存、切換以及各種第三方包安裝問題。
    2017-09-09
  • 使用scrapy實(shí)現(xiàn)爬網(wǎng)站例子和實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(蜘蛛)的步驟

    使用scrapy實(shí)現(xiàn)爬網(wǎng)站例子和實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(蜘蛛)的步驟

    本文分二個(gè)示例,第一個(gè)是個(gè)簡單的爬網(wǎng)站的小例子,第二個(gè)例子實(shí)現(xiàn)目是從一個(gè)網(wǎng)站的列表頁抓取文章列表,然后存入數(shù)據(jù)庫中,數(shù)據(jù)庫包括文章標(biāo)題、鏈接、時(shí)間,大家參考使用吧
    2014-01-01

最新評(píng)論

宣恩县| 海晏县| 平潭县| 鸡泽县| 阿瓦提县| 梨树县| 德化县| 金门县| 曲阳县| 海兴县| 九台市| 山西省| 津南区| 温宿县| 毕节市| 桑植县| 新泰市| 潍坊市| 巴南区| 聊城市| 桐柏县| 定兴县| 昭苏县| 江川县| 宿松县| 汽车| 陇西县| 临泽县| 察雅县| 荥阳市| 甘孜| 伽师县| 栖霞市| 明溪县| 甘泉县| 绵竹市| 萨迦县| 京山县| 镇沅| 郓城县| 彭阳县|