Skip to content

Instantly share code, notes, and snippets.

@cn007b

cn007b/OpenCVBlur.md

Last active Nov 13, 2020
Embed
What would you like to do?
OpenCV Blur - Fast/Faster/Fastest | Blur image using OpenCV

OpenCV Blur - Fast/Faster/Fastest

Python

import cv2


def b(f):
  img = cv2.imread(f)

  top, bottom, left, right = 286, 308, 111, 144
  rec = img[top:bottom, left:right]
  rec = cv2.GaussianBlur(rec, (3, 9), 5)
  img[top:bottom, left:right] = rec

  cv2.imwrite('img.after.py.png', img)


if __name__ == '__main__':
  b('/tmp/img.before.png')

Run:

python3 benchmark.py

Golang

package main

import (
	"image"

	"gocv.io/x/gocv"
)

func b(f string) {
	img := gocv.IMRead(f, gocv.IMReadUnchanged)
	defer img.Close()

	top, bottom, left, right := 286, 308, 111, 144
	r := image.Rectangle{Min: image.Point{X: left, Y: top}, Max: image.Point{X: right, Y: bottom}}
	rec := img.Region(r)
	gocv.GaussianBlur(rec, &rec, image.Pt(3, 9), 5, 5, gocv.BorderDefault)
	rec.Close()

	gocv.IMWrite("img.after.go.png", img)
}

func main() {
	b("/tmp/img.before.png")
}

Run:

go build -o /tmp/b_go benchmark.go
/tmp/b_go

C++

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <iostream>

int b(std::string f)
{
    cv::Mat img = cv::imread(f, CV_LOAD_IMAGE_UNCHANGED);

    int top = 286, bottom = 308, left = 111, right = 144;
    cv::Rect r = cv::Rect(left, top, right - left, bottom - top);
    cv::Mat rec = img(r);
    cv::blur(rec, rec, cv::Size(3, 9));
    img(r) = rec;

    cv::imwrite("img.after.cpp.png", img);
}

int main(int argc, char *argv[])
{
    b("/tmp/img.before.png");
    return 0;
}

Run:

g++ -w benchmark.cpp -o /tmp/b_cpp `pkg-config --cflags --libs opencv`
/tmp/b_cpp

PS

You can find more info in this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment