{"id":153,"date":"2023-07-18T12:57:36","date_gmt":"2023-07-18T04:57:36","guid":{"rendered":"http:\/\/192.168.1.15:89\/?p=153"},"modified":"2023-07-18T13:01:09","modified_gmt":"2023-07-18T05:01:09","slug":"python-ctypes-opencv","status":"publish","type":"post","link":"https:\/\/www.skyblogs.xyz\/index.php\/2023-07-18\/python-ctypes-opencv\/","title":{"rendered":"\u57fa\u4e8eCtypes\u4f9bPython\u8c03\u7528\u7684OpenCV\u52a8\u6001so\u5e93\u7f16\u8bd1"},"content":{"rendered":"<p>\u6628\u65e5\u63a5\u5230\u4e00\u4e2a\u9700\u6c42\uff0c\u8bf4\u662f\u7528python\u8c03\u7528cpp\u7684opencv\u5e93\u53bb\u505a\u56fe\u50cf\u8bc6\u522b\uff0c\u76ee\u524d\u5df2\u7ecf\u5728windows\u4e0a\u8dd1\u901a\u4e86\uff0c\u5e76\u4e14\u6210\u529f\u5bfc\u51fa\u4e86dll<\/p>\n<p>\u4f46\u662f\u63a5\u4e0b\u6765\u8fd8\u9700\u8981\u5728linux\u4e0a\u8dd1\u901a\uff0c\u540c\u65f6\u7f16\u8bd1\u51fa\u53ef\u4f9b\u76ee\u6807\u5e73\u53f0\u4f7f\u7528\u7684so\u52a8\u6001\u5e93<\/p>\n<p><strong>\u76ee\u6807\u5e73\u53f0\uff1aCentOS8 x86<\/strong><\/p>\n<p><strong>\u4f9d\u8d56\u5e93\u7248\u672c\uff1aPython3.8 OpenCV3.4.8<\/strong><\/p>\n<h3>OpenCV\u73af\u5883\u5b89\u88c5<\/h3>\n<p><pre class=\"prettyprint linenums\">wget https:\/\/github.com\/opencv\/opencv\/archive\/refs\/tags\/3.4.8.tar.gz<\/p>\n<p>tar -xvf 3.4.8.tar.gz<\/p>\n<p>cd opencv-3.4.8<\/p>\n<p>mkdir build<\/p>\n<p>cd build<\/p>\n<p>cmake ..<\/p>\n<p>make<\/p>\n<p>make install<\/pre>\u5168\u7a0b\u65e0\u62a5\u9519\uff0c\u518d\u8fdb\u884c\u4f9d\u8d56\u94fe\u63a5\u5904\u7406\uff0cOpenCV\u73af\u5883\u642d\u5efa\u5b8c\u6bd5<\/p>\n<h3>\u9759\u6001\u5e93\u7f16\u8bd1<\/h3>\n<h4>\u4ee3\u7801\u8bd5\u8fd0\u884c\u4e0e\u7f16\u8bd1<\/h4>\n<p>\u9996\u5148\u7f16\u5199camke\u6587\u4ef6<br \/>\n<pre class=\"prettyprint linenums\">vim CMakeLists.txt<\/p>\n<p>cmake_minimum_required(VERSION 2.8)<\/p>\n<p>project(deshadow)<\/p>\n<p>set( CMAKE_BUILD_TYPE &#8220;Debug&#8221; )<\/p>\n<p># Find OpenCV library<\/p>\n<p>find_package(OpenCV REQUIRED)<\/p>\n<p># Add shared library target<\/p>\n<p>add_library(deshadow SHARED imgae_deshadow.cpp)<\/p>\n<p>include_directories(${OpenCV_INCLUDE_DIRS})<\/p>\n<p># Link with OpenCV library<\/p>\n<p>target_link_libraries(deshadow ${OpenCV_LIBS})<\/p>\n<p>#message(${OpenCV_LIBS})<\/pre>\u540e:wq\u4fdd\u5b58\u9000\u51fa\uff0c\u5148\u901a\u8fc7mkdir build\u521b\u5efabuild\u6587\u4ef6\u5939\uff0c\u518dcd build\u7136\u540e\u901a\u8fc7cmake ..\u53bb\u521b\u5efacmake\u6587\u4ef6<br \/>\n\u4e4b\u540e\u8f93\u5165make\uff0c\u7ed3\u679c\u62a5\u9519\u4e3a<br \/>\n<pre class=\"prettyprint linenums\">make<\/p>\n<p>Scanning dependencies of target deshadow<\/p>\n<p>[100%] Building CXX object CMakeFiles\/deshadow.dir\/imgae_deshadow.cpp.o<\/p>\n<p>\/lingkong\/python\/imgae_deshadow.cpp:21:11: error: expected constructor, destructor, or type conversion before \u2018(\u2019 token<\/p>\n<p>__declspec(dllexport)<\/p>\n<p>^<\/p>\n<p>make[2]: *** [CMakeFiles\/deshadow.dir\/imgae_deshadow.cpp.o] Error 1<\/p>\n<p>make[1]: *** [CMakeFiles\/deshadow.dir\/all] Error 2<\/p>\n<p>make: *** [all] Error 2<\/pre>\u539f\u56e0\u4e3adllexport\u662fwindows\u5e73\u53f0\u4e0b\u4e13\u7528\u7684\u8bed\u6cd5\uff0c\u800c\u5728linux\u4e0b\u7f16\u8bd1\u65f6\u4f1a\u81ea\u52a8\u5bfc\u51faso\uff0c\u56e0\u6b64\u8fd9\u91cc\u53ea\u9700\u8981\u5220\u9664\u8be5\u90e8\u5206\u5373\u53ef<br \/>\n\u518d\u6b21make\uff0c\u7ed3\u679c\u8f93\u51fa\u4e3a<br \/>\n<pre class=\"prettyprint linenums\">make<\/p>\n<p>Scanning dependencies of target deshadow<\/p>\n<p>[100%] Building CXX object CMakeFiles\/deshadow.dir\/imgae_deshadow.cpp.o<\/p>\n<p>\/lingkong\/python\/imgae_deshadow.cpp: In function \u2018int Image_deshadow(std::string&amp;, int, char*)\u2019:<\/p>\n<p>\/lingkong\/python\/imgae_deshadow.cpp:174:42: error: \u2018cv::MorphShapes\u2019 is not a class or namespace<\/p>\n<p>Mat element = getStructuringElement(cv::MorphShapes::MORPH_RECT, cv::Size(n, n));<\/p>\n<p>^<\/p>\n<p>make[2]: *** [CMakeFiles\/deshadow.dir\/imgae_deshadow.cpp.o] Error 1<\/p>\n<p>make[1]: *** [CMakeFiles\/deshadow.dir\/all] Error 2<\/p>\n<p>make: *** [all] Error 2<\/pre>\u8fd9\u91cc\u662f\u7531\u4e8e\u4ee3\u7801\u7684\u5bf9\u76f8\u5173\u7684cv\u7c7b\u5b9a\u4e49\u4e0d\u6e05\u695a\uff0c\u5bfc\u81f4\u5373\u4f7f\u5728windows\u4e0b\u7684msvc\u4e2d\u53ef\u7f16\u8bd1\u6210\u529f\uff0c\u5728gcc\u4e0b\u4ecd\u65e7\u7f16\u8bd1\u5931\u8d25<br \/>\n\u53ea\u9700\u8981\u5c06MORPH_RECT\u524d\u9762\u7684\u201ccv::MorphShapes::\u201d\u5220\u9664\u5373\u53ef\uff0c\u518d\u5bf9\u4ee3\u7801\u4e2d\u7684\u5176\u4ed6\u7c7b\u4f3c\u53c2\u6570\u4e5f\u505a\u540c\u6837\u5904\u7406\u3002<br \/>\n\u540e\u8fdb\u884c\u7b2c\u4e09\u6b21make<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-154\"  src=\"https:\/\/www.skyblogs.xyz\/wp-content\/themes\/lolimeow-lolimeowV13.13\/assets\/images\/loading.gif\" data-src=\"http:\/\/192.168.1.15:89\/wp-content\/uploads\/2023\/07\/\u5c4f\u5e55\u622a\u56fe-2023-07-18-122345-300x46.png\" class=\"lazy\" loading=\"lazy\" alt=\"cmake\u8f93\u51fa\u65e5\u5fd7\" srcset=\"https:\/\/www.skyblogs.xyz\/wp-content\/uploads\/2023\/07\/\u5c4f\u5e55\u622a\u56fe-2023-07-18-122345-300x46.png 300w, https:\/\/www.skyblogs.xyz\/wp-content\/uploads\/2023\/07\/\u5c4f\u5e55\u622a\u56fe-2023-07-18-122345.png 656w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><br \/>\nso\u5e93\u6b63\u5e38\u8f93\u51fa\uff0c\u7f16\u8bd1\u6210\u529f<\/p>\n<h3>Python\u5f02\u5e38\u6355\u6349<\/h3>\n<p>\u63a5\u7740\u8fdb\u5165python\u4ee3\u7801\u4fee\u6539\u4f9d\u8d56\u5e93\u4f4d\u7f6e\uff0c\u540c\u65f6pip\u4e0b\u8f7d\u5bf9\u5e94\u4f9d\u8d56\u5e93<br \/>\n\u8bd5\u8fd0\u884c\u62a5\u9519 \u663e\u793aSegmentation fault\uff0c\u5373\u5728linux\u4e0bpython\u901a\u8fc7ctype\u8c03\u7528c\u7684\u5e38\u89c1\u6bb5\u9519\u8bef\uff0c<br \/>\n\u8fd9\u91cc\u5229\u7528faulthandle\u5e93\u8fdb\u884c\u9519\u8bef\u6355\u6349\uff0c\u5b9a\u4f4d\u5230\u9519\u8bef\u4ee3\u7801\u4e3a<br \/>\n<pre class=\"prettyprint linenums\">pbyteraw = np.zeros(size, dtype=np.uint8)<\/p>\n<p>image = np.ascontiguous(image, dtype=image.dtype)<\/p>\n<p>image_ctypes_ptr = cast(image.ctypes.data, POINTER(c_uint8))<\/p>\n<p>lib.Image_deshadow(image_ctypes_ptr, ctypes.c_int(w), ctypes.c_int(h), ctypes.c_int(channels), ctypes.c_int(n),pbyteraw.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)))<\/p>\n<p>\u518d\u7ed3\u5408cpp\u5904\u7684\u5bf9\u5e94\u51fd\u6570\uff1a<\/p>\n<p>int Image_deshadow(std::string &amp;base64_src, int n, char* pout)<\/pre>\u53ef\u77e5\u662f\u4f20\u53c2\u7c7b\u578b\u7684\u95ee\u9898\uff0c\u5bf9\u4ee3\u7801\u8fdb\u884c\u4fee\u6539\uff1a<br \/>\n<pre class=\"prettyprint linenums\">pbyteraw = np.zeros(size, dtype=np.uint8)<\/p>\n<p># \u5c06\u56fe\u50cf\u6570\u636e\u7684\u6307\u9488\u4f20\u9012\u7ed9C++\u51fd\u6570<\/p>\n<p>lib.Image_deshadow(image.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)), ctypes.c_int(w), ctypes.c_int(h), ctypes.c_int(channels), ctypes.c_int(n), pbyteraw.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)))<\/p>\n<p>cpp\uff1a<\/p>\n<p>int Image_deshadow(unsigned char* pimg, int w, int h, int channels, int n, unsigned char* pout)<\/pre>\u518d\u505a\u4e00\u4e9b\u9002\u914d\u5904\u7406\uff0c\u8f93\u5165Python3 main.py\uff0c\u56fe\u50cf\u6b63\u5e38\u751f\u6210\uff0c\u4efb\u52a1\u5b8c\u6210\u3002<\/p>\n<h4>\u5c0f\u7ed3<\/h4>\n<p>\u867d\u8bf4\u5728\u4fee\u590d\u95ee\u9898\u4e0a\u6240\u82b1\u8d39\u65f6\u95f4\u4e0d\u591a\uff0c\u4f46\u662f\u4f7f\u7528faulthandle\u4ee5\u53cagdb\u7b49\u8c03\u8bd5\u5de5\u5177\u51c6\u786e\u5b9a\u4f4d\u9519\u8bef\u6bb5\u5374\u82b1\u8d39\u4e86\u4e0d\u5c11\u65f6\u95f4<br \/>\n\u8d77\u521d\u672c\u4ee5\u4e3a\u8fd9\u4f1a\u662f\u4e00\u8d77\u6bd4\u8f83\u597d\u505a\u7684\u5355\u5b50\uff0c\u4f46\u968f\u5373\u4fbf\u88ab\u5ba2\u6237\u4ee3\u7801\u7684\u62bd\u8c61\u7a0b\u5ea6\u7ed9\u9707\u60ca\u5230\u4e86\uff0c\u751a\u81f3\u4ee4\u4eba\u6000\u7591\u662f\u5426\u8fd9\u6bb5\u4ee3\u7801\u771f\u7684\u5728windows\u4e0a\u8dd1\u901a\u8fc7<br \/>\n\u4e0d\u8fc7\u4e5f\u7b97\u79ef\u6512\u4e86\u4e00\u70b9python\u4e0ecpp\u7684\u4ea4\u4e92\u7ecf\u9a8c\uff0c\u5e0c\u671b\u4ee5\u540e\u5728\u5904\u7406ctype\u76f8\u5173\u95ee\u9898\u7684\u65f6\u5019\uff0c\u4eca\u65e5\u7684\u7ecf\u9a8c\u80fd\u591f\u591a\u591a\u5c11\u5c11\u53d1\u6325\u4e00\u70b9\u4f5c\u7528<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6628\u65e5\u63a5\u5230\u4e00\u4e2a\u9700\u6c42\uff0c\u8bf4\u662f\u7528python\u8c03\u7528cpp\u7684opencv\u5e93\u53bb\u505a\u56fe\u50cf\u8bc6\u522b\uff0c\u76ee\u524d\u5df2\u7ecf\u5728windows\u4e0a\u8dd1\u901a\u4e86\uff0c\u5e76\u4e14\u6210\u529f\u5bfc\u51fa\u4e86dll \u4f46\u662f\u63a5\u4e0b\u6765\u8fd8\u9700\u8981\u5728linux\u4e0a\u8dd1\u901a\uff0c\u540c\u65f6\u7f16\u8bd1\u51fa\u53ef\u4f9b\u76ee\u6807\u5e73\u53f0\u4f7f\u7528\u7684so\u52a8\u6001\u5e93 \u76ee\u6807\u5e73\u53f0\uff1aCentOS8 x86 \u4f9d\u8d56\u5e93\u7248\u672c\uff1aPython3.8 OpenCV3.4.8 OpenCV\u73af\u5883\u5b89\u88c5 \u5168\u7a0b\u65e0\u62a5\u9519\uff0c\u518d\u8fdb\u884c\u4f9d\u8d56\u94fe\u63a5\u5904\u7406\uff0cOpenCV\u73af\u5883\u642d\u5efa\u5b8c\u6bd5 \u9759\u6001\u5e93\u7f16\u8bd1 \u4ee3\u7801\u8bd5\u8fd0\u884c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":154,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"#%%post_excerpt%%","_seopress_robots_index":"","footnotes":""},"categories":[6],"tags":[33,31,30,34,32],"class_list":["post-153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-teck-discussion","tag-cpp","tag-ctypes","tag-opencv","tag-python","tag-so"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":9,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":163,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions\/163"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/media\/154"}],"wp:attachment":[{"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}