I have been trying to stack two images.
The end result will be used as the input to my convolutional neural network.
Now I tried to use dstack, I also tried to use PIL by importing Image.blend but I cannot seem to be arriving to my desired result.
I am asking if anyone has any other ideas which I can use would be greatly appreciated.
This could help you out.
from PIL import Image
image1 = Image.open("img1.jpg")
image2 = Image.open("img2.jpg")
image1 = image1.resize((224, 224))
image1_size = image1.size
image2_size = image2.size
new_image = Image.new('RGB',(2*image1_size[0], image1_size[1]), (250,250,250))
Resize them so that they are the same size, and then use np.stack with axis=3 (if you are using multi-channel images. Else, use axis=2.
Or are you trying to combine them into one image? If so, how? Masking, adding subtracting?
Related
I have a bunch of images that have different exposures and I want to stitch them together:
OpenCV has a Stitcher example but it relies on matching features between the images and they should overlap with each other.
My images are not overlapping BUT they are connected to each other contiguously (as if I partitioned a Panorama into multiple splits) and I want to connect them together and fix their exposures.
How can I do this in OpenCV Python? At the very least the Compensator class needs:
compensator.feed(corners=corners, images=images_warped, masks=masks_warped)
images_warped could just be normal input images and masks_warped could be a 2D array of Ones but I don't know how the corners value should be given manually?
a simple example would suffice, Thanks.
Individual images
With the assumption that each of your images have the same dimensions and since OpenCV images are Numpy arrays, we can use np.concatenate, np.hstack, or cv2.hconcat.
import cv2
import numpy as np
# Load images
image1 = cv2.imread('1.png')
image2 = cv2.imread('2.png')
image3 = cv2.imread('3.png')
# Method #1
hstack = np.hstack((image1, image2, image3))
# Method #2
concatenate = np.concatenate((image1, image2, image3), axis=1)
# Method #3
hconcat = cv2.hconcat([image1, image2, image3])
cv2.imshow('hstack', hstack)
cv2.imshow('concatenate', concatenate)
cv2.imshow('hconcat', hconcat)
cv2.waitKey()
The result should be the same
I would create an overlapping section by duplicating on each picture a small part of the adjacent picture.
Then there is a bit of work to process that as a normal stitching process.
I have grayscale images of different dimensions so I need to convert them to same dimension (say, 28*28) for my experiments. I tried to do it using different methods and I was able to do it but I observed that resizing of image lead to increase in number of channels. I am new to python and image processing so please help.
from PIL import Image
image = Image.open('6.tif')
image = image.resize((28, 28), Image.ANTIALIAS)
image.save('6.png', 'PNG', quality=100)
And then following code shows different dimensions:
import imageio
image_data = imageio.imread("6.tif").astype(float)
print(image_data.shape)
image_data = imageio.imread("6.png").astype(float)
print(image_data.shape)
and result is:
(65, 74)
(28, 28, 4)
I don't need the last dimension. How is this coming? I get the similar results even with "from resizeimage import resizeimage".
There are a number of issues with your code...
If you are expecting a greyscale image, make sure that is what you get. So, change this:
image = Image.open('6.tif')
to:
image = Image.open('6.tif').convert('L')
When you resize an image, you need to use one of the correct resampling methods:
PIL.Image.NEAREST
PIL.Image.BOX
PIL.Image.BILINEAR
PIL.Image.HAMMING
PIL.Image.BICUBIC
PIL.Image.LANCZOS
So, you need to replace the ANTI_ALIAS with something from the above list on this line:
image = image.resize((28, 28), Image.ANTIALIAS)
When you save as PNG, it is always loss-less. The quality factor does not work the same as for JPEG images, so you should omit it unless you have a good understanding of how it affects the PNG encoder.
If you make these changes, specifically the first, I think your problem will go away. Bear in mind though that the PNG encoder may take an RGB image and save it as a palletised image, or it may take a greyscale image and encode it as RGB, or RGB alpha.
I'm a newbie with Python, I wonder if there is a way to resize an array which contains an rgb image.
imageA_array[height1][width1][3]
convert to
imageB_array[height2][width2][3]
imageB will result in a smaller image than imageA (height2
Thanks in advance.
The easiest method would be to use the scikit-image library available at http://scikit-image.org/ for python
Then all you would have to do is:
from skimage.transform import resize
imageB_array = resize(imageA_array, (height2, width2), anti_aliasing=True)
enter image description here
I am new to the this image processing stuff. Why I am asking this question is because I have a code which works for RGB mode but doesnt for P mode ?
So I came to conclusion that it is something related to modes. I did some basic research on modes.but did not find any simple explanation. Will be helpful if someone can help me understand this.
CODE:
image=Image.open('image.png')
image.load()
image_data = np.asarray(image)
image_data_bw = image_data.max(axis=2)
non_empty_columns = np.where(image_data_bw.max(axis=0)>0)[0]
non_empty_rows = np.where(image_data_bw.max(axis=1)>0)[0]
cropBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))
image_data_new = image_data[cropBox[0]:cropBox[1]+1, cropBox[2]:cropBox[3]+1 , :]
new_image = Image.fromarray(image_data_new)
new_image.save('cropped_image.png')
Codesource
Input to the code following Image:
Output should be like the following image(It is cropped to the edges of the picture. Please click on the image for understanding):
This Image is in RGBA mode.so the code is working fine for such images. But not with the image in P mode.
ERROR:
Error I get with P mode:
axis 2 is out of bounds for array of dimension 2
The answer you found greatly overcomplicates the process, by using numpy. The PIL library supports this usecase natively, with the image.getbbox() and image.crop() methods:
cropbox = image.getbbox()
new_image = image.crop(cropbox)
This works for all the different modes, regardless. The cropbox produced by image.getbbox() is exactly the same size as the one produced by the numpy route.
from PIL import Image
img = Image.open('Image.png')
print(x,y)
img.show()
cropbox_1 = img.getbbox()
new_image_1 = img.crop(cropbox_1)
new_image_1.save('Cropped_image,png')
new_image_1.show()
This code completely crops the image to the edges. Only if the images are having alpha channel, you might have to remove that channel by converting it.
ex. If it is a RGBA mode make it RGB and then use getbbox().
img = image.convert('RGB')
cropbox = img.getbbox()
image_1 = img.crop(cropbox)
addition of this should do the task.
I am having issue in feeding my own image into LeNet using Caffe library. I have deployed and initialised the weight obtained through training with no difficulties. As the net is trained using input size of 28x28, I tried resizing the input image to 28x28 and feed into the deployed LeNet, but it gave me "unhashable numpy array" error.
Not only that, I also tried to transpose it with img = img.transpose(img, (2,0,1)) after resizing but it gave me "TypeError: only length-1 arrays can be converted to Python scalars"
Below are the python codes I tried so far in pre-processing my image :
img = caffe.io.load_image('number5.png')
img = caffe.io.resize_image(img, (28,28), interp_order=3)
img = img.transpose(img, (2,0,1))
I am a beginner in using Caffe and still in the process of learning. I hope someone can give me some example or insights in how to pre-process an image before feeding into the net.
Thank You.
Best regards.
Just use this instead:
img = img.transpose((2,0,1))
You can use caffe.io.Transformer
This is used to preprocess caffe 'data' blob.
Define it as
transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
transformer.set_transpose('data',(2,0,1))
then you can,
img = caffe.io.load_image('number5.png')
img = caffe.io.resize_image(img, (28,28), interp_order=3)
img_transposed=transformer.preprocess('data',img)