Hey i am trying to resize an image without stretching it but adding white pixels instead. I looked arround but i found nothing specifying how that can be achieved from within skimage. So instead i used numpy to add the extra pixels before the resize as arrays of [float(255)].
from skimage.io import imread
from skimage.io import imsave
from skimage.transform import resize
from matplotlib import pyplot as plt
from pylab import cm
import numpy as np
from skimage import morphology
from skimage import measure
from scipy import misc
def process(file_):
im = imread(file_, as_grey=True)
#im = misc.imread(file_)
#im=np.fromfile(file_, dtype=np.int64)
#Filler to avoid stretching
orig_rows, orig_cols = im.shape
print orig_rows, orig_cols
if orig_rows < orig_cols:
for addition in range(0,orig_cols-orig_rows):
#adding white rows
lst = np.array(list(float(255) for x in range(0,orig_cols)))
im= np.vstack((im,lst))
if orig_rows > orig_cols:
for addition in range(0,orig_rows-orig_cols):
#adding white columns
lst = np.array(list([float(255)] for x in range(0,orig_rows)))
im= np.hstack((im,lst))
image = resize(im, (48, 48))
imsave('test.jpg',im)
imsave('test1.jpg',image)
plt.imshow(im, cmap=cm.gray)
plt.show()
When i view the image with pyplot it looks like this
We can see that the columns have been added, but after i save the image with
image = resize(im, (48, 48))
imsave('test.jpg',im)
imsave('test1.jpg',image)
The the images look like negatives, and the resized image looks completely white(next to the dark its invisible on the sites background). Any ideas?
The code below should work. Note that the padded areas' color is not exactly white in order to see the image boundaries in the uploaded image. For white padding set fill_cval = np.max(img).
def resize_padded(img, new_shape, fill_cval=None, order=1):
import numpy as np
from skimage.transform import resize
if fill_cval is None:
fill_cval = np.max(img)
ratio = np.min([n / i for n, i in zip(new_shape, img.shape)])
interm_shape = np.rint([s * ratio for s in img.shape]).astype(np.int)
interm_img = resize(img, interm_shape, order=order, cval=fill_cval)
new_img = np.empty(new_shape, dtype=interm_img.dtype)
new_img.fill(fill_cval)
pad = [(n - s) >> 1 for n, s in zip(new_shape, interm_shape)]
new_img[[slice(p, -p, None) if 0 != p else slice(None, None, None)
for p in pad]] = interm_img
return new_img
import numpy as np
import matplotlib.pylab as plt
from skimage.data import astronaut
from skimage.color import rgb2gray # using luminance
from skimage.io import imsave
img = rgb2gray(astronaut())
# set desired image size
out_size = (30, 100) # height, width
# set the color of the padded area. Here: "95% luminance"
fill_cval = np.max(img) * 0.95
resized_img = resize_padded(img, out_size, fill_cval=fill_cval)
imsave('img.png', img)
imsave('img_res.png', resized_img)
Related
iam a beginner at python and image processing etc. I want to plot this white pixel. as i know the pixel color identifier for black is 0 and for white is 255. here's the image that i want to plot:
The Image
i try to print out the image ndarray with these following command:
#importing module
import cv2
import numpy as np
import matplotlib.pyplot as plt
import sys
#load image
img = cv2.imread('thin.png')
#image to nd.array
arr0 = np.array(img)
#finding white pixel
arr1 = np.where(arr0 == 255)
#indexing tuple, the printout arr1 is tuple with dtype=int64
tuple1 = arr1[0]
tuple2 = arr1[1]
tuple3 = arr1[2]
#defining x and y axis
x = np.array(tuple1)
y = np.array(tuple2)
z = np.array(tuple3)
plot = plt.plot(x,y)
plt.show()
this is what i get..
output image
i think it's very noisy but i dont have a clue. Thank you very much for help
I think there is some confusion in the dimensions of you array arr0. I think you should look at the indices of the white pixels:
import cv2
import numpy as np
import matplotlib.pyplot as plt
#load image
img = cv2.imread('thin.png')
#image to nd.array
arr0 = np.array(img)
height, width, _ = arr0.shape
x = range(width)
y = [height - np.argwhere(arr0[:, i, 0]==255).mean() for i in x] # "height -" to reverse the y-axis
plt.plot(x,y)
plt.show()
Note: taking the mean because a vertical line can have more than one white pixel (also some won't have any, see picture below)
Output:
I am trying to remove the black spots from a face of this image using the erosion methods.
I have implemented:
img = skimage.io.imread('blemish.jpeg')
img = skimage.color.rgb2gray(img)
img_inten = skimage.exposure.rescale_intensity(img,in_range=(50,100))
diliation_seed = img_inten.copy()
diliation_seed[1:-1,1:-1] = img_inten.min()
mask = img_inten
eroded_img = skimage.morphology.reconstruction(diliation_seed,mask,method='dilation')
matplotlib.pyplot.imshow(eroded_img,cmap='gray')
My output is always a black image in both the cases. What is going wrong here?
rgb2gray is outputting an image as a matrix of floats, with values in [0;1]
So the rescale_intensity is just outputting a matrix of 0, since you ask for values between 50 and 100 and there is none in the gray img.
you can fix it like this :
import skimage
from skimage import data, exposure, img_as_float
from skimage.morphology import reconstruction
import matplotlib.pyplot as plt
img = skimage.io.imread('blemish.jpeg')
gray_img = 255*skimage.color.rgb2gray(img) # multiply by 255 to get back in the [0;255] range
img_inten = exposure.rescale_intensity(gray_img,in_range=(50,100))
diliation_seed = img_inten.copy()
diliation_seed[1:-1,1:-1] = img_inten.min()
mask = img_inten
eroded_img = reconstruction(diliation_seed,mask,method='dilation')
plt.imshow(eroded_img,cmap='gray')
plt.show()
Is it possible to rotate an image and keep the true values? When I rotate a black and white image I get back grey values. Can I rotate without averaging the pixel values? I can almost do it manually by using np.where() but it gets difficult when there are more than 2 pixel values.
Code:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from scipy import ndimage
import cv2
filename = 'rot square.png'
img = cv2.imread('square test.png')
img_rot = ndimage.rotate(img, 10, reshape = False)
cv2.imwrite(filename, img_rot)
Original Image
Rotated Image
Averaged Values
True Values
Here:
from PIL import Image
img = Image.open('original.png')
rotated = img.rotate(45)
rotated.save('rotated.png')
I am trying to implement dimming method to dim a image by converting pixel values and using this equation X(new) = 0.5 * X(old)^2. I did some googling and but could not succeed. Here is my code:
import math
from PIL import Image
import numpy as np
from skimage import color, io
import matplotlib.pyplot as plt
def load(image_path):
out = plt.imread(image_path)
out = out.astype(np.float64) / 255
return out
def dim_image(image):
out = image.point(lambda x: x*0.5)
return out
def display(img):
# Show image
plt.figure(figsize = (5,5))
plt.imshow(img)
plt.axis('off')
plt.show()
image1 = load(image1_path)
image2 = load(image2_path)
display(image1)
display(image2)
new_image = dim_image(image1)
display(new_image)
You are trying to use .point on a numpy array, which doesn't exist. I assume you meant to reduce all color values by 50% to dim the image, in which case you should use np.dot (docs):
def dim_image(image):
out = np.dot(image, 0.5)
return out
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
import Image
from scipy import ndimage
import Image, ImageDraw
import PIL
import cv
import cv2
from scipy.ndimage import measurements, morphology
from PIL import Image
from numpy import *
from scipy.ndimage import filters
import pylab
img = np.asarray(Image.open('test.tif').convert('L')) #read and convert image
img = 1 * (img < 127) # threshold
plt.imshow(img, cmap=cm.Greys_r) # show as black and white
plt.show()
Code above gives white pixels on black background, how to compute white region on image, then to split image into 100 rectangles and find rectangles with min, max and average numbers of pixels? thanks
Since your image is binary you can just sum the values to get a count of the white pixels.
from PIL import Image
import numpy as np
img = np.asarray(Image.open("foo.jpg").convert('L'))
img = 1 * (img < 127)
m,n = img.shape
# use np.sum to count white pixels
print("{} white pixels, out of {} pixels in total.".format(img.sum(), m*n))
# use slicing to count any sub part, for example from rows 300-320 and columns 400-440
print("{} white pixels in rectangle.".format(img[300:320,400:440].sum()))
Use slicing to pick out any rectangle and use sum() on that part.