Skip to content

Instantly share code, notes, and snippets.

@clngn
Created March 6, 2012 07:18
Show Gist options
  • Save clngn/1984544 to your computer and use it in GitHub Desktop.
Save clngn/1984544 to your computer and use it in GitHub Desktop.
画素操作におけるスライスとfor文
# -*- coding: utf-8 -*-
import cv2
import numpy
src = cv2.imread('azu.jpg', 1)
dst = numpy.zeros(src.shape, dtype=numpy.uint8)
dst = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
dst1 = numpy.copy(dst)
dst2 = numpy.copy(dst)
'''
明度を倍に
越えたら明度最大で
'''
# スライスを使った場合
buf = numpy.zeros(src.shape[:2], dtype=numpy.float64)
buf[:,:] = dst1[:,:,2]
buf[:,:] = buf[:,:] * 2
buf[numpy.where(buf > 255)] = 255
dst1[:,:,2] = buf[:,:]
dst1 = cv2.cvtColor(dst1, cv2.COLOR_HSV2BGR)
# forを使った場合
for y in range(dst2.shape[0]):
for x in range(dst2.shape[1]):
v = dst2[y,x,2]
v = v * 2
if v > 255:
v = 255
dst2[y,x,2] = v
dst2 = cv2.cvtColor(dst2, cv2.COLOR_HSV2BGR)
cv2.imshow('src', src)
cv2.cv.MoveWindow('src', 0, 0)
cv2.imshow('dst1', dst1)
cv2.cv.MoveWindow('dst1', 320, 0)
cv2.imshow('dst2', dst2)
cv2.cv.MoveWindow('dst2', 640, 0)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment