NuGet - OpenCV 5 beta

NuGet packages for OpenCV 5 - Static Library for Visual Studio 2019 and 2022

https://www.tiziran.com/topics/opencv

Install and setup your OpenCV project in just 5 minutes

Config your visual studio project for computer vision application

static opencv library for visual studio 2022 by using NuGet package manager just in few minutes

https://www.youtube.com/watch?v=AEqZO_fZHZ8

#YouTube #OpenCV5 #cpp #vs22

Test, C++, Computer Vision, Image Processing,

download source code (GitHub): https://github.com/pirahansiah/

#OpenCV #Farshid_PirahanSiah #tiziran

My NuGet packages comprised of two versions for different VS version.



more: https://www.tiziran.com/topics/opencv

cvtest: Computer Vision Test

Unit Test, Integration Test, System Test, Acceptance Test for Computer Vision and Deep Learning

Do you want to test your output of computer vision application which is video or images?

Standard test for computer vision application

There isn't any standard test for computer vision program. I wrote many test by myself and I would like to share some of them here. For example, I write a program to test docker and check the processing time, memory usage, CPU usage, etc. In computer vision application sometime you need to check the output which is the image. How do you want to check it. I write some program to check the output which is the image and compare the ground truth. I check some well known methods such as PSNR, SSIM, Image quality, distortion, brightness, sharpness, etc. Furthermore, I check much different hardware and write some test for computer vision application base on different hardware architecture and Evaluation hardware.

Do you want to know your program Automatically adjusting brightness of image in the right way?, How do you know using generic sharpening kernel to remove blurriness is working?, How to do check FPS process?, Which OCR system work better for your input image?

https://github.com/pirahansiah/cvtest/blob/main/README.md



download source code (GitHub): https://github.com/pirahansiah/

more: https://www.tiziran.com/topics/opencv

#OpenCV #Farshid_PirahanSiah #tiziran

sample code in C++:



cvtest

Advanced OpenCV techniques:


sub-pixel, floating points, more precise, real-valued coordinates, Changing the contrast and brightness of an image in CV_32FC3 with OpenCV, Tips and Tricks of OpenCV that Nobody Told You


download source code (GitHub): https://github.com/pirahansiah/

more: https://www.tiziran.com/topics/opencv

#OpenCV #Farshid_PirahanSiah #tiziran

sample code in C++:


Advanced OpenCV techniques:


Cross correlation (CC): TM_CCORR

Mean shifted cross correlation (Pearson correlation coefficient): TM_CCOEFF

Normalization: TM_SQDIFF_NORMED, TM_CCORR_NORMED, TM_CCOEFF_NORMED

maximum absolute difference metric (MaxAD), which is also known as the uniform distance metric computeECC() and findTransformECC().

Sum of absolute differences (SAD)

Cross correlation (CC)


find identical regions of an image that match a template, select by giving a threshold

2D convolution

It simply slides the template image over the input image (as in 2D convolution) and compares the template and patch of input image under the template image.

Template matching

> cv2.TM_CCOEFF

> cv2.TM_CCOEFF_NORMED

> cv2.TM_CCORR

> cv2.TM_CCORR_NORMED

< cv2.TM_SQDIFF

< cv2.TM_SQDIFF_NORMED

cv2.minMaxLoc()



more: https://www.tiziran.com/topics/opencv

#OpenCV #Farshid_PirahanSiah #tiziran

sample code in C++:


https://docs.opencv.org/master/df/dfb/group__imgproc__object.html#gga3a7850640f1fe1f58fe91a2d7583695da5be00b45a4d99b5e42625b4400bfde65

https://stackoverflow.com/questions/58158129/understanding-and-evaluating-template-matching-methods

Advanced OpenCV techniques: balance white


balance white with OpenCV, Tips and Tricks of OpenCV that Nobody Told You

more: https://www.tiziran.com/topics/opencv

#OpenCV #Farshid_PirahanSiah #tiziran

sample code in C++:



void balance_white(cv::Mat mat) {

double discard_ratio = 0.05;

int hists[3][256];

memset(hists, 0, 3 * 256 * sizeof(int));


for (int y = 0; y < mat.rows; ++y) {

uchar* ptr = mat.ptr<uchar>(y);

for (int x = 0; x < mat.cols; ++x) {

for (int j = 0; j < 3; ++j) {

hists[j][ptr[x * 3 + j]] += 1;

}

}

}


// cumulative hist

int total = mat.cols * mat.rows;

int vmin[3], vmax[3];

for (int i = 0; i < 3; ++i) {

for (int j = 0; j < 255; ++j) {

hists[i][j + 1] += hists[i][j];

}

vmin[i] = 0;

vmax[i] = 255;

while (hists[i][vmin[i]] < discard_ratio * total)

vmin[i] += 1;

while (hists[i][vmax[i]] > (1 - discard_ratio) * total)

vmax[i] -= 1;

if (vmax[i] < 255 - 1)

vmax[i] += 1;

}


for (int y = 0; y < mat.rows; ++y) {

uchar* ptr = mat.ptr<uchar>(y);

for (int x = 0; x < mat.cols; ++x) {

for (int j = 0; j < 3; ++j) {

int val = ptr[x * 3 + j];

if (val < vmin[j])

val = vmin[j];

if (val > vmax[j])

val = vmax[j];

ptr[x * 3 + j] = static_cast<uchar>((val - vmin[j]) * 255.0 / (vmax[j] - vmin[j]));

}

}

}

}


