{"id":7879,"date":"2015-11-04T21:18:34","date_gmt":"2015-11-04T21:18:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/04\/convert-rgb-to-black-white-in-opencv-open-source-projects-itseez-opencv\/"},"modified":"2015-11-04T21:18:34","modified_gmt":"2015-11-04T21:18:34","slug":"convert-rgb-to-black-white-in-opencv-open-source-projects-itseez-opencv","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/04\/convert-rgb-to-black-white-in-opencv-open-source-projects-itseez-opencv\/","title":{"rendered":"Convert RGB to Black &#038; White in OpenCV-open source projects Itseez\/opencv"},"content":{"rendered":"<p>AFAIK, you have to convert it to grayscale and then threshold it to binary.<\/p>\n<p><strong>1. Read the image as a grayscale image<\/strong> If you&#8217;re reading the RGB image from disk, then you can directly read it as a grayscale image, like this:<\/p>\n<pre><code>\/\/ C\nIplImage* im_gray = cvLoadImage(\"image.jpg\",CV_LOAD_IMAGE_GRAYSCALE);\n\n\/\/ C++ (OpenCV 2.0)\nMat im_gray = imread(\"image.jpg\",CV_LOAD_IMAGE_GRAYSCALE);\n<\/code><\/pre>\n<p><strong>2. Convert an RGB image <code>im_rgb<\/code> into a grayscale image<\/strong>: Otherwise, you&#8217;ll have to convert the previously obtained RGB image into a grayscale image<\/p>\n<pre><code>\/\/ C\nIplImage *im_rgb  = cvLoadImage(\"image.jpg\");\nIplImage *im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);\ncvCvtColor(im_rgb,im_gray,CV_RGB2GRAY);\n\n\/\/ C++\nMat im_rgb  = imread(\"image.jpg\");\nMat im_gray;\ncvtColor(im_rgb,im_gray,CV_RGB2GRAY);\n<\/code><\/pre>\n<p><strong>3. Convert to binary<\/strong> You can use adaptive thresholding or fixed-level thresholding to convert your grayscale image to a binary image.<\/p>\n<p>E.g. in C you can do the following (you can also do the same in C++ with Mat and the corresponding functions):<\/p>\n<pre><code>\/\/ C\nIplImage* im_bw = cvCreateImage(cvGetSize(im_gray),IPL_DEPTH_8U,1);\ncvThreshold(im_gray, im_bw, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);\n\n\/\/ C++\nMat img_bw = im_gray &gt; 128;\n<\/code><\/pre>\n<p>In the above example, 128 is the threshold.<\/p>\n<p><strong>4. Save to disk<\/strong><\/p>\n<pre><code>\/\/ C\ncvSaveImage(\"image_bw.jpg\",img_bw);\n\n\/\/ C++\nimwrite(\"image_bw.jpg\", img_bw);\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>AFAIK, you have to convert it to grayscale and then threshold it to binary. 1. Read the image as a grayscale image If you&#8217;re reading the RGB image from disk, then you can directly read it as a grayscale image, like this: \/\/ C IplImage* im_gray = cvLoadImage(&#8220;image.jpg&#8221;,CV_LOAD_IMAGE_GRAYSCALE); \/\/ C++ (OpenCV 2.0) Mat im_gray = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7879","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7879"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7879\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}