Skip to content

im_processing

Eddy Mina edited this page Aug 6, 2019 · 12 revisions

im_processing

General day to day numpy based image manipulation techniques (this is growing)

import image_processing as imp
from image_processing  import im_processing

cv_read(file_path,color_scale='RGB')::

Read jpg, jpeg, png image file paths. Can be read as RGB or default openCV read as BGR img.

  • Input:

    • file_path:: path to img file
    • Color Scale:: default 'RGB'. All scales == ['RGB','BGR','YIQ','GRAY']
      • (Red, Green, Blue) | (Blue, Green, Red) | (Blue, Green, Red) | YIQ | (Greyscale)
  • Output:

    • image (np.array)
img = im_processing.cv_read('images/img_10.jpg') #load RGB

plot_grey(im,title=None,xlabel=None,ylabel=None,convert_RGB=False)::

Simple matplotlib based image plotter. Takes in simple matplotlib args and plots grays and colored images easier without having to specify color maps.

  • Input:

    • im:: RGB image (np.array)
    • title:: image title
    • xlabel:: image xaxis label
    • ylabel:: image yaxis label
    • convert_RGB:: convert input image to matplotlib R,G,B format (otherwise image has blueish tint)
  • Output:

    • image plot
    • example:
img = im_processing.cv_read('images/img_10.jpg') #load RGB
img = im_processing.plot_grey(img) #plot img 

cv_plot(img,title= ' ',convert_BGR=False)::

Simple cv based image plotter. Plots grays and colored images easier without having to specify.

  • Input:

    • im:: BGR image (np.array)
    • title:: image title
    • convert_BGR:: convert input image to openCV B,G,R format (otherwise image has blueish tint)
  • Output:

    • image plot
img = im_processing.cv_read('images/img_10.jpg') #load RGB
img = im_processing.cv_plot(img) #plot img

im_subplot(ims,shape=None,titles=None,cmap= 'gray',suptitle=None,plot=True)::

Basic Image Subplotting Function.

  • Input:

    • ims:: list of images (np.array).
    • shape:: list of len 2 ie [1,2] is a subplot of one row 2 cols. Default = [-1,4]
    • titles:: images titles (optional)
    • cmap:: default gray. Allows colors and grays to plot easier
    • suptitle:: overhead title for all plots
    • plot:: True --> show plot
  • Output:

    • im subplot
img1 = im_processing.cv_read('images/img_1.jpg',color_scale='GRAY') #load GRAY
img10 = im_processing.cv_read('images/img_10.jpg',color_scale='RGB') #load RGB
im_processing.im_subplot(ims=[img1,img10],shape=[1,2], titles=['img1 gray','img10 colored'])

CLASS: im_stats(img)::

Currently works for a single image --> support to multiple images coming soon Provides rms,mean,std,and other distribution info for a single img

See source code for more indepth functions

  • Input:
    • img:: img/channel (np.array)
.describe_all(per_channel=True)
  • Input:

    • per_channel:: set to true for per channel stats. Only works for colored imgs
  • Output:

    • General, Distribution Stats, and Percentile Stats
Works for Grays ...
>> img1 = im_processing.cv_read('images/img_1.jpg',color_scale='GRAY') #load GRAY
>> im_processing.im_stats(img1).describe_all()
########## Image Stats ##########
Image Size (394, 640) ---> (Gray Image)
Min: 0
Max: 255
RMS: 10.57
----------------------
Distribution:
Intensity: 161.12 +/- 45.21
83.75% of pixels > 127.5
----------------------
Percentiles:
25% | 5.49% of pixels (~ 0 < i < 63.75)
50% | 10.76% of pixels (~ 63.75 < i < 127.5)
75% | 64.93% of pixels (~ 127.5 < i < 191.25)
100%| 34.92% of pixels (~ 191.25 < i < 255)

And Colored Images
>> img1 = im_processing.cv_read('images/img_1.jpg',color_scale='RGB') #load RGB
>> im_processing.im_stats(img1).describe_all(per_channel=True)
########## Image Stats ##########
Image Size (394, 640, 3) ---> (Colored Image)

 -+---------Channel-------+ 1 

Min: 0
Max: 255
RMS: 10.29
----------------------
Distribution:
Intensity: 106.59 +/- 49.32
36.02% of pixels > 127.5
----------------------
Percentiles:
25% | 22.14% of pixels (~ 0 < i < 63.75)
50% | 41.84% of pixels (~ 63.75 < i < 127.5)
75% | 53.77% of pixels (~ 127.5 < i < 191.25)
100%| 46.14% of pixels (~ 191.25 < i < 255)

 -+---------Channel-------+ 2 