reference http://www.ipol.im/pub/art/2011/llmps-scb/

https://gist.github.com/tomykaira/94472e9f4921ec2cf582





Advanced OpenCV techniques: contrast and brightness

sub-pixel, floating points, more precise, real-valued coordinates, Changing the contrast and brightness of an image in CV_32FC3 with OpenCV, Tips and Tricks of OpenCV that Nobody Told You

more: https://www.tiziran.com/topics/opencv

#OpenCV #Farshid_PirahanSiah #tiziran

sample code in C++:


showImage.convertTo(showImage, CV_32FC3);

double alpha = 2.0; /*< Simple contrast control */

int beta = 100; /*< Simple brightness control */

for (int y = 0; y < showImage.rows; y++) {

for (int x = 0; x < showImage.cols; x++) {

for (int c = 0; c < showImage.channels(); c++) {

showImage.at<cv::Vec3f>(y, x)[c] =cv::saturate_cast<float>(alpha * showImage.at<cv::Vec3f>(y, x)[c] + beta);

}

}

}

showImage.convertTo(showImage, CV_8UC3);

cv::imshow("Changing the contrast and brightness of an image! ", showImage);

cv::waitKey(0);



based on https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html

https://blog.csdn.net/u014230360/article/details/109802229

Advanced subpixel techniques: Shift image content

sub-pixel, floating points, mesh grid, remap, more precise, real-valued coordinates, moving image pixel, Shift image content with OpenCV, Tips and Tricks of OpenCV that Nobody Told You

more: https://www.tiziran.com/topics/opencv

#OpenCV #Farshid_PirahanSiah #tiziran

sample code in C++:


Mesh grid float

static void meshgrid_float(const cv::Mat& xgv, const cv::Mat& ygv,

cv::Mat1f& X, cv::Mat1f& Y)

{

cv::repeat(xgv.reshape(1, 1), ygv.total(), 1, X);

cv::repeat(ygv.reshape(1, 1).t(), 1, xgv.total(), Y);

}


static void meshgrid_map_float(const cv::Range& xgv, const cv::Range& ygv,

cv::Mat1f& X, cv::Mat1f& Y)

{

std::vector<float> t_x, t_y;

for (int i = xgv.start; i <= xgv.end; i++) t_x.push_back(i);

for (int i = ygv.start; i <= ygv.end; i++) t_y.push_back(i);

meshgrid_float(cv::Mat(t_x), cv::Mat(t_y), X, Y);

}


mesh grid int

static void meshgrid(const cv::Mat& xgv, const cv::Mat& ygv,

cv::Mat1i& X, cv::Mat1i& Y)

{

cv::repeat(xgv.reshape(1, 1), ygv.total(), 1, X);

cv::repeat(ygv.reshape(1, 1).t(), 1, xgv.total(), Y);

}


static void meshgridTest(const cv::Range& xgv, const cv::Range& ygv,

cv::Mat1i& X, cv::Mat1i& Y)

{

std::vector<int> t_x, t_y;

for (int i = xgv.start; i <= xgv.end; i++) t_x.push_back(i);

for (int i = ygv.start; i <= ygv.end; i++) t_y.push_back(i);

meshgrid(cv::Mat(t_x), cv::Mat(t_y), X, Y);

}



main:

cv::Mat1f XF, YF;

//for int

//meshgrid_map_float(cv::Range(0, cols_main), cv::Range(0, rows_main), XF, YF);

//for float

meshgrid_map_float(cv::Range(0, cols_main-1), cv::Range(0, rows_main-1), XF, YF);

for (int rowsImage = 0; rowsImage < rows_main; ++rowsImage) {

for (int colsImage = 0; colsImage < cols_main; ++colsImage) {

XF.at<float>(rowsImage, colsImage) += offset1;

YF.at<float>(rowsImage, colsImage) += offset2;

}

}

cv::remap(convert_img_i, dst, XF, YF, cv::INTER_LINEAR);

if (show)

