Showing posts with label TBB. Show all posts
Showing posts with label TBB. Show all posts

2/19/2023

How to Install OpenCV 4.7 with CUDA, cuDNN, TBB, CUDA Video Codec, and Extra Modules in Linux

 


refer to bash code


.

#!/bin/bash

# Install dependencies
sudo apt-get update
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
sudo apt-get install libcanberra-gtk-module libcanberra-gtk3-module

# Install CUDA 11
wget https://developer.download.nvidia.com/compute/cuda/11.4.0/local_installers/cuda_11.4.0_470.57.02_linux.run
sudo sh cuda_11.4.0_470.57.02_linux.run --silent --toolkit --override
echo 'export PATH=/usr/local/cuda-11.4/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-11.4/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

# Download and extract TBB
wget https://github.com/oneapi-src/oneTBB/releases/download/v2022.0.0/oneapi-tbb-2022.0.0-lin.tgz
tar -xf oneapi-tbb-2022.0.0-lin.tgz
sudo cp -r oneapi-tbb-2022.0.0/lib/* /usr/local/lib/
echo 'export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

# Download and extract OpenCV 4.7 and OpenCV extra modules
wget https://github.com/opencv/opencv/archive/4.7.0.zip
unzip 4.7.0.zip
cd opencv-4.7.0

wget https://github.com/opencv/opencv_contrib/archive/4.7.0.zip
unzip 4.7.0.zip

# Build and install OpenCV 4.7 with CUDA, cuDNN, TBB, CUDA video codec, and OpenCV extra modules
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.7.0/modules -D WITH_CUDA=ON -D WITH_TBB=ON -D WITH_NVCUVID=ON -D WITH_GSTREAMER=ON -D WITH_GSTREAMER_0_10=OFF -D WITH_LIBV4L=ON -D WITH_CUDNN=ON -D CUDA_ARCH_BIN=7.5 ..
make -j$(nproc)
sudo make install
echo 'export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH' >> ~/.bashrc
source ~/.bashrc

# Compile and run the sample code
cd ../../
wget https://raw.githubusercontent.com/spmallick/learnopencv/master/Averaging4kVideo/Averaging4kVideo.cpp
g++ Averaging4kVideo.cpp -o Averaging4kVideo `pkg-config --cflags --libs opencv4`
./Averaging4kVideo

..



thank you.

🙇🏻‍♂️

www.marearts.com

3/08/2017

Refer to this setting when you build opencv with TBB


When you build opencv with checking with_tbb option, cmake ask crrect paths about tbb.

Refer to this setting.
note, this is set for vc12 and 32bit.



TBB_ENV_INCLUDE :
/tbb2017_20170226oss_win/tbb2017_20170226oss/include

TBB_ENV_LIB :
/tbb2017_20170226oss_win/tbb2017_20170226oss/lib/ia32/vc12/tbb.lib

TBB_ENV_LIB_DEBUG :
/opencv/tbb2017_20170226oss_win/tbb2017_20170226oss/lib/ia32/vc12/tbb_debug.

TBB_VER_FILE :
/tbb2017_20170226oss_win/tbb2017_20170226oss/include/tbb/tbb_stddef.h


3/06/2017

TBB doesn't have CMakelist.txt, go to here.

Intel Threading Building Blocks..

I downloaded the source code here -> https://www.threadingbuildingblocks.org/
and I want to build it with cmake.

But I meet this error -> ...does not appear to contain CMakeLists.txt

Looking for a long time to figure out what to do ..
I found this site -> https://github.com/wjakob/tbb

Thank you.


5/04/2015

build opencv 3.0 + cuda 6.5 + QT 5.4 + tbb

These are cmake configuration for OpenCV rc1 + CUDA 6.5 + QT 5.4 + TBB 4.3








I thought it will help opencv building beginner.
I will help how to set path and what I should check.

And configuration result is here.



Now I am building on VS 2012... I hope to build with no error.. ^^

Refer to this page, these are my result of building opencv in waste of time.




12/23/2014

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

In past, I wrote an articel about YUV 444, 422, 411 introduction and yuv <-> rgb converting example code.
refer to this page -> http://feelmare.blogspot.kr/2012/11/yuv-color-format-444-422-411-simple.html

In this article, I will introduce method of using opencv and TBB.
TBB is an acronym for Thread Building Block.
TBB is to enable parellel processing using multi thread.
see the this page -> http://feelmare.blogspot.kr/2014/12/opencv-tbb-utility-parallelfor.html


This is YUV422 to RGB example source code.
In my case YUV422 is consisted of YUYV.
And input type of the YUYV data is unsigned char *.

So example is
unsigned char * yuyv to Mat rgb
In here, m_stride is real width length of yuyv data.

....
Mat yuyv(m_height, m_width, CV_8UC2);
memcpy( yuyv.data, yuyv_buffer, sizeof(unsigned char) * (m_stride * m_height) );
Mat rgb(m_height, m_width, CV_8UC3);
cvtColor(yuyv, rgb, CV_YUV2BGR_YUYV);
....



Next example is rgb to yuyv using TBB.
....
class Parallel_process : public cv::ParallelLoopBody
{

private:
 cv::Mat& inImg;
 unsigned char* outImg;
 int widhStep;
 int m_stride;

public:
 Parallel_process(cv::Mat& inputImgage,  unsigned char* outImage)
  : inImg(inputImgage), outImg(outImage){

   widhStep = inputImgage.size().width * 3; 
   m_stride = inputImgage.size().width *2;

 }

 virtual void operator()(const cv::Range& range) const
 {
  //thread
  for(int i = range.start; i < range.end; i++)
  {

   int s1 = i*widhStep;

   for(int iw=0; iw< inImg.size().width; iw=iw+2)
   {
    int s2 = iw*3;

    int mc = s1+s2;
    int B1 = (unsigned char)(inImg.data[mc + 0]);
    int G1 = (unsigned char)(inImg.data[mc + 1]);
    int R1 = (unsigned char)(inImg.data[mc + 2]);
    int B2 = (unsigned char)(inImg.data[mc + 3]);
    int G2 = (unsigned char)(inImg.data[mc + 4]);
    int R2 = (unsigned char)(inImg.data[mc + 5]);


    int Y = (0.257*R1) + (0.504*G1) + (0.098*B1) +16;
    int U = -(0.148*R1) - (0.291*G1) + (0.439*B1) + 128;
    int V = (0.439*R1 ) - (0.368*G1) - (0.071*B1) + 128;
    int Y2 = (0.257*R2) + (0.504*G2) + (0.098*B2) +16;

    Y = MMIN(255, MMAX(0, Y));
    U = MMIN(255, MMAX(0, U));
    V = MMIN(255, MMAX(0, V));
    Y2 = MMIN(255, MMAX(0, Y2)); 

    mc = i*m_stride + iw*2;
    outImg[mc + 0] = Y;
    outImg[mc + 1] = U;
    outImg[mc + 2] = Y2;
    outImg[mc + 3] = V;

   }
  }
 }
};



//in main rutine
cv::parallel_for_(cv::Range(0, (OriginMat).rows), Parallel_process((OriginMat), inP_OriginImg));

....


In opencv convert function,  YUYV to RGB option is exist (-> CV_YUV2BGR_YUYV).
But RGB to YUYV option is not exist.

thank you.

12/23/2013

TBB example source code ( parallel_invoke function )

You need TBB lib to run this source code.

Download TBB file, and unzip your propely new folder in your computer.
And copy dll files into windows\system folder.

TBB download url -> https://www.threadingbuildingblocks.org/download

 
 
Set path of TBB incoude, lib location on the Visual studio.
 



This is example source code.
 

/////
#include < stdio.h>
#include < tchar.h>
#include < time.h>
#include < tbb/tbb.h>

#ifdef _DEBUG
#pragma comment(lib, "tbb_debug.lib")
#else
#pragma comment(lib, "tbb.lib");
#endif // _DEBUG

using namespace tbb;

void main()
{
 task_scheduler_init init;

 parallel_invoke(
  []()->void
 {
  ::Sleep(1000);
  ::printf("finish:%d\n", 1000);
 },
  []()->void
 {
  ::Sleep(1000);
  ::printf("finish:%d\n", 10000);
 });

 printf("All work is done \n");

 getchar();
 
}
/////

12/20/2013

Install TBB + CUDA with OpenCV, ( How to setup TBB, CUDA on the OpenCV, VS 2011, window)

I introduced CUDA + OpenCV on this page -> http://feelmare.blogspot.kr/2013/12/gpicuda-opencv-setting-method-and.html

In this post, I will introduce TBB + CUDA + OpenCV install(setup) method.
TBB is abbreviation of Intel Threading Builidng Block.
This supports speed up method using parallel processing of CPU.

TBB is free and you can download on this site - > https://www.threadingbuildingblocks.org/download
Downloaded TBB file don't need setup. Just unzip the file in the appropreate directory.
There is Bin, Lib and include folder in the unzeipped directory. You can be programming directly using these files.


But if you want to use the TBB supported function of OpenCV, you have to make new OpenCV libs, dlls on your computer.
It is same procees with CUDA + OpenCV -> http://feelmare.blogspot.kr/2013/12/gpicuda-opencv-setting-method-and.html.

1. run cmake.
 
"C:/opencv_247/source" is the location of OpenCV folder.
"C:/opencv_247/source/OpenCV_CUDA_Tbb_247" is target folder to make source codes.
Make target folder at any where freely.
 
2. configuration click and select your compiler.
3. Check option


 
4. click configuration and select the location of TBB's include folder.

 
 
5. click configuration
 
You will see this red line, but the direction infomation will probably correct.
so click configuration again.
 
 
Do you see yes sign in the "Use TBB : " list?
Check~!!
 
This is my last option check list.


 
This list included cuda and TBB options.
Now, click gnerate. and you can see files made newly in the target folder.
Open OpenCV.sln file by your Visual Studio Tool.
 
And complie ~!! praying..
 
After compile realese and debug mode.
Gathering the bin and lib file in the properly folder.
 
 
 
 

 
 
----------------------------------------------------------------------------------------------
#Tip 1 : If you fail to compile opencv_source code, try rebuild again.
              In my case, I have successed by twice compile( build -> rebuild )
----------------------------------------------------------------------------------------------
 
To use TBB, You should set tbb include, lib path on your visual studio.

 
 
And copy tbb dll files in to the Windows->systems folder.
 

 
 
This is example source code using TBB
 
////
#include <  stdio.h >  
#include <  vector >
#include <  opencv2\opencv.hpp > 
#include <  opencv2\stitching\stitcher.hpp >

#ifdef _DEBUG  
#pragma comment(lib, "opencv_core247d.lib")   
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing  
//#pragma comment(lib, "opencv_objdetect246d.lib")   
//#pragma comment(lib, "opencv_gpu247d.lib")  
//#pragma comment(lib, "opencv_features2d246d.lib")  
#pragma comment(lib, "opencv_highgui247d.lib")  
//#pragma comment(lib, "opencv_ml246d.lib")
#pragma comment(lib, "opencv_stitching247d.lib")
#pragma comment(lib, "tbb_debug.lib")

#else  
#pragma comment(lib, "opencv_core247.lib")  
#pragma comment(lib, "opencv_imgproc247.lib")  
//#pragma comment(lib, "opencv_objdetect246.lib")  
//#pragma comment(lib, "opencv_gpu247.lib")  
//#pragma comment(lib, "opencv_features2d246.lib")  
#pragma comment(lib, "opencv_highgui247.lib")  
//#pragma comment(lib, "opencv_ml246.lib") 
#pragma comment(lib, "opencv_stitching247.lib")
#pragma comment(lib, "tbb.lib")
#endif  


using namespace cv;  
using namespace std;


void main()  
{
 
 vector<  Mat > vImg; 
 vector<  vector<  Rect > > vvRect;
 Mat rImg;

 vImg.push_back( imread("./stitching_img/m1.jpg") );
 //vImg.push_back( imread("./stitching_img/m8.jpg") );
 //vImg.push_back( imread("./stitching_img/m5.jpg") );
 vImg.push_back( imread("./stitching_img/m4.jpg") );
 vImg.push_back( imread("./stitching_img/m2.jpg") );
 //vImg.push_back( imread("./stitching_img/m7.jpg") );
 //vImg.push_back( imread("./stitching_img/m6.jpg") );
 vImg.push_back( imread("./stitching_img/m3.jpg") );
 //vImg.push_back( imread("./stitching_img/m9.jpg") );
 //vImg.push_back( imread("./stitching_img/m10.jpg") );
 //vImg.push_back( imread("./stitching_img/m11.jpg") );
 //vImg.push_back( imread("./stitching_img/m12.jpg") );
 //vImg.push_back( imread("./stitching_img/m13.jpg") );
  
  
 
 
 int c = gpu::getCudaEnabledDeviceCount();
 printf("%d\n", c);
    

 Stitcher stitcher = Stitcher::createDefault(1);


 unsigned long AAtime=0, BBtime=0;
 AAtime = getTickCount();

 //stitcher.stitch(vImg, vvRect, rImg);
 stitcher.stitch(vImg, rImg);

 BBtime = getTickCount(); 
 printf("%.2lf sec \n",  (BBtime - AAtime)/getTickFrequency() );

 imshow("Stitching Result", rImg);
 
 waitKey(0); 

}  
////

----------------------------------
#Tip 2 : If you don't know setting method Opencv + Visual Studio, reference this page http://feelmare.blogspot.kr/2013/08/visual-studio-2012-opencv-246-setting.html
#Tip 3 : If you want to GPU + Opencv, reference this page
 http://feelmare.blogspot.kr/2013/12/gpicuda-opencv-setting-method-and.html.
#Tip 4: You want to know my TBB run successfully or not, reference this page
http://feelmare.blogspot.kr/2013/12/tbb-example-source-code-parallelinvoke.html