Skimage Python33 Canny - python-3.x

Long story short, I'm just simply trying to get a canny edged image of image.jpg.
The documentation is very spotty so I'm getting very confused. If anyone can help that'd be greatly appreciated.
from scipy import misc
import numpy as np
from skimage import data
from skimage import feature
from skimage import io
im=misc.imread('image1.jpg')
edges1 = feature.canny(im)
...
And I'm getting this error
ValueError: The parameter `image` must be a 2-dimensional array
Can anyone explain how to create a 2D array from an image file?
Thanks!

I suspect image1.jpg is a color image, so im is 3D, with shape (num_rows, num_cols, num_color_channels). One option is to tell imread to flatten the image into a 2D array by giving it the argument flatten=True:
im = misc.imread('image1.jpg', flatten=True)
Or you could apply canny to just one of the color channels, e.g.
im = misc.imread('image1.jpg')
red_edges = feature.canny(im[:, :, 0])

The canny edge detection needs a grayscale image input in order to work.
You can convert 3D (color) images to 2D (grayscale) using the rgb2gray module in scikit-image.
from skimage import io, features
from skimage.color import rgb2gray
image = rgb2gray(io.imread("image.png"))
edges = feature.canny(image)

Related

In image dataset loading into numpy array, How can I convert image's 3d format into 2d?

To load my image dataset, I have done following coding
X=[]
for i in range(1,682):
image=Image.open(str(i)+'.jpg')
image=image.resize((100,100))
temp=asarray(image)
X.append(temp)
Shape of X is (681,100,100,3) but I want shape of X to be (681,100,100). How can I do that?
You can use opencv to read images it reads images as numpy arrays
import cv2
X=[]
for i in range(1,682):
temp = cv2.imread(str(i)+'.jpg', cv2.IMREAD_GRAYSCALE)
temp = cv2.resize(temp, (100,100))
X.append(temp)

how to show binary image data in python?

i can show the image using image.open, but how do i display from the binary data?
trying to use plot gets: ValueError: x and y can be no greater than 2-D, but have shapes (64,) and (64, 64, 3). this makes sense as that is what the result is supposed to be, but how do i display it?
import pathlib
import glob
from os.path import join
import matplotlib.pyplot as plt
from PIL import Image
import tensorflow as tf
def parse(image): # my like ings, but with .png instead of .jpeg.
image_string = tf.io.read_file(image)
image = tf.image.decode_png(image_string, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize(image, [64, 64])
return image
root = "in/flower_photos/tulips"
path = join(root,"*.jpg")
files = sorted(glob.glob(path))
file=files[0]
image = Image.open(file)
image.show()
binary=parse(file)
print(type(binary))
# how do i see this?
#plt.plot(binary) # does not seem to work
#plt.show() # does not seem to work
found a nice pillow tutorial.
from matplotlib import image
from matplotlib import pyplot
from PIL import Image
# load the image
filename='Sydney-Opera-House.jpg'
im = Image.open(filename)
# summarize some details about the image
print(im.format)
print(im.mode)
print(im.size)
# show the image
#image.show()
# load image as pixel array
data = image.imread(filename)
# summarize shape of the pixel array
print(data.dtype)
print(data.shape)
# display the array of pixels as an image
pyplot.imshow(data)
pyplot.show()

Effects of channel_shift_range in ImageDataGenerator (Keras image augmentation)

Maybe I'm misunderstanding. If I implement channel_shift_range in my ImageDataGenerator, the output should have "scrambled" color values, right? I would like to use it to make my model more robust to variance in color.
However, when I test it, I'm not seeing any effect. Am I using it wrong? Here's my code:
from keras.preprocessing.image import ImageDataGenerator
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
path = '/mnt/Project/Imaging/samples'
datagen = ImageDataGenerator(channel_shift_range=0.9)
genObject = datagen.flow_from_directory(path,
batch_size=1)
augs = []
i = 0
for batch in genObject:
augs.append(batch)
i += 1
if i > 10:
break
for item in augs:
plt.imshow(item[0][0].astype('uint8'))
plt.show()
Environment:
Jupyter Lab
Python 3.6.6
Keras==2.2.4
Keras-Applications==1.0.7
Keras-Preprocessing==1.0.9
tensorboard==1.9.0
tensorflow-gpu==1.9.0
Thanks in advance for the help!
The values of the image data are in the range [0..255], so a shift of 0.. 0.9 is hardly visible. Try a much large shift to see any effect.
Note that using rescale=1./255. does not help as the rescaling is applied after the transformation.

How to calculate and sort RGB data on OpenCV?

RGB data. How to calculate and sort them on Python, OpenCV
I want to work on Python, OpenCV these below steps
1. Get the RGB data from pictures
2. Calculate the R*G*B on each pixel of the pictures
3. Sort the data by descending order and plot them on graph or csv
4. Get the max and min and medium of R*G*B
I could handle that the step1. as below code.
However, I don’t know how to write a program after step2
It's better to save the data as csv or numpy
Does anybody have an idea? Please help me. it would be very helpful if you show me the code.
import cv2
import numpy
im_f = np.array(Image.open('data/image.jpg'), 'f')
print(im[:, :])
It is better to keep data in-memory as numpy array. Also, read image using cv2.imread rather than Image.open if it has to be converted to np.array eventually.
For plotting, matplotlib can be used.
Here is how the above mentioned process can be achieved using OpenCV, numpy and matplotlib.
import numpy as np
import cv2, sys
import matplotlib.pyplot as plt
#Read image
im_f = cv2.imread('data/image.jpg')
#Validate image
if im_f is None:
print('Image Not Found')
sys.exit();
#Cast to float type to hold the results
im_f = im_f.astype(np.float32)
#Compute the product of channels and flatten the result to get 1D array
product = (im_f[:,:,0] * im_f[:,:,1] * im_f[:,:,2]).flatten()
#Sort the flattened array and flip it to get elements in descending order
product = np.sort(product)[::-1]
#Compute the min, max and median of product
pmin, pmax , pmed = np.amin(product), np.amax(product), np.median(product)
print('Min = ' + str(pmin))
print('Max = ' + str(pmax))
print('Med = ' + str(pmed))
#Show the sorted array
plt.plot(product)
plt.show()
Tested with Python 3.5.2, OpenCV 4.0.1, numpy 1.15.4, and matplotlib 3.0.2 on Ubuntu 16.04.

How to convert binary image to RGB with PIL?

I have PIL Image in binary and I need to convert it in RGB. I did this diskew image
binary image
I need this way:
I already tried this which is not working
from PIL import Image as im
img = im.fromarray((255 * Image).astype("uint8")).convert("RGB")
I still don't understand why you convert to RGBA if you want RGB, but this code converts your image to RGB as you ask:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Open input image
im = Image.open('text.png').convert('RGB')
# Invert
npim = 255 - np.array(im)
# Save
Image.fromarray(npim).save('result.png')

Resources