Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the mosaic effect of photos by JavaCV

Shulou Source: shulou.com Published: 2022-06-01 23:46:49 09月20日 Update

Editor to share with you how to achieve the mosaic effect of JavaCV photos, I hope you will learn something after reading this article, let's discuss it together!

Preparatory work

Let's introduce the dependency library of JavaCV first.

Org.bytedeco javacv-platform 1.5.6

If introduced in this way, everything contained in javacv will be introduced. You can do this when you test and use it at ordinary times. But in a real project, it still needs to be streamlined.

Besides, we have prepared a picture.

Code implementation

Read a file

Val path = "path/to/img/" val img = opencv_imgcodecs.imread (path + "meinv.jpeg")

Get the pixel width and height of the original image, and then scale the pixel ratio.

Val size = img.size () val height = size.height () val width = size.width () val pixelSize = 10 val newWidth = width / pixelSizeval newHeight = height / pixelSize

According to the set pixel ratio, the original image is reduced, and then magnified two times of resize operation. In this way, the pixel image processing is completed.

Val imgTmp: Mat? = null opencv_imgproc.resize (img, imgTmp, Size (newWidth, newHeight), 0.0,0.0, opencv_imgproc.INTER_NEAREST) opencv_imgproc.resize (img, imgTmp, Size (width, height), 0.0,0.0, opencv_imgproc.INTER_NEAREST)

Let's take a look at the image effect after processing.

The effect looks OK, the picture color is single, the picture size is slightly smaller, the effect will be much better.

Complete code import org.bytedeco.opencv.global.opencv_highguiimport org.bytedeco.opencv.global.opencv_imgcodecsimport org.bytedeco.opencv.global.opencv_imgprocimport org.bytedeco.opencv.opencv_core.Matimport org.bytedeco.opencv.opencv_core.Sizefun main (args: Array) {val path = "path/to/img/" val img = opencv_imgcodecs.imread (path + "meinv.jpeg") val size = img.size () val height = size.height () Val width = size.width () val pixelSize = 10 val newWidth = width / pixelSizeval newHeight = height / pixelSizeval imgTmp: Mat? = null opencv_imgproc.resize (img ImgTmp, Size (newWidth, newHeight), 0.0,0.0, opencv_imgproc.INTER_NEAREST) opencv_imgproc.resize (img, imgTmp, Size (width, height), 0.0,0.0, opencv_imgproc.INTER_NEAREST) opencv_highgui.imshow ("meinv", img) Opencv_highgui.waitKey (0)} after reading this article, I believe you have a certain understanding of "how JavaCV achieves photo mosaic effect". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Tags: Effects pixels pictures photos mosaics Marseilles code original pictures articles preparation processing images finished dimensions files methods time more knowledge industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft Apple OPPO Reno macOS