05 cvInRangeS 找出特定顏色

目的:

cvInRangeS 找出特定的顏色

函數介紹:

cvInRangeS 找出特定的顏色,找到的話,就顯示為白色,不然為黑色。

 

參數值:

C: void cvInRangeS(const CvArr* src, CvScalar lower, CvScalar upper, CvArr* dst)
Parameters:
  • src – first input array.
  • lowerb – inclusive lower boundary array or a scalar.
  • upperb – inclusive upper boundary array or a scalar.
  • dst – output array of the same size as src and CV_8U type.

使用範例:

sample12_cvInRangeS

例如找出顏色在

0>=B>=21

200>=G>=255

0>=R>=20

 

 

</pre>
<span class="s1">#include </span><span class="s2"><opencv2/core/core.hpp></span>

<span class="s1">#include </span><span class="s2"><opencv2/highgui/highgui.hpp></span>

<span class="s1">#include </span><span class="s2"><opencv2/opencv.hpp></span>

<span class="s1">#include </span><span class="s2"><iostream></span>

<span class="s1">#include </span><span class="s2"><string.h></span>

<span class="s2">#include </span><span class="s3"><fstream></span>

<span class="s2">using</span> <span class="s2">namespace</span><span class="s4"> cv;</span>

<span class="s5">int</span><span class="s2"> main(</span><span class="s5">int</span><span class="s2"> argc,</span><span class="s5">char</span><span class="s2"> **argv)</span>

<span class="s2">{</span>

<span class="s2"><span class="Apple-converted-space">    </span></span><span class="s5">int</span><span class="s2"> width,height;</span>

<span class="s2"><span class="Apple-converted-space">    </span></span><span class="s5">if</span><span class="s2">(argc<=</span><span class="s6">1</span><span class="s2">)</span>

<span class="s2"><span class="Apple-converted-space">    </span>{</span>

<span class="s2"><span class="Apple-converted-space">        </span>std::cout<<</span><span class="s3">"Error:Please Load a picture!"</span><span class="s2"><<std::endl;</span>

<span class="s2"><span class="Apple-converted-space">        </span></span><span class="s5">return</span> <span class="s6">0</span><span class="s2">;</span>

<span class="s2"><span class="Apple-converted-space">    </span>}</span>

<span class="s2"><span class="Apple-converted-space">    </span></span>

<span class="s2"><span class="Apple-converted-space">    </span>IplImage *image;</span>

<span class="s4"><span class="Apple-converted-space">    </span></span><span class="s2">//</span><span class="s7">建立視窗</span>

<span class="s2"><span class="Apple-converted-space">    </span>namedWindow(</span><span class="s3">"image"</span><span class="s2">,CV_WINDOW_AUTOSIZE);</span>

<span class="s2"><span class="Apple-converted-space">    </span></span>

<span class="s4"><span class="Apple-converted-space">    </span></span><span class="s2">//</span><span class="s7">讀取圖片</span>

<span class="s2"><span class="Apple-converted-space">    </span>image=cvLoadImage(argv[</span><span class="s6">1</span><span class="s2">]);</span>

<span class="s2"><span class="Apple-converted-space">    </span>width=image->width;</span>

<span class="s2"><span class="Apple-converted-space">    </span>height=image->height;</span>

<span class="s2"><span class="Apple-converted-space">    </span></span>

<span class="s8"><span class="Apple-converted-space">    </span></span><span class="s9">// </span><span class="s2">畫出正方形的外框</span>

<span class="s2"><span class="Apple-converted-space">    </span></span>

<span class="s2"><span class="Apple-converted-space">    </span>IplImage * img2=cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, </span><span class="s6">1</span><span class="s2">);</span>

<span class="s2"><span class="Apple-converted-space">    </span>cvInRangeS(image, cvScalar(</span><span class="s6">0</span><span class="s2">,</span><span class="s6">200</span><span class="s2">,</span><span class="s6">0</span><span class="s2">), cvScalar(</span><span class="s6">21</span><span class="s2">,</span><span class="s6">255</span><span class="s2">,</span><span class="s6">20</span><span class="s2">), img2);</span>

<span class="s4"><span class="Apple-converted-space">   </span></span><span class="s2">// cvInRangeS(image, CV_RGB(255,200,255), CV_RGB(255,250,255), img2);</span>

<span class="s2"><span class="Apple-converted-space">    </span></span>

<span class="s2"><span class="Apple-converted-space">    </span>cvShowImage(</span><span class="s3">"image"</span><span class="s2">, image);</span>

<span class="s2"><span class="Apple-converted-space">    </span></span>

<span class="s2"><span class="Apple-converted-space">    </span>waitKey(</span><span class="s6">0</span><span class="s2">);</span>

<span class="s2"><span class="Apple-converted-space">    </span>cvShowImage(</span><span class="s3">"image2"</span><span class="s2">, img2);</span>

<span class="s2"><span class="Apple-converted-space">    </span></span>

<span class="s2"><span class="Apple-converted-space">    </span>waitKey(</span><span class="s6">0</span><span class="s2">);</span>

<span class="s2"><span class="Apple-converted-space">    </span></span>

<span class="s2"><span class="Apple-converted-space">    </span>cvDestroyAllWindows();</span>

<span class="s4"><span class="Apple-converted-space">    </span></span><span class="s2">//cvReleaseImage(&image);</span>

<span class="s4"><span class="Apple-converted-space">    </span></span><span class="s2">//system("pause");</span>

<span class="s2"><span class="Apple-converted-space">    </span></span><span class="s5">return</span> <span class="s6">0</span><span class="s2">;</span>

<span class="s2">}</span>
<pre>

結果:

Screen Shot 2015-03-01 at 12.37.17 PM

補充資料:

 

OpenCV
CH01 簡介CH02 OpenCV APICH03 繪圖CH04 應用範例CH06 haarcascadesCH06 surf和siftCH07 OCRCH09 FaceCH5 輪廓(findContours、drawContours)