Friday, December 13, 2019

6. Using Numpy with OpenCV-Python

Hey, welcome back its been a while since I have been active on this blog, but yes i was preparing something great, that is a free YouTube course on Advanced Computer Vision using OpenCV-Python.


I have already released one section of the course you can find it on this
URL: https://www.youtube.com/playlist?list=PLwRoxHWReaEhVFjTeKlifKUimbw6ZyV7K 


Now about Numpy, Numpy stands for Numeric Python, it is highly optimized library for python for doing numeric operations, that is addition multiplication, subtraction and division not only on numbers but n-dimentional array or matrix you can say.

So OpenCV uses Numpy to represent the images, now whatever operation you do, you'll be doing with Numpy Array or matrix you can say.

To prove this I'm going to show you a small code snippet which reads image using opencv and displays the data type of the image variable.



As you can see it shows numpy.ndarray which means numpy n-dimentional array because the image is RGB image, it has width,height and channels.


Can we create an image using Numpy then??


The answer is Yes, we can create a blank image using Numpy, let see how to do it.


The first step is to import numpy library with alias, alias is nothing but the custom name you give to your package which is to be considered throughout the program life cycle. It can be np, it can be num or anything you like, the best practice is to keep it a short name because we will be needing the alias name to access numpy methods.

In can second step we are creating a blank image, we are using np alias of numpy package to access zeros method what it means that whatever size of the array will be created it will be filled with zero(0) values. In OpenCV 0 value means black Pixel, so our image will be of black pixel.

Zeros method takes two arguments 1. Shape 2. data type.

1. Shape: shape is nothing the the dimension or size of our image, if its gray or single channel image the shape will be (width,height) and if your image is RGB then the shape will be  (width,height,channels).

Important thing to not here is these values are passed as tuples, tuples are nothing but the type of array you can say except its values can not be changed.

2. dtype (data type): opencv uses uint8 data type to represent the images, because the pixel values ranges from 0 to 255. any addition or  or subtraction from the values should be result in circular increment or decrements . that means the maximum value of the pixel is 255 if you add +1 to the pixel value dtype="uint8" will reset the value to 0 so 256 value will be considered as 0 and 257 as 1 and so on.

Same when subtracting value from 0 it will reset the value to 255 like 0 - 1 = 255, 0 - 2 = 254 and so on.



 Changing the value of x=0 y=0 c=0 by adding 265 to its original value it results in 9. its circular.

In other words, 0 + 265 = 9 when uint8 data type is used, because 255 is max value for uint8 data type. when the values reaches 256 its sets its value to 0 and then again the increment starts so result is 9.

I hope you understood the concept. Let me know in case of any doubts.


Until then Happy Learning.


Jaimin Bhoi
Assistant System Engineer/Computer Vision Researcher
Tata Consultancy Services.





#opencv #python #ai #ml #dl #machinelearning #deeplearning #keras #tensorflow #pytorch #anaconda #conda #spyder #jupyter #notebook #opencv #computervision #logo #deeplearning #artificialintelligence #ai #python #aipython #machinelearning #deeplearning #photography #photoshop

Wednesday, July 10, 2019

5. Saving Video using OpenCV

Hi there, its been a long time since i haven't updated anything on this blog, but i haven't come back with empty hands or empty mind, i have very great ideas to post on this blog, so stay tuned.

If you haven't check my other posts i request you to go through it once.

Let me start where i left, i have completed below topics.

1. Introduction to OpenCV & Installing OpenCV in your PC. (Link)
2. Writing your first OpenCV program. (Link)
3. Read,Save and display Images (Link)
4. Reading videos with OpenCV (Link)
4. Writing videos with OpenCV (This blog)

In this blog i want to show you how to save videos with OpenCV, before that we need to read video or video stream to our program so if you dont know how to do that i request you to go through this post to know how to read videos using OpenCV.

Ok lets get started,

1. Read video from either your webcam or from any other video file.


In my previous post i showed you how to read videos using imutils package, but in this video i want to show you how to read video using inbuilt OpenCV function cv2.VideoCapture().

