Vous êtes sur la page 1sur 11

Laboratory workbook for Image Processing using GPU

____________________________________________________________________________________

LAB 22

GRAB CUT SEGMENTATION

Objective: To perform grab cut segmentation .

Part List:
 Nvidia Jetson TX2 Board
 Power adapter
 Hard Disk with Ubuntu 16.04
 Ethernet connection
 Nsight Eclipse Edition
 OpenCV 3.4.0
 PC
 Jetpack
 Cuda Toolkit 9.0
 SSD card

Hardware Connection:
 Connect the Jetson board to the monitor with an HDMI cable.
 Connect the adapter to power ON the board.
 Connect ethernet to the host PC and the Jetson Board.

Included Files:

Header Files
iostream
cstdio
opencv2/opencv.hpp
opencv2/cudalegacy.hpp
opencv2/core.hpp
opencv2/core/utility.hpp
vector
fstream

Library Files:

 opencv_calib3d
 opencv_core
 opencv_features2d
 opencv_flann
 opencv_highgui
 opencv_imgcodecs
 opencv_imgproc

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Image Processing using GPU
____________________________________________________________________________________

 opencv_ml
 opencv_objdetect
 opencv_photo
 opencv_shape
 opencv_stitching
 opencv_superres
 opencv_video
 opencv_videoio
 opencv_videostab
 opencv_cudalegacy
 opencv_cudabgsegm
 opencv_cudaimgproc
 opencv_cudawarping
 opencv_cudafilters
 opencv_cudaarithm

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Image Processing using GPU
____________________________________________________________________________________

Program Listing:

#if defined _MSC_VER && _MSC_VER >= 1400


#pragma warning(disable : 4100)
#endif
#include <iostream>
#include <iomanip>
#include "opencv2/opencv.hpp"

static void help()


{
cout << "Usage: ./cascadeclassifier \n\t--cascade
<cascade_file>\n\t(<image>|--video <video>|--camera <camera_id>)\n"
"Using OpenCV version " << CV_VERSION << endl << endl;
}

static void convertAndResize(const Mat& src, Mat& gray, Mat&


resized, double scale)
{
if (src.channels() == 3)
{
cv::cvtColor( src, gray, COLOR_BGR2GRAY );
}
else
{
gray = src;
}

Size sz(cvRound(gray.cols * scale), cvRound(gray.rows * scale));

if (scale != 1)
{
cv::resize(gray, resized, sz);
}
else
{
resized = gray;
}
}

static void convertAndResize(const cv::cuda::GpuMat& src,


cv::cuda::GpuMat& gray, cv::cuda::GpuMat& resized, double scale)
{
if (src.channels() == 3)
{
cv::cuda::cvtColor( src, gray, COLOR_BGR2GRAY );
}
else
{
gray = src;
}

Size sz(cvRound(gray.cols * scale), cvRound(gray.rows * scale));

if (scale != 1)
{
cv::cuda::resize(gray, resized, sz);

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Image Processing using GPU
____________________________________________________________________________________

}
else
{
resized = gray;
}
}

static void matPrint(Mat &img, int lineOffsY, Scalar fontColor,


const string &ss)
{
int fontFace = FONT_HERSHEY_DUPLEX;
double fontScale = 0.8;
int fontThickness = 2;
Size fontSize = cv::getTextSize("T[]", fontFace, fontScale,
fontThickness, 0);

Point org;
org.x = 1;
org.y = 3 * fontSize.height * (lineOffsY + 1) / 2;
putText(img, ss, org, fontFace, fontScale, Scalar(0,0,0),
5*fontThickness/2, 16);
putText(img, ss, org, fontFace, fontScale, fontColor,
fontThickness, 16);
}

