After 3 episodes and a long break ( which i am extremely sorry for ) your friendly developer has come back with part 4 , likewise i thought you the basics of image processing we will continue our journey…

Talk is cheap, show me the code ~ linus Torvalds

Ok like wise our lord linus says , code shows your status, your dignity and your breed ( i should probably stop watching medieval movies : / )

In this tutorial i will be following up with python 2.7 and OpenCv3 ( i am extremely sorry for all the python 3 lovers … its the same shit with some different syntax i know you will manage it )

So to start with let me begin by teaching you about loading and displaying an image , but before i start let me tell you about a library called argparse – well i am pretty sure it will help you in the long run

So what is argparse ? well argparse is a command based argument parser lib for python, yeah don’t force your brain … i know : / ok to simply put it up argparse is a library which helps you to upload raw data into your python program, in from of meaningful data. Still didn’t get it ? continue reading you will surely get it when i some examples to you 😀 .

OpenCv Basics

Ok so consider you want to build a photoshop like software. What is the first thing that comes to your mind ? … you need to load a image first then you will edit it right? so to load a image and read the image file in form of coordinates (if you didn’t get the coordinates part refer part 1 for basics)

Ok enough of talk shit i ll show you the code –

import cv2

img = cv2.imread('human.png')

cv2.imshow("Title", img)

cv2.waitKey(0)
  1. The first thing you should do is you should load the cv2 library
  2. In second line we make a variable img and then store the processed value’s of the image which we get from imread (function)
  3. Now it should show you the image right ? so we create a window for showing the taken image using imshow(function)
  4. Now finally we use waitkey function to pause the created window else the window will not stay it will just run and close

Now lets see the parameters

cv2.imread (‘human.png’) now firstly if you don’t have knowledge about programming then you would probably find it confusing so to make it simple , firstly i ll tell you about basics so ,cv2 is a library and it contains many functions which are pre-made to help programmers right ? so when i use cv2.imread we run a function from cv2 library which is ( imread) to read a image file and then save that file to img which is a variable. ( if you are total newbie in programming then you start with learning python or JavaScript )

cv2.imshow(“title” , img) now here what we are doing is we are generating a window to show the loaded image ( when you run this program it will create a window to show loaded image) firstly we define window title ( see its like whenever you open a software it has a title bar upside right ? so this is that same title) then we put the variable name ( which is img in this case in which our processed image is stored).

Now finally we have cv2.waitKey(0) … remember K in Key should be capital(function naming scheme), this function is used for waiting ( i ll discuss this function in further posts because unless you try the program you won’t get it ) here 0 is for loop.

Now likewise i have discussed about image coordinates and RGB color code in my previous posts … so its time to play with it BABY ! hell yeah … so to start with it, again we will write a amazing program.

import cv2

image = cv2.imread('dog.png')

cv2.imshow("original", image)

image[0,0] = ( 0, 0, 255)

cv2.imshow("new", image)

cv2.waitKey(0)

note : i have just changed the name of variable as ” img ” as ” image ”

NOW, we know how to load a image so i will skip the image loading part and come to img[0,0] = ( 0, 0, 255)

now so when you see image[0,0] what are thinking ? if you are thinking that 0 ,0 are coordinate values of image in ( x,y ) then you are absolutely right ! tap your back , now lets see the further part ( 0 , 0, 255) do you recognize it ? YES its RGB format ! I know i am a good teacher ( only those who have read my previous posts will understand)

but there is a plot twist here – like most of the movies , idk why opencv developers did that but ( 0, 0, 255 ) is in reverse format BGR so ( 0, 0, 255 ) is (RED) , So here in this line ( img[0,0] = ( 0, 0, 255 ) ) what we are doing is we are changing the color of 0,0th coordinate pixel of dog image to red let me show you the results

So those are the two windows original and new , i know you can see the difference because dot is very small ( see left upside corner you will see a very tiny red dot)

OK lets make it more visible and see one more example

import cv2 

image = cv2.imread('dog.png')

cv2.imshow("original", image) 

image[0,0] = (0,0,255)
image[0,1] = (0,0,255)
image[0,2] = (0,0,255)
image[0,3] = (0,0,255)
image[0,4] = (0,0,255)
image[1,0] = (0,0,255)
image[2,0] = (0,0,255)
image[3,0] = (0,0,255)
image[4,0] = (0,0,255)
image[1,1] = (0,0,255)
image[2,2] = (0,0,255)
image[3,3] = (0,0,255)
image[4,4] = (0,0,255)
image[2,1] = (0,0,255)
image[2,2] = (0,0,255)
image[2,3] = (0,0,255)
image[2,4] = (0,0,255)

cv2.imshow("new", image)
cv2.waitKey(0)

This is the result…

I hope now you can see the red dot ! but hey remember you don’t need to write this whole bunch of coordinate’s to change pixels , there are algorithms for that ! i just showed you this in a basic manner ( to create rect there is a pre-made function called cv2.rectangle , its a drawing function which will be discussed later).

hey we have just moved one step closer to our photoshop like software !… we edited a image using programming ! isn’t it amazing you can also save this edited image using cv2.imwrite(“finished.png”, image ) ! here finished.png is the name of file and image is the variable name which holds the processed image.

So this is it for today, i hope you liked it and the next part i will be talking about more basic concepts like drawing using cv2 drawing functions ! if you have any doubts make a comment or send your questions to the forum page ! and do register to my blog.

P.S : Due to my busy schedule i ll be busy for some months so i ll not be posting frequently , sorry.But yet i will surely try to post.

Leave a comment

Your email address will not be published. Required fields are marked *