{"id":1322,"date":"2022-05-23T21:58:19","date_gmt":"2022-05-23T14:58:19","guid":{"rendered":"https:\/\/bigdolphin.com.vn\/?p=1322"},"modified":"2024-03-26T15:24:50","modified_gmt":"2024-03-26T08:24:50","slug":"programming-opencv-with-cuda","status":"publish","type":"post","link":"https:\/\/bigdolphin.com.vn\/?p=1322","title":{"rendered":"Programming OpenCV with CUDA"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. Preparation<\/h3>\n\n\n\n<p>In this tutorial, I use OpenCV version 3.4.17 with Cuda v8.0 on Ubuntu 16.04, my GPU card is GT630 which is Fermi architecture. If you have newer card, you should re-compile your openCV to match with compute capability of your card and your Cuda version. Just change option<em><font color=\"red\"> -D CUDA_ARCH_BIN<\/font><\/em> in your cmake command. <\/p>\n\n\n\n<p>I will use <a href=\"https:\/\/en.wikipedia.org\/wiki\/Lenna\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Lenna image\">Lenna image<\/a> as test image for codes in this post.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/lenna.png\" alt=\"\" class=\"wp-image-1326\" width=\"384\" height=\"384\" srcset=\"https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/lenna.png 512w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/lenna-300x300.png 300w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/lenna-150x150.png 150w\" sizes=\"(max-width: 384px) 100vw, 384px\" \/><figcaption><em>Image of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Lena_Fors%C3%A9n\" target=\"_blank\" rel=\"noreferrer noopener\">Lena Fors\u00e9n<\/a><\/em><\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. Adaptive Histogram Equalization (AHE)<\/h3>\n\n\n\n<p>Ordinary AHE tends to overamplify the contrast in near-constant regions of the image, since the histogram in such regions is highly concentrated. As a result, AHE may cause noise to be amplified in near-constant regions. Contrast Limited AHE (CLAHE) is a variant of adaptive histogram equalization in which the contrast amplification is limited, so as to reduce this problem of noise amplification (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Adaptive_histogram_equalization#Contrast_Limited_AHE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Wikipedia\">Wikipedia<\/a>). <\/p>\n\n\n\n<p><strong>Create a new main<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">nano main.cpp<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp line-numbers\">#include&lt;opencv2\/opencv.hpp&gt;\nusing namespace cv;\nusing namespace cv::cuda;\nint main(){    \n    \/\/ Load image\n    Mat image = imread(\"lenna.png\", IMREAD_GRAYSCALE);\n    imwrite(\"gray.png\",image);\n    \/\/ Cuda mat\n    GpuMat dst, src;\n    \/\/ Contrast Limited Adaptive Histogram Equalization pointer\n    Ptr ptr_clahe = cuda::createCLAHE(5.0, Size(8, 8));\n    \/\/ Transfer image to CUDA\n    src.upload(image);\n    \/\/ Apply CLAHE\n    ptr_clahe-&gt;apply(src, dst);\n    \/\/ Get result back\n    Mat result;\n    dst.download(result);\n    \/\/ Write to file\n    imwrite(\"rs.png\",result);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>Compile and test<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">ltkhanh@ServerTX:~\/openCV$ g++ main.cpp $(pkg-config --libs --cflags opencv)\nltkhanh@ServerTX:~\/openCV$ .\/a.out \nltkhanh@ServerTX:~\/openCV$ <\/code><\/pre>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-layout-1 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"512\" height=\"512\" src=\"https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/gray.png\" alt=\"\" class=\"wp-image-1333\" srcset=\"https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/gray.png 512w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/gray-300x300.png 300w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/gray-150x150.png 150w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><figcaption><font size=\"4\" color=\"red\"><b>Grayscale image of original test image<\/b><\/font><\/figcaption><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"512\" height=\"512\" src=\"https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/rs.png\" alt=\"\" class=\"wp-image-1334\" srcset=\"https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/rs.png 512w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/rs-300x300.png 300w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2022\/05\/rs-150x150.png 150w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><figcaption><font size=\"4\" color=\"red\"><b>Result image using CLAHE with CUDA<\/b><\/font><\/figcaption><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use some Cuda functions for OpenCV application.<\/p>\n","protected":false},"author":2,"featured_media":1323,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[10],"tags":[63,59,61,22],"_links":{"self":[{"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/posts\/1322"}],"collection":[{"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1322"}],"version-history":[{"count":16,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/posts\/1322\/revisions"}],"predecessor-version":[{"id":1342,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/posts\/1322\/revisions\/1342"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/media\/1323"}],"wp:attachment":[{"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}