static void displayState(Mat &canvas, bool bHelp, bool bGpu, bool


bLargestFace, bool bFilter, double fps)
{
Scalar fontColorRed = Scalar(255,0,0);
Scalar fontColorNV = Scalar(118,185,0);

ostringstream ss;
ss << "FPS = " << setprecision(1) << fixed << fps;
matPrint(canvas, 0, fontColorRed, ss.str());
ss.str("");
ss << "[" << canvas.cols << "x" << canvas.rows << "], " <<
(bGpu ? "GPU, " : "CPU, ") <<
(bLargestFace ? "OneFace, " : "MultiFace, ") <<
(bFilter ? "Filter:ON" : "Filter:OFF");
matPrint(canvas, 1, fontColorRed, ss.str());

// by Anatoly. MacOS fix. ostringstream(const string&) is a


private
// matPrint(canvas, 2, fontColorNV, ostringstream("Space -
switch GPU / CPU"));
if (bHelp)
{
matPrint(canvas, 2, fontColorNV, "Space - switch GPU /
CPU");
matPrint(canvas, 3, fontColorNV, "M - switch OneFace /
MultiFace");
matPrint(canvas, 4, fontColorNV, "F - toggle rectangles
Filter");
matPrint(canvas, 5, fontColorNV, "H - toggle hotkeys help");
matPrint(canvas, 6, fontColorNV, "1/Q - increase/decrease
scale");
}

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Image Processing using GPU
____________________________________________________________________________________

else
{
matPrint(canvas, 2, fontColorNV, "H - toggle hotkeys help");
}
}

int main(int argc,char** argv)


{

if (getCudaEnabledDeviceCount() == 0)
{
return cerr << "No GPU found or the library is compiled
without CUDA support" << endl, -1;
}

cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
string inputName;
string cascadeName;
bool isInputImage = false;
bool isInputVideo = false;
bool isInputCamera = false;

for (int i = 1; i < argc; ++i)


{
if (string(argv[i]) == "--cascade")
cascadeName = argv[++i];
else if (string(argv[i]) == "--video")
{
inputName = argv[++i];
isInputVideo = true;
}
else if (string(argv[i]) == "--camera")
{
inputName = argv[++i];
isInputCamera = true;
}
else if (string(argv[i]) == "--help")
{
help();
return -1;
}
else if (!isInputImage)
{
inputName = argv[i];
isInputImage = true;
}
else
{
cout << "Unknown key: " << argv[i] << endl;
return -1;
}

Ptr<cuda::CascadeClassifier> cascade_gpu =
cuda::CascadeClassifier::create("/home/nvidia/opencv-
3.4.0/data/haarcascades_cuda/haarcascade_fullbody.xml");

cv::CascadeClassifier cascade_cpu;

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Image Processing using GPU
____________________________________________________________________________________

if (!cascade_cpu.load("/home/nvidia/opencv-
3.4.0/data/haarcascades_cuda/haarcascade_fullbody.xml"))
{
return cerr << "ERROR: Could not load cascade classifier
\""<< "\"" << endl, help(), -1;
}

cv::VideoCapture capture;
cv::Mat image;

if (isInputImage)
{
image = imread(inputName);
CV_Assert(!image.empty());
}
else if (isInputVideo)
{
capture.open(inputName);
CV_Assert(capture.isOpened());
}
else
{
capture.open(atoi(inputName.c_str()));
CV_Assert(capture.isOpened());
}

namedWindow("result", 1);

cv::Mat frame, frame_cpu, gray_cpu, resized_cpu, frameDisp;


cv::vector<Rect> faces;

cv::GpuMat frame_gpu, gray_gpu, resized_gpu, facesBuf_gpu;

/* parameters */
bool useGPU = true;
double scaleFactor = 1.0;
bool findLargestObject = false;
bool filterRects = true;
bool helpScreen = false;

for (;;)
{
if (isInputCamera || isInputVideo)
{
capture >> frame;
if (frame.empty())
{
break;
}
}

(image.empty() ? frame : image).copyTo(frame_cpu);


frame_gpu.upload(image.empty() ? frame : image);

convertAndResize(frame_gpu, gray_gpu, resized_gpu,


scaleFactor);
convertAndResize(frame_cpu, gray_cpu, resized_cpu,
scaleFactor);

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Image Processing using GPU
____________________________________________________________________________________

TickMeter tm;
tm.start();

if (useGPU)
{
cascade_gpu->setFindLargestObject(findLargestObject);
cascade_gpu->setScaleFactor(1.2);
cascade_gpu->setMinNeighbors((filterRects ||
findLargestObject) ? 4 : 0);

cascade_gpu->detectMultiScale(resized_gpu,
facesBuf_gpu);
cascade_gpu->convert(facesBuf_gpu, faces);
}
else
{
Size minSize = cascade_gpu->getClassifierSize();
cascade_cpu.detectMultiScale(resized_cpu, faces, 1.2,
(filterRects ||
findLargestObject) ? 4 : 0,
(findLargestObject ?
CASCADE_FIND_BIGGEST_OBJECT : 0)
| CASCADE_SCALE_IMAGE,
minSize);
}

for (size_t i = 0; i < faces.size(); ++i)


{
rectangle(resized_cpu, faces[i], Scalar(255));
}

tm.stop();
double detectionTime = tm.getTimeMilli();
double fps = 1000 / detectionTime;

//print detections to console


cout << setfill(' ') << setprecision(2);
cout << setw(6) << fixed << fps << " FPS, " << faces.size()
<< " det";
if ((filterRects || findLargestObject) && !faces.empty())
{
for (size_t i = 0; i < faces.size(); ++i)
{
cout << ", [" << setw(4) << faces[i].x
<< ", " << setw(4) << faces[i].y
<< ", " << setw(4) << faces[i].width
<< ", " << setw(4) << faces[i].height << "]";
}
}
cout << endl;

cv::cvtColor(resized_cpu, frameDisp, COLOR_GRAY2BGR);


displayState(frameDisp, helpScreen, useGPU,
findLargestObject, filterRects, fps);
cv::imshow("result", frameDisp);

char key = (char)waitKey(5);


if (key == 27)
{

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Image Processing using GPU
____________________________________________________________________________________

break;
}

switch (key)
{
case ' ':
useGPU = !useGPU;
break;
case 'm':
case 'M':
findLargestObject = !findLargestObject;
break;
case 'f':
case 'F':
filterRects = !filterRects;
break;
case '1':
scaleFactor *= 1.05;
break;
case 'q':
case 'Q':
scaleFactor /= 1.05;
break;
case 'h':
case 'H':
helpScreen = !helpScreen;
break;
}
}

return 0;
}}

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Image Processing using GPU
____________________________________________________________________________________

Steps to create project and program compilation:


Steps:
 Create a new folder on the remote PC.
 Open Nsight on the host PC.
 Click on File > New> Cuda C/C++ Project.
 Select the Empty File option under Executable Project type and provide a name for the
project. Now, click on Next.
 In the Basic Settings window, tick mark 6.2 for both Generate PTX code and Generate
GPU code and click on Next.
 Here, we are accessing the Jetson board, remotely. So, In the Target Systems window,
Click on Manage to add remote devices using their IP addresses. (To know the IP
address of the remote device, type ifconfig on the terminal of remote device). Close
the Local Systems tab.
 Give the project path (which is the empty folder we created on the remote PC) and keep
the CPU architecture as Native. Click on Finish.
 Right Click on the created project and go to New > Source File. Write the code in the
source file.
 Now, right click on the project and go to Properties. Select Paths and symbols option
under C/C++ General drop down menu. Now, go to Libraries tab and add all the
libraries manually. Similarly, add the paths to those libraries under the Library
Paths tab.
 Select the Target systems option under the Build drop down menu and make sure that
the CPU Architecture is kept Native and the remote connection is properly
established. This configuration will remain the same for all the projects.
 Now, again go to synchronize > Sync active now. (The project folder in your remote
PC must contain the Source File at this stage. If you don’t find it, synchronize the
project again.)
 Now that the project is configured and the code is written, we can build the project.
Click on the build option in the toolbar or right click on the project and select Build
Project. (Check for the Debug folder in the project folder in your remote PC. If it is
not present, Synchronize your project again.)
 If there are no errors, right click on the project name and go to Run as > Run
Configurations. In the Remote tab, select the remote connection and enter the path
of remote executable and remote toolkit and choose Run Remote Executable.

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Image Processing using GPU
____________________________________________________________________________________

Result:
Above given code extracts the desired portion of the image.

Input: Output:

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Image Processing using GPU
____________________________________________________________________________________

____________________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com

Vous aimerez peut-être aussi