5.6 Images
Accessing Individual Image in an Image Stack
import originpro as op
import numpy as np
from skimage.util import invert
#load image stack
fn = op.path('e') + r'Samples\Image Processing and Analysis\*.tif'
iw=op.new_image()
iw.from_file(fn)
print(iw.frames)
#get the 3rd image
im2 = iw.to_np2d(2)
im2 *= 2
im2 = np.invert(im2)
#put it back into 2nd image
iw.from_np2d(im2, 1)
#show thumbnails
iw.set_int('NAV',1)
iw.set_str('Palette', 'Fire')
Image Split and Merge Channels
import originpro as op
fn = op.path('e') + r'Samples\Image Processing and Analysis\Leaves.jpg'
iw=op.new_image()
iw.from_file(fn)
iw.split()
g = iw.to_np2d(1)
g *= 0
iw.from_np2d(g, 1)
iw.merge()
iw.lname='Green is removed'
Image Window Basics
import originpro as op
#single image
fn = op.path('e') + r'Samples\Image Processing and Analysis\car.bmp'
iw=op.new_image()
iw.from_file(fn)
print(f'channels {iw.channels}, size {iw.size}, type {iw.type}')
iw.rgb2gray()
print(f'after convert to grayscale, channels {iw.channels}, frames {iw.frames}')
#load multiple as image stack
fn2 = op.path('e') + r'Samples\Image Processing and Analysis\*.tif'
iw2=op.new_image()
iw2.from_file(fn2)
print(f'channels {iw2.channels}, size {iw2.size}, type {iw2.type}, frames {iw2.frames}')
#show image thumbnails(1), 2=play control, 3=slider
if iw2.type > 1:
iw2.set_int('NAV', 1)
Image thinning with opencv
#recommand to install "opencv-python-headless"
import cv2
import numpy as np
import originpro as op
fn = op.path('e') + r'Samples\Image Processing and Analysis\car.bmp'
iw=op.new_image()
iw.from_file(fn)
iw.lname='original image'
iw.rgb2gray()
#make a copy to put result
iw2=iw.duplicate()
iw2.lname = 'thinning result'
img = iw2.to_np()
# Structuring Element
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
# Create an empty output image to hold values
thin = np.zeros(img.shape,dtype='uint8')
# Loop until erosion leads to an empty set or max
max = 20
while (cv2.countNonZero(img)!=0):
# Erosion
erode = cv2.erode(img,kernel)
# Opening on eroded image
opening = cv2.morphologyEx(erode,cv2.MORPH_OPEN,kernel)
# Subtract these two
subset = erode - opening
# Union of all previous sets
thin = cv2.bitwise_or(subset,thin)
# Set the eroded image for next iteration
img = erode.copy()
max -= 1
if max == 0:
break
iw2.from_np(thin)
Inverting an Image with skimage
#need to install "scikit-image" first
import originpro as op
import numpy as np
from skimage.util import invert
fn = op.path('e') + r'Samples\Image Processing and Analysis\Flower.jpg'
iw=op.new_image()
iw.from_file(fn)
iw.lname='original image'
#make a copy to put result
iw2=iw.duplicate()
iw2.lname='inverted'
img = iw2.to_np()
inv = invert(img)
iw2.from_np(inv)
#put into new empty image window
iw3=op.new_image()
#need to first setup the image window as 3 channels and not multi-frames
iw3.setup(3,False)
iw3.lname='invert back'
inv = invert(inv)
iw3.from_np(inv)
Load Digits Dataset from scikit-learn
import originpro as op
from sklearn.datasets import load_digits
digits = load_digits() # load digit images, total 1797 images, each is 8x8
aa = digits.images.astype(dtype='uint8')
iw = op.new_image()
iw.setup(1, True) # set image window is gray scale, with multiple frames
data = aa[0:10,:,:]
iw.from_np(data, False)
iw.set_int('GrayMax',16) #set to show only 17 colors (0-16)
iw.set_int('nav', 3) # Show navigation bar as slider
iw.set_str('Palette', 'Fire')
|