Now we will see some more aspects of python OpenCV. So, let’s start with writing the video. We have seen how we can start the web camera and visualize the image. However, sometimes we have to store the video in a format that we can watch it later. For example, if a company is doing some observation via webcam or IP cam. The person in front of a computer cannot monitor the footage all the time. In such cases, we store the video in a local computer for later usage. Now we will see how we can save the video. Following is the code:
So first we import modules and initialize the webcam. After that, we initialize the codec to write and give the format MJPG as an argument. Then we make an object of out file in which we specify the name of a file with proper extension and give the size of an image too. While the loop is started and will continue until the frames are present, it will write the frame and show the frames. In this way, we can write the video file.
If the program generates some error, then it may be because of the format. In such a scenario you can replace MJPG with following:
How Pixels Works:
We are now going to develop a program in which we will see how pixel works. This program is about converting the image into word. We have images of English alphabets as follows:
Now our main task is to analyze its pixel and print the alphabet that is shown in the image in the form of stars. In my directory I have all of the images from A to Z as follows:
So, I have to take all of the images and then analyze it as follows:
After importing the module, we have defined a function whose name is img2word as we are converting images to some printable words. In the first line, we read an image and then convert that image to grayscale means from 3 channels to one channel so that we can have only one value for one pixel which will be easy to manipulate. After that, we have resized the image to seven by seven as you can see in the images that there are seven dots vertically and seven horizontally. You can make any of the pixels multiples of seven depending on how big word you want to display. After this the array that is generated is as follows:
This array is an output of image of A alphabet. Now we will threshold the image and convert all the pixel to zero which is less than 140 using NumPy method of converting the elements. After that, we will make a loop and check if the pixel is zero than print “* “otherwise print the space.
Now we have to print out the name of king the output that is enervated as a result of running this program is as follows:
The image of it is as shown in Figure 2;
In such a way we can manipulate the pixels
Morphological operation is the task which we used to enhance the image. Sometimes the image is not so sharp or bright, or there may be too much noise in an image in such a situation we have to apply a morphological operation to change the image in the desired form. Following is the image on which we will be functioning.
Mentioned below are some morphological operations