Skip to main content

Modules, Classes and Objects (Python): Simple Explanation

First let us start with dictionary..

The dictionary is defined in the format: key:value 

For example,

dict={'name' : 'Python', 'version' : 2.7, 'new version' : 3.5} 

In the dictionary, name is associated to python, version is associated to 2.7 and so on.

Now we start with definition of MODULES..

Modules are like dictionaries, we can think module is a python file (*.py) and then it can be imported and used. The modules can contain some functions and variables.

For example, if i have a dictionary like this

mydict={'Job' : "Research"}

then its module can be created as

#this file need to be saved as mydict.py

def Job():
    print("Research")

#let also define a variable
domain="Image Processing"

Now we can access the file (technically module need to be imported)

import mydict  
mydict.Job()  

print (mydict.domain)

Lets go to CLASSES now.. Class is an another construct to create a function group, data and pack them as a container.

class mydict(object):
 
    def __init__(self):

         self.domain="Image Processing"

    def Job(self):
       
          print("Research")

Like how we imported the modules, the class need to be instantiated and once don you get an OBJECT. The class can be instantiated as follows


my=mydict()
my. Job()
print (my.domain)

The python creates an object "my" which contains function and variables associated with the class mydict.

simple explanation of the init can be found here

https://micropyramid.com/blog/understand-self-and-__init__-method-in-python-class/






















Comments

Popular posts from this blog

Image RGB to CMYK : Image Conversion - MATLAB Code (RGB to CMYK, CMYK to RGB)

MATAB use makecform and applycform functions to perform this color model conversion  Ref: makecform:  https://www.mathworks.com/help/images/ref/makecform.html Ref: applycform:  https://www.mathworks.com/help/images/ref/applycform.html RGB to CMYK: I_rgb = imread('lena_color.jpg'); %Read the color image  C = makecform('srgb2cmyk');     %srgb (standard rgb) to cmyk  I_CMYK= applycform(I_rgb,C); TO SAVE IN CMYK Format imwrite(I_CMYK, 'test.tiff'); USE IMFINFO to confirm the conversion imfinfo('test.tiff') the command gives the output ... Width: 512 Height: 512 BitDepth: 32 ColorType: 'CMYK' ... CMYK to RGB: I_CMYK = imread('lena_color.jpg'); %Read the color image  C = makecform('cmyk2srgb');     %srgb (standard rgb) to cmyk  I_rgb= applycform(I_CMYK, C);

Digital Half Toning - Ordered Dithering - MATLAB Code Bayer/ Ulichney

Digital Half-toning is a technique to convert the gray scale / color image (in the range 0-255) to binary images (in the range 0-1)  that is useful for printing (especially black and white printers). Though at first glance dots are clearly visible, as Human Vision System is a low pass filter, when dots are printed closely and viewed from the specific distance it can be perceived as a continuous tone image (i.e. gray scale). In general, half toning techniques is widely used in newspapers, journals and magazines printing.  Halftoning algorithms aims for better #Image Quality #Low Complexity #Optimal ink usage Coming to the topic, Ordered dithering is one of the standard technique to produce halftone image from continuous (gray scale) images. Let us assume, the first block containing 'B' is an image matrix and the  'H' is the threshold matrix. And the process is simply a element wise (pixel wise) comparison; if element of B > H (g

Digital Half-toning Error Diffusion Method (Floyd-Steinberg) - MATLAB Code

Introduction to Digital Half toning is provided in the below link http://imageprocessing-sankarsrin.blogspot.tw/2016/12/digital-half-toning-ordered-dithering.html The halftones are generally obtained by thresholding each image pixel with a certain value (ex. 128). In error diffusion, the error is computed between the actual and obtained output and it is distributed to the neighborhood pixels. Floyd-Stein berg, Jarvis, Stucki kernels are widely adopted  to distribute the error. The Error Diffusion half toning is as follows 1) image(x,y) > 128  Apply thresholding (assume  the image pixel value is 157,  in that case, 255 will be assigned to output). ( as the half toning corresponds to two values 0 and 255) 2) Compute the error between actual and obtained output    E=   255-157= 98 3) In case of floyd steinberg the value 98 is distributed along the neighborhood pixels 4) This steps are performed in sequence and serpentine scan yields superior halftone output. The ma