Min: 0
Max: 255
RMS: 10.29
----------------------
Distribution:
Intensity: 152.45 +/- 45.43
78.95% of pixels > 127.5
----------------------
Percentiles:
25% | 6.31% of pixels (~ 0 < i < 63.75)
50% | 14.74% of pixels (~ 63.75 < i < 127.5)
75% | 68.37% of pixels (~ 127.5 < i < 191.25)
100%| 31.51% of pixels (~ 191.25 < i < 255)

 -+---------Channel-------+ 3 

Min: 0
Max: 255
RMS: 10.29
----------------------
Distribution:
Intensity: 198.87 +/- 50.89
89.92% of pixels > 127.5
----------------------
Percentiles:
25% | 3.62% of pixels (~ 0 < i < 63.75)
50% | 6.46% of pixels (~ 63.75 < i < 127.5)
75% | 22.34% of pixels (~ 127.5 < i < 191.25)
100%| 75.80% of pixels (~ 191.25 < i < 255)

resize(image, width = None, height = None, inter = cv2.INTER_AREA)::

Image Resizing Functon

  • Input:
    • image:: image (np.array).
    • width:: desired new width
    • height:: desired new width
    • inter:: interpolaton method (default cv2.INTER_AREA)

Function works based on height and width ratio

width/height = image.shape[0]/image.shape[1]

  • Therefore if width == None:

    • width = image.shape[0]/image.shape[1] * height
  • Else if height == None:

    • height = width/(image.shape[0]/image.shape[1])
  • Output:

    • resized image (np.array)
>> img = im_processing.cv_read('images/img_1.jpg') #load RGB
>> print(img.shape)
(491, 640, 3)
>> print(im_processing.resize(img,height=100).shape)
(100, 130, 3)

crop(img, x_left=0,x_right=0,y_bot=0,y_up=0)::

Crop an image

  • Input:

    • img:: image (np.array).
    • x_left:: (int) how much to cut in from left to right along x axis
    • x_right:: (int) how much to cut in from right to left along x axis
    • y_bot:: (int) how much to cut in from bottom to top along y axis
    • y_up:: (int) how much to cut in from top to bottom along y axis
  • Output:

    • cropped image (np.array)
>> img = im_processing.cv_read('images/img_1.jpg') #load RGB
>> print(img.shape)
(491, 640, 3)
>> print(im_processing.crop(img, x_left=100,y_bot=100).shape)
(391, 540, 3)

pad_with(img, pad_len=2, val=10)::

Pad image with set pad length and value

  • Input:

    • img:: color/gray image (np.array)
    • pad_len:: (int) thickness of padding (0 will just return the image)
    • val:: (int) value of padding
  • Output:

    • padded image (np.array)
>> img = im_processing.cv_read('images/img_1.jpg') #load RGB
>> print(img.shape)
(491, 640, 3)
>> print(im_processing.pad_with(img, pad_len=10, val=0).shape)#zero padding 
(511, 660, 3)

Putting it together

import image_processing as imp 
from image_processing  import im_processing,color_adjust #general image processing 

img = imp.cv_read('images/img_10.jpg',RGB=True) #load RGB 

gray= color_adjust.rgb2gray(img) #gray scale 

padded= im_processing.pad_with(gray,pad_len=100,val=128) #pad img with set length and vals (works for colors as well)

zoomed = im_processing.zoom_dup(img,factor=2) #simple zoom by pixel duplication (only ints)

cut = im_processing.cut(img,thresh=128) #vals < thresh = 0 (black) :: vals > thresh = 255 (white)

resized= im_processing.resize(img,img.shape[0]//2,img.shape[1]//2) #reshape image given desired size 

cropped = im_processing.crop(img,x_left=100,x_right=0,y_bot=100,y_up=0) #crop img 

ims= [img,gray,padded,zoomed,cut, resized, cropped]

titles= ["Original Image","Gray Scaled","Padded","Zoomed by Duplication","Thresholded", 
"Resized (1/2)","Cropped"]


imp.im_subplot(ims,shape=[3,3],titles=titles,suptitle="Some Tools in Image Processing Lib") #plot img subplot for comparison 

octocat