| OpenCV 5.0.0alpha Open Source Computer Vision

Loading...
Searching...
No Matches
Ascend NPU Image Processing

# Goal

In this guide, you will gain insights into the thread safety of Ascend operators already in use, as well as discover how to effectively employ Ascend operators for image preprocessing and understand their usage limitations.

# Preface

We provide a suite of common matrix operation operators that support the Ascend NPU within OpenCV. For user convenience, the new 'AscendMat' structure and its associated operators maintain compatibility with the 'Mat' interface in OpenCV. These operators encompass a wide range of frequently used functions, including arithmetic operations, image processing operations, and image color space conversion. All of these operators are implemented utilizing CANN(Compute Architecture of Neural Networks). The Ascend operator facilitates accelerated operations on the NPU by making use of CANN. This acceleration effect is particularly noticeable when working with larger images, such as those with dimensions like 2048x2048, 3840x2160, 7680x4320, etc.

# Instructions on Thread Safety

Our stream function is implemented by invoking the CANN operators. In the same stream, tasks are executed sequentially, while across different streams, tasks are executed in parallel. The use of event mechanisms ensures synchronization of tasks between streams, please refer to the Stream Management documentation for details.

# Example for Image Preprocessing

In this section, you will discover how to use Ascend operators for image preprocessing, including functions below:

- Add
- Rotate
- Flip

## code

C++
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include <iostream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/cann.hpp>
#include <opencv2/cann_interface.hpp>
int main(int argc, char* argv[])
{
cv::CommandLineParser parser(argc, argv,
"{@input|puppy.png|path to input image}"
"{@output|output.png|path to output image}"
"{help||show help}");
parser.about("This is a sample for image processing with Ascend NPU. \n");
if (argc != 3 || parser.has("help"))
{
parser.printMessage();
return 0;
}
std::string imagePath = parser.get<std::string>(0);
std::string outputPath = parser.get<std::string>(1);
// read input image and generate guass noise
cv::Mat img = cv::imread(imagePath);
// Generate gauss noise that will be added into the input image
cv::Mat gaussNoise(img.rows, img.cols, img.type());
cv::RNG rng;
rng.fill(gaussNoise, cv::RNG::NORMAL, 0, 25);
// setup cann
cv::cann::initAcl();
cv::cann::setDevice(0);
cv::Mat output;
// add gauss noise to the image
cv::cann::add(img, gaussNoise, output);
// rotate the image with a certain mode (0, 1 and 2, correspond to rotation of 90, 180 and 270
// degrees clockwise respectively)
cv::cann::rotate(output, output, 0);
// flip the image with a certain mode (0, positive and negative number, correspond to flipping
// around the x-axis, y-axis and both axes respectively)
cv::cann::flip(output, output, 0);
cv::imwrite(outputPath, output);
cv::cann::resetDevice();
cv::cann::finalizeAcl();
return 0;
}
cann.hpp
cann_interface.hpp
cv::CommandLineParser
Designed for command line parsing.
Definition utility.hpp:890
cv::Mat
n-dimensional dense array class
Definition mat.hpp:951
cv::RNG
Random Number Generator.
Definition core.hpp:2805
cv::RNG::NORMAL
@ NORMAL
Definition core.hpp:2808
cv::RNG::fill
void fill(InputOutputArray mat, int distType, InputArray a, InputArray b, bool saturateRange=false)
Fills arrays with random numbers.
cv::cann::resetDevice
void resetDevice()
Clear all context created in current Ascend device.
cv::cann::initAcl
void initAcl()
init AscendCL.
cv::cann::finalizeAcl
void finalizeAcl()
finalize AscendCL.
cv::cann::setDevice
void setDevice(int device)
Choose Ascend npu device.
cv::cann::rotate
void rotate(InputArray src, OutputArray dst, int rotateCode, AscendStream &stream=AscendStream::Null())
Rotates a 2D array in multiples of 90 degrees. The function cv::rotate rotates the array in one of th...
cv::cann::flip
void flip(InputArray src, OutputArray dst, int flipCode, AscendStream &stream=AscendStream::Null())
Flips a 2D matrix around vertical, horizontal, or both axes.
cv::cann::add
void add(const InputArray src1, const InputArray src2, OutputArray dst, const InputArray mask=noArray(), int dtype=-1, AscendStream &stream=AscendStream::Null())
Computes a matrix-matrix or matrix-scalar sum.
cv::imwrite
CV_EXPORTS_W bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
Saves an image to a specified file.
cv::imread
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
main
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
imgcodecs.hpp
Python
# This file is part of OpenCV project.
# It is subject to the license terms in the LICENSE file found in the top-level directory
# of this distribution and at http://opencv.org/license.html.
import numpy as np
import cv2
import argparse
parser = argparse.ArgumentParser(description='This is a sample for image processing with Ascend NPU.')
parser.add_argument('image', help='path to input image')
parser.add_argument('output', help='path to output image')
args = parser.parse_args()
# read input image and generate guass noise
#! [input_noise]
img = cv2.imread(args.image)
# Generate gauss noise that will be added into the input image
gaussNoise = np.random.normal(0, 25,(img.shape[0], img.shape[1], img.shape[2])).astype(img.dtype)
#! [input_noise]
# setup cann
#! [setup]
cv2.cann.initAcl()
cv2.cann.setDevice(0)
#! [setup]
#! [image-process]
# add gauss noise to the image
output = cv2.cann.add(img, gaussNoise)
# rotate the image with a certain mode (0, 1 and 2, correspond to rotation of 90, 180
# and 270 degrees clockwise respectively)
output = cv2.cann.rotate(output, 0)
# flip the image with a certain mode (0, positive and negative number, correspond to flipping
# around the x-axis, y-axis and both axes respectively)
output = cv2.cann.flip(output, 0)
#! [image-process]
cv2.imwrite(args.output, output)
#! [tear-down-cann]
cv2.cann.finalizeAcl()
#! [tear-down-cann]