{

cv::Mat resizedImage = dst.clone();

dst.convertTo(resizedImage, CV_8UC3);

cv::resize(resizedImage, resizedImage, cv::Size(), 0.5, 0.5);

std::string nameWindow = " meshgrid and remap in float ";

cv::imshow(nameWindow, resizedImage);

cv::waitKey(0);

}






Tips and Tricks of OpenCV that Nobody Told You

Tips and Tricks of OpenCV that Nobody Told You

more: https://www.tiziran.com/topics/opencv

#OpenCV #Farshid_PirahanSiah #tiziran

sample code in C++:






Tricks

cv::multiply(outMat, cv::Scalar(gain, gain, gain), outMat); //color

cv::multiply(outMat, cv::Scalar(gain), outMat); //grayscale



//copy small Mat to bigger Mat

cv::Rect roi( cv::Point( originX, originY ), smallImage.size() );

smallImage.copyTo( bigImage( roi ) );


Tips

  • copy mat to vector need clone()


Save results

  • save image in float

    • cv::imwrite("image.exr", MatImage);

  • save image in uncompressed format :

    • cv::imwrite("fa.tiff", resizedImage, { cv::IMWRITE_TIFF_COMPRESSION, 1,cv::IMWRITE_TIFF_XDPI, 300,cv::IMWRITE_TIFF_YDPI,300 });

        • cv2.imwrite(filename, array, params=(cv2.IMWRITE_TIFF_COMPRESSION, 1)) # None

        • cv2.imwrite(filename, array, params=(cv2.IMWRITE_TIFF_COMPRESSION, 5)) # LZW

        • cv2.imwrite(filename, array, params=(cv2.IMWRITE_TIFF_COMPRESSION, 8)) # Adobe Deflate

        • cv2.imwrite(filename, array, params=(cv2.IMWRITE_TIFF_COMPRESSION, 32946)) # Deflate

    • cv::imwrite("far.png", resizedImage, { cv::IMWRITE_PNG_COMPRESSION, 0 });

  • Save images in streaming

    • int64 t0 = cv::getTickCount();

    • std::string fileName= "fashid_"+std::to_string(t0)+ ".png";

    • cv::imwrite(fileName, cimMat, { cv::IMWRITE_PNG_COMPRESSION, 0 });

  • Save file name:

    • std::filesystem::path p = std::filesystem::path(files[i]).filename();

    • std::string imgFile = savePath + "/" + p.string() + ".tiff";

    • cv2.imwrite(filename, array, params=(cv2.IMWRITE_TIFF_COMPRESSION, 1)) ;


Error

  • Exception thrown at 0x00007FFB5CB21636 (vcruntime140d.dll) in farshid.exe: 0xC0000005: Access violation writing location 0x000002B09BAC4000.

    • check the size of Mat

      • cv::Mat meanImages = cv::Mat::zeros(rows_main, cols_main, CV_32F);

      • cv::Mat meanImages = cv::Mat::zeros(farshidMat.size(), CV_32F);

  • Linker Tools Error LNK2038 & Linker Tools Error LNK2001; Severity Code Description Project File Line Suppression State Error LNK2038 mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in Source.obj ; Severity Code Description Project File Line Suppression State Error LNK2038 mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease'

    • the link files are not match based on release or debug mode.



Testing for OpenCV Projects

Test, C++, Computer Vision, Image Processing,

more: https://www.tiziran.com/topics/opencv

#OpenCV #Farshid_PirahanSiah #tiziran

sample code in C++:

  • Arrange, Act and Assert (AAA) Pattern

  • Google C++ Test Framework

    • Assertions Types and Test Fixtures


ASSERT_FALSE(frame.empty());

ASSERT_NO_THROW(cap >> img);

ASSERT_FALSE(img.empty()) << "idx=" << idx;




Tricks

embedded system, keep the software as small as possible, Embedding static elements in your application,

https://gstreamer.freedesktop.org/documentation/installing/index.html?gi-language=c




Tips

  • copy mat to vector need clone()


Example

#include <cassert>

assert(!im.empty());

assert(x.size()==y.size());

assert(x.size()>2);

#ifdef _DEBUG

#endif

#if true

#else

#endif

YouTube

Install and setup your OpenCV project in just 5 minutes

Config your visual studio project for computer vision application

static opencv library for visual studio 2022 by using NuGet package manager just in few minutes

https://www.youtube.com/watch?v=AEqZO_fZHZ8

#YouTube #OpenCV5 #cpp #vs22

Test, C++, Computer Vision, Image Processing,

more: https://www.tiziran.com/topics/opencv

#OpenCV #Farshid_PirahanSiah #tiziran










https://github.com/xiaohongchen1991/meshgen

A set of C++ APIs are provided to mimic the same behaviors as the MATLAB function "linspace" and "meshgrid".

Must-Read Books of All Time in Computer Vision and Machine Learning