Convert grayscale png to RGB png image - python

I have a dataset of medical images in grayscale Png format which must be converted to RGB format. Tried many solutions but in vain.

GIMP, Menu image -> Mode -> RGB mode

If you want to just convert the format, the following method will help you:
In python3, using PILLOW and Numpy:
From PIL import Image
import numpy as np
im = Image.open(path/to/image, 'r').convert('L')
im = np.stack((im,)*3, axis=-1)
im = Image.fromarray(im)
im.save(path/to/save)
But if you want to colorize the image, know that colorization is an well-known image translation problem. Even if multiple approachs exist depending on the domain, I don't know any method that colorize any kind of images.
Some ways of doing so is to train a neural network, but for that, you need to have a dataset of B/W and colored images. Here are some approaches:
Using CNNs and considering the colorization as a regression problem: Let there be Color!
Using CNNs and considering the colorization as a classification problem: Colorful Image Colorization
Using GANs : cycle-gan

Related

Is there a way to efficiently (realtime) convert numpy arrays into bytes in Python?

I am looking for an efficient way to convert multiple numpy arrays (images) into bytes so I can display them into a GUI, in my case imgui from https://github.com/pyimgui/pyimgui.
The way I'm doing this seems a bit counterintuitive, since I am getting the images from neural networks and I need to transform frame by frame to display in the rendering engine. The pipeline is:
get z vector ->
generate image data from the z vector ->
convert the image data to PIL image ->
.convert("RGB") the PIL image ->
get the PIL image in bytes using : data = im.tobytes("raw", "RGBA", 0, -1)
This seems extremely inefficient to me and I am doing this for 5 textures at the same time (from two different neural networks).
Even when I try to display, instead of bytes, either the PIL image or even the numpy array directly in the OpenGL context I only see a glitch.
Any help is appreciated.

Image display issue, Neural Style Transfer

I am attempting to read in my own images in python following the tensorflow Neural Style Transfer tutorial and when displaying them they look nothing like the original image. Can someone please explain why this is? Is it because of the tensorflow preprocessing? Ultimately my issue with Neural Style Transfer is the style never changes (whole code here). I'm always getting a rainbow style applied, no matter what the style image is, and I'm wondering if this is the issue?
Simple code of how I'm reading in images and displaying for debugging:
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import cv2
style_image = tf.keras.preprocessing.image.img_to_array(Image.open('blackGold.jpg'))
plt.imshow(style_image)
plt.show()
cv2.imshow("img", style_image)
Original Image:
Displaying with matplotlib:
Displaying with opencv:
Neural Style Transfer results:
I appreciate any input, thanks!
Normalize the image, divide the values of your input image, which usually range from 0-255, by 255 so the images are in range of 0-1

How to test an embedder for face Recognition

Im trying to see the output after embedding an image. I tried using an opencv image function and ask to print the result, but it is not working. Any suggestions on how to test embedding using dlib?
Step1: Get the position of each image in the embedding space.
Step2: Visualize it. May by the dimension is higher than 2D/3D you can use some methods like t-SNE to do the visualization.

perform Image cropping on Image like data using Python

I have image-like data. And I wont to perform Image cropping, squishing and zooming, on one or both axis. The problem is that the data is not in between 0-255, and normalizing it to 0-255, would mean loosing a lot of the information I want to preserve. So unfortunately I can’t use PIL or cv2. Is there a easy way to do it with numpy or scipy?
Thanks for the help
You can crop pictures with simple indexing, like
picture[100:300, 400:800]
To squish or zoom (is that anything more than resizing?), you can just resize with skimage:
from skimage import data, color
from skimage.transform import resize
image = color.rgb2gray(data.astronaut())
image_resized = resize(image, (image.shape[0] // 4, image.shape[1] // 4),
anti_aliasing=True)
Check ImageChops function of Image Librart

Extract numbers and letters features from an image

I'm writting a python program to classify letters and numbers. I've wrote the classifier and I have the images for my dataset. I really don't have much experience in python or working with images.
My problem is how to create my dataset with the images I have. How to create like an array with the shape of them. Should I just create a numpy array of each image? Or use color histogram?
I will probably convert all images to grayscale.
I've found the link bellow that classifies cats and dogs. It uses two method to extract images features but I don't know if this would apply for my case.
k-nn-classifier-for-image-classification
Could anyone guide me could I extract the features of my images to a vector, for example, so I can write this data in my "dataset.data" file?
I'll use images like the image bellow:
Letter "e"
I've even considered resizing the image to 32x32 and create like a bitmap of 0's and 1's representing the image.
Could anyone guide me could I extract the features of my images to a vector, for example, so I can write this data in my "dataset.data" file?
Thank you.
You would usually want to create a Numpy array to hold all your training data. It is common to arrange it in the following shape:
X_train.shape = (N, img.shape[0], img.shape[1])
where N is the number of images in the set.
This way, if you are using single channel (gray scale), X_train[i,:,:] will hold the values of the i'th image pixels. Note that it's recommended to normalize these values, but this will depend on the model you choose to train.
Here is a quick example of how you can create build such an array:
import numpy as np
import cv2
X = np.zeros((N, IMG_SIZE[0], IMG_SIZE[1]), dtype=np.float32)
y = np.zeros((N))
for idx, img_path in enumerate(images_path):
img = cv2.imread(img_path)
assert ((img.shape[0], img.shape[1]) == IMG_SIZE)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
X[idx, :, :] = gray
y[idx] = # label of this image
# if you wish to normalize:
X = (X/255.0) - 0.5
There are many tutorials for digit classifiers out there, usually using the MNIST dataset as an example. Here is one example but you should go ahead and google it.
If you want to achieve better results, you would probably want to look into neural networks. Again, many tutorials out there, here is one example using tensorflow.
I think you might be looking for this:
http://www.scipy-lectures.org/advanced/scikit-learn/
Sklearn is a very easy to learn machine learning package, with lots of tutorials.
Hope it helps,

Categories

Resources