[PYTHON] Try blurring the image with opencv2

Purpose

Because of the article the other day, I became a little interested in image processing. However, the last time I touched mathematics was the final exam of high school 3. Moreover, 2 points. There were some things that I couldn't understand, but first I wrote the code to try it out.

First of all, installation and preparation of images

opencv2 seems to be a module for image processing. So it seems that I can't find it even if I do "pip install open cv2" easily. Apparently this is the correct one.

$ pip install opencv-python

The blurred image uses a country-like image of free material as an eye candy.

inaka.jpg

Let's blur this guy.

code

Let's write the code.

Import part + image reading

main.py


import cv2

#Load image
image = cv2.imread("inaka.jpg ")

(Supplement) If you want to convert from BGR to RGB

This opencv2 seems to express the image in BGR. And matplotlib, a library that can be used to save and draw processed images, is in RGB format. If you use matplotlib, ignore the differences here and proceed! Lol

new_image.jpg

So, when converting from BGR to RGB, do this. This time, the processed image is not passed to matplotlib, so it is unnecessary.

main.py


image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

Blur

It's finally time to start blurring. That said, just call the method.

main.py


blurr_image = cv2.GaussianBlur(image, (5, 5), 0)

This Guassian Blur, I didn't quite understand the argument. The first argument is an image object, the second argument is a numeric array called a kernel, and the third argument seems to be the standard deviation. It all sounds cosmic to me. I should have studied properly.

Export image

Now export the completed image.

main.py


cv2.imwrite("new_image.jpg ", blurr_image)

Code final form

The whole code looks like this.

main.py


import cv2

image = cv2.imread("inaka.jpg ")
blurr_image = cv2.GaussianBlur(image, (5, 5), 0)
cv2.imwrite("new_image.jpg ", blurr_image)

I will check

It's changed ...?

new_image.jpg

I tried playing with Gaussian Blur's arguments a little bit, but I couldn't grasp the rules and gave up today because I didn't have much time.

Gori push breakthrough

As an aside, I hate glasses because my eyes are too bad, and I'm afraid of contact lenses, so I can't put them in. Everything looks blurry from usual. I don't know at all whether it really blurred me. Perhaps the above image is also blurred, but I couldn't feel it, so I thought I'd turn it about 100 times repeatedly (I think it's definitely wrong).

main.py


import cv2
image = cv2.imread("inaka.jpg ")

for i in range(1,100):
    image = cv2.GaussianBlur(image, (5, 5), 0)

cv2.imwrite("new_image.jpg ", image)

I feel like it's blurred properly. new_image.jpg

By the way, even if I increased the number of repetitions any more, the degree of blur did not increase dramatically. I feel like there was someone who was familiar with the juniors who left the company, so I'd like to ask.

Conclusion

・ Image blur can be realized with opencv2 ・ I don't know how to use Guassian Blur, but it looks like it after repeating the muddy blurring process.

Recommended Posts

Try blurring the image with opencv2
Try using the camera with Python's OpenCV
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
Try edge detection with OpenCV
Image editing with python OpenCV
Get image features with OpenCV
Try OpenCV with Google Colaboratory
Image recognition with Keras + OpenCV
I tried "gamma correction" of the image with Python + OpenCV
Real-time image processing basics with opencv
CNN with keras Try it with the image you picked up
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
Finding the simplest mistakes with OpenCV
Try face recognition with python + OpenCV
Find image similarity with Python + OpenCV
I tried to process the image in "sketch style" with OpenCV
I tried to process the image in "pencil style" with OpenCV
Image display taken with the built-in ISIGHT
Try the Variational-Quantum-Eigensolver (VQE) algorithm with Blueqat
Image processing with Python & OpenCV [Tone Curve]
Image acquisition from camera with Python + OpenCV
Right-click the image → realize "Compress with TinyPNG"
Easily try automatic image generation with DCGAN-tensorflow
Try rewriting the file with the less command
Try to generate an image with aliasing
Light image processing with Python x OpenCV
Image processing with Lambda + OpenCV (gray image creation)
How to crop the lower right part of the image with Python OpenCV
Try to image the elevation data of the Geographical Survey Institute with Python
Read the graph image with OpenCV and get the coordinates of the final point of the graph
Try to separate the background and moving object of the video with OpenCV
Try to solve the fizzbuzz problem with Keras
XavierNX accelerates OpenCV image processing with GPU (CUDA)
Crop the image to rounded corners with pythonista
Try to solve the man-machine chart with Python
How to crop an image with Python + OpenCV
How to try the friends-of-friends algorithm with pyfof
Detect folders with the same image in ImageHash
The easiest way to use OpenCV with python
Try converting videos in real time with OpenCV
I tried using the image filter of OpenCV
Try to detect fish with python + OpenCV2.4 (unfinished)
[Python + OpenCV] Whiten the transparent part of the image
Display the image after Data Augmentation with Pytorch
Former image processing engineer tried to solve Saizeriya's spot the difference with OpenCV
Region extraction method using cellular automaton Try region extraction from the image with growcut (Python)
Try to measure the position of the object on the desk (real coordinate system) from the camera image with Python + OpenCV
Try scraping with Python.
Image processing with MyHDL
Extract the table of image files with OneDrive & Python
Try to solve the programming challenge book with python3
Detect stoop with OpenCV
Binarization with OpenCV / Python
Image recognition with keras
[Cloudian # 8] Try setting the bucket versioning with Python (boto3)
Read the OpenCV documentation
Try to visualize the room with Raspberry Pi, part 1
Try to solve the internship assignment problem with Python