Extract one particular channel from a png using scikit-image - scikit-image

I'm evaluating scikit-image and looking for a way to read only green channel from my images.
The images are stored as RGBA having only green channel with values.
Is there something similar to CvSplit as found in OpenCV?

Images are stored as MxNxd arrays where, in your case, d == 4. So, use standard NumPy slicing to get hold of green:
green = image[..., 1]

Related

How to create a 1 pixel white .gif from code only

I'm new to programming, i won't become a developer but I like to learn a few stuff, since i'm a creative artist. I'm trying to solve an exercise I came up with.
I'm looking for guidelines to create a 1 pixel image from code. I want to try to create the most basic form of a white image, and I thought that a lossless 1 pixel gif of white (#FFFFFF or RGB 255, 255, 255) could be the simplest form, generating the smallest code possible.
Where would I even start?
I created two 1 pixel white .gifs, one in Paint and another in Photoshop, using 8bit Grayscale color mode. Gave both of them the same name, and when I hash them, both hashes are different, so they are not exactly the same. I also tried exporting from photoshop twice, to different directories, and this time hashes are the same.
My intention is to create the simplest and purest form of that white pixel.
Tips?
In Python, you can use PIL to create an image. Here is the code for what you requested.
from PIL import Image # import library
img = Image.new(mode = "RGB", size = (1,1)) # creates a RGB image with the size of 1x1
pixels = img.load() # Creates the pixel map
pixels[0,0] = (255,255,255) # Set the colour of the first (and only) pixel to white
img.save(format='GIF', fp='./test.gif')
# Saves the image as a GIF in a file called test.gif in your current directory
The purest, simplest form that is widely readable (GIMP, Photoshop, MS-Paint) is NetPBM IMHO.
Here is a single white pixel - no metadata, no copyright, no EXIF, no creation date, no palette, no checksum. You can create it with a text editor or code:
P1
1 1
0
Save it as white.pbm
If you want a simple, single, red pixel, save the following as red.ppm:
P3
1 1
255
255 0 0
The simplest Python to create a white single pixel GIF with PIL is probably:
from PIL import Image
Image.new('L', (1,1), 255).save('white.gif')
The simplest Python to create a red single pixel GIF with PIL is probably:
from PIL import Image
Image.new('RGB', (1,1), 'red').save('red.gif')

Extracting pixel values using Pillow - every coordinate I use gives the same value

I am attempting to use the Pillow library in Python 2.7 to extract pixel values at given coordinates on PNG and JPG images. I don't get an errors but I always get the same value irrespective of the coordinates I have used on an image where the values do vary.
This is an extract from my script where I print all the values (its a small test image):
from PIL import Image
box = Image.open("col_grad.jpg")
pixels = list(box.getdata())
print(pixels)
And from when I try to extract a single value:
from PIL import Image, ImageFilter
box = Image.open("col_grad.jpg")
value = box.load()
print(value[10,10])
I have been using these previous questions on this topic for guidance including:
Getting list of pixel values from PIL
How can I read the RGB value of a given pixel in Python?
Thanks for any help with this,
Alex
I'm not sure if you can access it the way you want, because of the complexity of images data.
Just get the pixel:
Image.getpixel(xy)
Returns the pixel value at a given position.
Parameters: xy – The coordinate, given as (x, y).
Returns: The pixel value. If the image is a multi-layer image, this method returns a tuple.:
You should consider doing one of the following:
FIRST convert your image to grayscale and then find the pixel values present.
img_grey= img.convert('1') # convert image to black and white
SECOND If you want the pixel values of RGB channels, you have to split your color image. Then find the pixel values in all the channels at a particular coordinate.
Image.split(img)

Fastest way to check if an image contain any (semi)transparent pixels?

Does anyone know is there a better way to check if some image contain a (semi)transparent pixel beside going trough all pixels and check their alpha channel?
[pseudo]
for each pixel in image:
if pixel.alpha != 0xff:
return true
Thanks in advance.
You could use BufferedImage.getType()
or
ColorModel.hasAlpha()
to check if there is an alpha channel.
If there is an alpha channel, you will have to check the individual pixels.
yes there is a better way than simply iterating all pixels. if you already have a mip-map stored for the alpha channels you can check from top to bottom for any non-opaque pixels.
JAI supports these: put the alpha channel or the whole image into a javax.media.jai.ImageMIPMap then iterate its levels from top to bottom using getImage(int level)
some keywords for googling: gauss-laplace image pyramids, mipmaps

disparity map creation opencv

I have 2 image one is left the other is right image. I want to implement disparity mapping. There are some questions in my mind.
Firstlty dispartiy image is 1 channel gray image but the left and right images are 3 channels bgr images. If we convert these 3 channels image to gray image, do i loss information? If yes, how can i get disparity image from 3 channels img to 1 channel disparity image. I mean how can demote to 3 channels image to 1 channels image to get disparity image?
Thanks for answers..
Yes, you are losing information by combining all three channels into a single channel grayscale image.
There are certainly algorithms that use 3 channels to compute a disparity map, but opencv does not contain functions that implement these algorithms.
Have you considered taking out each channel R, G and B and computing the correspondence for each seperately? Of course, you would need to figure out a clever way to combine the results into one final disparity map, but it could be fun!
Try first to find the corresponding pixels, then apply SAD difference on the two matrices, then find the minimum value and display it at the disparity map.

I need to zero out one of the color channels in an NSImage, whats the right way to do this?

I want to achieve the same result as if I were in photoshop and turned off one of the channels. I was about to try to loop through every pixel changing colors. Is there a better way to do this?
Use Core Image's Color Matrix filter. The array of vectors can be bewildering, but it's very powerful. In your case, you'll want to set the vector for the channel you want to turn off to all-zeroes.
Obviously, this will only work for RGB images, since Core Image only works for RGB images. You can make it work for gray images (turn off R, G, and B to turn off the K channel), but not for CMYK.

Resources