how can I efficiently pad a RGB pixel to make it the center pixel of a resulting image using numpy
Date : March 29 2020, 07:55 AM
With these it helps I have an image consisting of 100 pixels. for each pixel, I want to pad it with zeros (if on the edge) so that it's in the center, concatenate with neighboring pixels and generate a new 10x10 image. Thus I want to generate 100 images from the original image by sliding through each pixel along the row. e.g. for pixel[0,0], I want to add 4 zero columns on right, 4 zero rows on top, neighboring 5 column pixels on right and neighboring 5 row pixels on the bottom. from PIL import Image
import numpy as np, time
im_array = np.random.rand(10,10,3)
pad = 4
padded_array = np.pad(im_array, ((pad,pad+1),(pad,pad+1),(0,0)), 'constant')
for i in xrange(im_array.shape[0]):
for j in xrange(im_array.shape[1] ):
temp_im = padded_array[i:i+10, j:j+10]
# print temp_im.shape
if i == 0 and j == 0:
new_im = temp_im[np.newaxis,...]
else:
new_im = np.vstack([new_im, temp_im[np.newaxis,...]])
|
Why demosaic introduce pixel value greater than the max pixel in raw image?
Date : March 29 2020, 07:55 AM
may help you . Since demosaic internally performs so many filters; it's natural for the resultant pixel to be greater than the max input value. For some pixels you can have 5001, for others you can get 5023 etc which is quite normal. In MATLAB when you have a 8 bit data in 8-bit container, it automatically saturates all values greater than 255 to 255. Similarly for a 16-bit data in a 16 bit container it saturates the values to 65535. So it's the container/data type that determines to clip off value and not your data.
|
numpy.ndarray with shape (height, width, n) from n values per Image pixel
Date : March 29 2020, 07:55 AM
will be helpful for those in need For starters, you can convert an image directly to a numpy array and use vectorized operations to do what you want: def get_ycbcr_vectorized(img: Image.Image):
R,G,B = np.array(img).transpose(2,0,1)[:3] # ignore alpha if present
Y = 0.299 * R + 0.587 * G + 0.114 * B
Cb = 128 - 0.168736 * R - 0.331264 * G + 0.5 * B
Cr = 128 + 0.5 * R - 0.418688 * G - 0.081312 * B
return np.array([Y,Cb,Cr]).transpose(1,2,0)
print(np.array_equal(get_ycbcr_arr(img), get_ycbcr_vectorized(img))) # True
import matplotlib.pyplot as plt
def aux():
# generate every integer R/G/B combination
R,G,B = np.ogrid[:256,:256,:256]
Y = 0.299 * R + 0.587 * G + 0.114 * B
Cb = 128 - 0.168736 * R - 0.331264 * G + 0.5 * B
Cr = 128 + 0.5 * R - 0.418688 * G - 0.081312 * B
# plot the maximum error along one of the RGB channels
for arr,label in zip([Y,Cb,Cr], ['Y', 'Cb', 'Cr']):
plt.figure()
plt.imshow((arr - arr.round()).max(-1))
plt.xlabel('R')
plt.ylabel('G')
plt.title(f'max_B ({label} - {label}.round())')
plt.colorbar()
aux()
plt.show()
arr = np.array(img.convert('YCbCr'))
|
OpenCV: Normalizing pixel values of an image
Date : March 29 2020, 07:55 AM
|
Read an image pixel by pixel (ndimage/ndarray)
Date : March 29 2020, 07:55 AM
|