## Explanation

Input Image

C++
cv::Mat img = cv::imread(imagePath);
// Generate gauss noise that will be added into the input image
cv::Mat gaussNoise(img.rows, img.cols, img.type());
cv::RNG rng;
rng.fill(gaussNoise, cv::RNG::NORMAL, 0, 25);
Python
# Read the input image
img = cv2.imread("/path/to/img")
# Generate gauss noise that will be added into the input image
gaussNoise = np.random.normal(mean=0,sigma=25,(img.shape[0],img.shape[1],img.shape[2])).astype(img.dtype)

Setup CANN

C++
cv::cann::initAcl();
cv::cann::setDevice(0);
Python
cv2.cann.initAcl()
cv2.cann.setDevice(0)

Image Preprocessing Example

C++
cv::Mat output;
// add gauss noise to the image
cv::cann::add(img, gaussNoise, output);
// rotate the image with a certain mode (0, 1 and 2, correspond to rotation of 90, 180 and 270
// degrees clockwise respectively)
cv::cann::rotate(output, output, 0);
// flip the image with a certain mode (0, positive and negative number, correspond to flipping
// around the x-axis, y-axis and both axes respectively)
cv::cann::flip(output, output, 0);
Python
# add gauss noise to the image
output = cv2.cann.add(img, gaussNoise)
# rotate the image with a certain mode (0, 1 and 2, correspond to rotation of 90, 180
# and 270 degrees clockwise respectively)
output = cv2.cann.rotate(output, 0)
# flip the image with a certain mode (0, positive and negative number, correspond to flipping
# around the x-axis, y-axis and both axes respectively)
output = cv2.cann.flip(output, 0)

Tear down CANN

C++
cv::cann::resetDevice();
cv::cann::finalizeAcl();
Python
cv2.cann.finalizeAcl()

Results

1. The original RGB input image with dimensions of (480, 640, 3):

puppy

1. After introducing Gaussian noise, we obtain the following result:

puppy_noisy

1. When applying the rotate operation with a rotation code of 0 (90 degrees clockwise), we obtain this result:

puppy_noisy_rotate

1. Upon applying the flip operation with a flip code of 0 (flipping around the x-axis), we achieve the final result:

puppy_processed_normalized