简体   繁体   English

图像的随机翻转和RGB抖动/轻微变化?

[英]Random flipping and RGB jittering/slight value change of image?

I want to implement a small program which will randomly flip and introduce RGB jitter/slight value change. 我想实现一个小型程序,该程序将随机翻转并引入RGB抖动/轻微值变化。

And if possible to limit the jitter/slight value change to 2 of the 3 layers in the color image. 并尽可能将抖动/轻微值更改为彩色图像中3层中的2层。

import cv2
import random

probofflip=0.5
probofRGBjit= 0.6

img=cv2.imread('path/to/img.png',1)
if (random.uniform(0,1)>1-probofflip):
    img= cv2.flip(img,1)
if if (random.uniform(0,1)>1-probofRGBjit):
    #function to jitter the RGB layers 
#do something with resultant image.

If you are using cv2, then numpy is quite helpful for this kind of operation. 如果您使用的是cv2,那么numpy对于此类操作非常有帮助。 By jitter, do you mean shifting some pixels around? 抖动是指移动一些像素吗? This example only deals with slight value change. 本示例仅处理微小的价值变化。

import cv2
import numpy as np
from pylab import *

img = cv2.imread( r'C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg' )
img = cv2.cvtColor(img, cv2.cv.CV_BGR2RGB)  # cv2 defaul color code is BGR
h,w,c = img.shape # (768, 1024, 3)

noise = np.random.randint(0,50,(h, w)) # design jitter/noise here
zitter = np.zeros_like(img)
zitter[:,:,1] = noise  

noise_added = cv2.add(img, zitter)
combined = np.vstack((img[:h/2,:,:], noise_added[h/2:,:,:]))

imshow(combined, interpolation='none')

在此处输入图片说明

If you want to shift each color channel by some pixels, then you can use np.roll. 如果要将每个颜色通道移动一些像素,则可以使用np.roll。 ex: 例如:

# shift each channel by 10 pixels
R = img[:,:,0]
G = img[:,:,1]
B = img[:,:,2]
RGBshifted = np.dstack( (
    np.roll(R, 10, axis=0), 
    np.roll(G, 10, axis=1), 
    np.roll(B, -10, axis=0)
    ))
imshow(RGBshifted)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM