SCARA-Laser. Part 3. Let's start processing the image
Prologue
Hello everyone! As promised, we are starting to analyze the machine vision part of this project. First, let's figure out what we have and what we want to get in the end. We have a color image of a human face. Let's just have it for now, because getting images from a camera on a Raspberry Pi is a well-known topic, I will not focus on it separately.
![Let's assume this. Now you can find me and beat me for the crooked code) Let's assume this. Now you can find me and beat me for the crooked code)](https://habrastorage.org/getpro/habr/upload_files/ffa/160/82e/ffa16082e153d2ba634cf1272c62ba06.jpg)
We need:
Make the laser distinguish one pixel from another
Clean up all unnecessary things
In other words, we convert the color to black and white and vectorize the raster. We need to get something similar to a pencil drawing.
The Plot
First, let's install the openCV library for python. To do this, open the terminal and type:
pip install opencv-python
With this easy move we installed openCV for all our python projects. I am happy with this arrangement, but if anything, you can always deviate from the guide and work with a virtual machine) Now open your favorite editor, in my case VS code, and create a project.
Development
The next thing we will do is import cv2 and open the image.
import cv2 as cv
orig = cv.imread('me.jpg')
Instead of me,jpg Enter the name of the image located in the project folder. religious For reasons of the patent on the RGB model, openCV uses the BGR model for storage and processing. Let's convert our color image to the BW format, as if we were taking a photo with grandma's camera. This is done as follows:
gray = cv.cvtColor(orig, cv.COLOR_BGR2GRAY)
Let's take a closer look cv.cvtColor(orig, cv.COLOR_BGR2GRAY).
This function converts an image and returns the result. The first argument is the already read image, and the second is the conversion profile. There are many of them in cv2, enough for all needs, we need one of the simplest.
Next, we need to somehow clean the image from unnecessary pixels so that there is no mess. The best way to deal with this is the blur of the king of mathematics (Gauss), but it will blur not the voids (light), but on the contrary, the dark. That is, what we need to express, so we first invert the image, and after applying GaussianBlur we'll return it back.
inv_gray = 255 - gray
blur_img = cv.GaussianBlur(inv_gray, (11, 11), 0)
inv_blur = 255 - blur_img
cv.GaussianBlur(inv_gray, (11, 11), 0)
takes three parameters as input: the original image, the dimensions of the rectangle from which the average reading is taken to determine the brightness of the current pixel, and the so-called sigma. We don't need it yet, so we'll leave it at 0.
All that's left is to overlay our gray image with a blurred white brightness of 255 and that's it. We do it like this:
sketch_img = cv.divide(gray, inv_blur, scale=255.0)
Climax
Let's add another preview of each stage before clicking the button. I think there is no need for explanations here:
cv.imshow("orig", orig)
cv.waitKey()
cv.imshow("gray", gray)
cv.waitKey()
cv.imshow("inv_gray", inv_gray)
cv.waitKey()
cv.imshow("blur_img", blur_img)
cv.waitKey()
cv.imshow("inv_blur", inv_blur)
cv.waitKey()
cv.imshow("sketch_img", sketch_img)
cv.waitKey()
cv.imwrite("sketch.jpg", sketch_img)
cv.destroyAllWindows()
At the end, I still save the result. We assemble the project and see what we got.
![Step by step 1 Step by step 1](https://habrastorage.org/getpro/habr/upload_files/8d8/df5/ce9/8d8df5ce91a9e395950319768a393310.png)
![Step by step 2 Step by step 2](https://habrastorage.org/getpro/habr/upload_files/2ee/690/75e/2ee69075e7d654575b5acb2bbf325f7f.png)
![Result) Result)](https://habrastorage.org/getpro/habr/upload_files/898/327/cc2/898327cc2d705973c5ea60ace2420eff.jpg)
The clarity can be adjusted with the blur rectangle and the camera resolution.
The Plot
Let me finally show you for beginners how to take an image from a camera in openCV.
To do this, we first initialize our camera, then we will receive a frame from it until the button is pressed. The last received frame will be our image.
cam = cv.VideoCapture(0)
while True:
_, frame = cam.read()
cv.imshow("monit", frame)
k = cv.waitKey(5)
if k == ord("q"):
cv.imwrite("me.jpg", frame)
break
Well, everything together looks like this:
import cv2 as cv
cam = cv.VideoCapture(0)
while True:
_, frame = cam.read()
cv.imshow("monit", frame)
k = cv.waitKey(5)
if k == ord("q"):
cv.imwrite("me.jpg", frame)
break
orig = cv.imread('me.jpg')
gray = cv.cvtColor(orig, cv.COLOR_BGR2GRAY)
inv_gray = 255 - gray
blur_img = cv.GaussianBlur(inv_gray, (11, 11), 0)
inv_blur = 255 - blur_img
sketch_img = cv.divide(gray, inv_blur, scale=255.0)
cv.imshow("orig", orig)
cv.waitKey()
cv.imshow("gray", gray)
cv.waitKey()
cv.imshow("inv_gray", inv_gray)
cv.waitKey()
cv.imshow("blur_img", blur_img)
cv.waitKey()
cv.imshow("inv_blur", inv_blur)
cv.waitKey()
cv.imshow("sketch_img", sketch_img)
cv.waitKey()
cv.imwrite("sketch.jpg", sketch_img)
cv.destroyAllWindows()
Epilogue
Well, I think I told you everything I wanted) I'll be very glad to hear comments. Read the previous parts)