Create a new file name it as WriteVideo.py




Import cv2 library, and initialize video capture instance.
videofile variable should contain a path to your video file if you are using video.

if you are using webcam just replace the code as below,



Now lets see if our video or webcam is working or not,


In above code what we are doing is that, we are checking if the video capture stream is opened or not? by using isOpened() method. the reason why we are using this method is to end the infinite while loop when video frame is ended when we are using video files. while in webcam if the webcam is not found the while loop will not initiate.

Once we are in while loop, we are reading frames from videofile or the webcam, the read() method returns two values, first one is flag containing true or false for frame is received or not, and second is frame it self. frame is nothing but an image from the video source.


after that we are quickly checking if we have received frame or not, if not we are ending our program, if you are using video file the program will end as soon as the video is ended.

on line 13 we are displaying our frame by using cv2.imshow(), line 14 is to record user keyboard event when we want to exit in between by pressing q key from keyboard we can exit the while loop by breaking the loop.

once the loop is exited, we have to close the window opened by cv2.imshow() method or else it'll stay opened all the time even if we end our program, and also we have to release our capture instance as well as the the resource that we are using (i.e videofile or webcam so that other process or program can access it afterwards.


2. Save video frames.


Before saving the video, you should know that each computer has something called codecs, which is used while saving,displaying(rendering) videos on our computer. my computer might have different codec and your computer might have different codec as well. so if i write code according to my computer's codec it might possible that it'll not work on your computer, if it is having different codec. So to solve this issue i have developed a module that can check what codecs are supported by your computer so that you can use them to save the video.


so to do that you need that module, you can download it from my github repo. Download it or just do git checkout of that file, place this file in same directory where WriteVideo.py present.


Now once you have this file import this file(module) in our WriteVideo.py file.


Make sure the file name is correct with case sensitive.

Once you import this file, you are ready to test the codecs installed in your system.


In above code i have commented out the webcam code because in this module we will be needing one video file to give as an input, so i have given path to the video file and i'm passing the videofile path to the CV2VideoWriter instance. you can take any file and give it as an input, make sure you enter correct path, best practice is to put video file in same directory and give videofile name in your program.

Note: Please take video which is short in length like 1 to 2 minutes, so that the procedure finishes faster. Also take long enough so that we can extract few frames and save the video.

Once you run this code, what actually this CV2VideoWriter does is that it reads the video given as an input and tries to save video with different available codecs, one which passes the saving procedure it counts as a working codec where it fails it count that codec as a not supported codec by system.

In output you can see the available codecs in your computer which you can use to save the videos from OpenCV.

If you are running code in cmd you can see some errors with output which you can ignore,but observe the line start with codec and showing its availability, where its stated as true means that codec is available to use.


But of you run this program on spyder IDE, you can see a clear output shown below. If you want to know more about spyder IDE leave your questions in comment section.



Now we have the codec names which we can use to save the videos using OpenCV.

Below i have attached the full code for saving video.


There are few changes that we will do while saving the video,

1. While initiating CV2VideoWriter Instance we will provide codec that is working in our your PC.
2. We will provide channel type i.e single channel that is black and white or 3 channel that is RGB. so to do this we will provide channel=0 for single channel and for RGB channel=1. (if you dont do this you might get an error)
3. We will start a session for video writer to save a frame.
4. We will read a frame from webcam and convert it into gray image using cv2.cvtcolor() method. if you are using video that is fine, if you have provided channel=0 that is single channel then you need to convert your frame into gray that is single channel or else it will throw an error.
5. After converting we will save the frame using writter.save_session_frame() method.

Once all the frame saving is done we need to end the session so that our writer knows to finally save the video and of course we need to release our capture instance so that other program can access out webcam or video file.

Kudos, now you know how to save video using OpenCV python.

Let me know in case you are facing any issue with above method.

tags: opencv,python,video,videowrite,how to write video using opencv,videowritter,ffmpeg, codec, VideoWriter_fourcc, opencv with python, video operations, convert to gray.