Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-

OpenCV-Python is an easy-to-develop environment for developing algorithms that use OpenCV. Here is a memo that will give you a hint when porting the algorithm developed there to C ++.

For OpenCV-Python: Use of numpy.array type → Replacement with cv :: Mat type numpy.array type specific method Replacement with cv :: Mat type method.

This kind of rewriting is easy. Even if you do not have enough experience to build image processing algorithms, you can rewrite OpenCV-Python code to OpenCV C ++ if you have experience with C ++.

numpy.array cv::Mat important point
a = np.ones((h,w), dtype=np.uint8) cv::Mat a=cv::Mat::ones(cv::Size(w, h), cv::CV_8U);
shape size shape is[h, w, channel]Order, or[h, w]
img.shape cv::Size(img.rows, img.cols)
img = cv2.imread("lena.png "); imgRGB=img[:, :, ::-1] cv::Mat img = cv::imread("lena.png "); cv2.imread()Is in order of BGR
import pylab as plt; plt.imshow(imgRGB) cv::imshow("title", img); matplotlib imshow()Is Matlab imshow()Close to
a[:, :] = 0 a.setTo(0); For color images a[:, :, :] = 0 。setTo()Argument is a constant
a[b>127] = 255 a.setTo(255, b > 127);
a = b+0 b.copyTo(a); In numpy just a=If b, then a is an alias for b, and the substance is the same.
a[c==d] = b[c==d] b.copyTo(a, c==d);
a = np.array(b, dtype=np.float32)/255.0 b.convertTo(a, CV_32F, 1.0/255);

OpenCV-Python code example

python


# -*- coding: utf-8 -*-

import cv2
import numpy as np
img = cv2.imread("lena.png ", 0)

b = np.zeros(img.shape[:2], dtype=np.uint8)
b[img > 128] = 255
cv2.imwrite("junk.png ", b)

Rewritten C ++ source code example

python


#include <opencv2/opencv.hpp>
int main(int argc, char* argv[]) {
    cv::Mat img= cv::imread("lena.png ", 0);
    cv::Mat b = cv::Mat::zeros(cv::Size(img.rows, img.cols), CV_8U);
    b.setTo(255, img>127);
    cv::imwrite("junk_cpp.png ", b);
}

Image of execution result junk.png


Useful functions in numpy numpy.rot90(m, k=1, axes=(0, 1) (https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.rot90.html)

Recommended Posts

Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
Use a scripting language for a comfortable C ++ life
Use a scripting language for a comfortable C ++ life 3-Leave graphing to matplotlib-
Let's use a scripting language for a comfortable C ++ life C ++ implementation after verification with python
Let's use a scripting language for a comfortable C ++ life 2 Automatically generate C ++ source
Use a scripting language for a comfortable C ++ life 4-Use your own C ++ library from a scripting language-
Let's use a scripting language for a comfortable C ++ life 6-How about developing a program as a library for Python?
Use a scripting language for a comfortable C ++ life 5 --Use the Spyder integrated environment to check numerical data-
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Python] How to use the in operator in a for statement?
[Python] Organizing how to use for statements
How to use "deque" for Python data
An introduction to Python for C programmers
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
Tips for Python beginners to use the Scikit-image example for themselves 9 Use from C
Tips for Python beginners to use Scikit-image examples for themselves 3 Write to a file
[Python] It was very convenient to use a Python class for a ROS program.
Ported a naive homebrew language compiler to Python
Writing logs to CSV file (Python, C language)
Wrap C ++ with Cython for use from Python
I made a module in C language to filter images loaded by Python
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
Create a dataset of images to use for learning
[For beginners] How to use say command in python!
How to use Python Kivy ① ~ Basics of Kv Language ~
[Python] How to call a c function from python (ctypes)
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module
How to use pip, a package management system that is indispensable for using Python
[Python] I want to use only index when looping a list with a for statement
A python script that wants to use Mac startup / end time for attendance management
Call C language functions from Python to exchange multidimensional arrays
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Tips for Python beginners to use Scikit-image examples for themselves 4 Use GUI
python3: How to use bottle (2)
A road to intermediate Python
[python] How to use the library Matplotlib for drawing graphs
[Python] Use a string sequence
[Python] How to use list 1
How to use the __call__ method in a Python class
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
[python] A note when trying to use numpy with Cython
How to temporarily implement a progress bar in a scripting language
A simple way to avoid multiple for loops in Python
How to use Python argparse
How to define multiple variables in a python for statement
How to make a Python package (written for an intern)
How to use machine learning for work? 03_Python coding procedure
Python: How to use pydub
[Python] How to use checkio
I didn't know how to use the [python] for statement
Tips for Python beginners to use the Scikit-image example for themselves
~ Tips for beginners to Python ③ ~
[Python] How to use input ()
Try to select a language
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes