title: How to get pixel value from the contoured image with mask?
body: <p>I tried to extract average pixel values (R, G, B) from the contoured image. However, my problem is when I applied the code below, something strange values were observed.</p>
  
  <pre><code>int main(){
  cv::Mat star = imread("C:\\Users\\PC\\Desktop\\star\\starcircle.png");
  cv::Mat mask = cv::Mat::zeros(star.rows, star.cols, CV_8UC1);
  cv::Mat frame;
  double b, g, r = 0.0;
  
  cv::imshow("Original", star);
  
  cv::cvtColor(star, frame, CV_BGR2HSV);
  
  cv::inRange(frame, cv::Scalar(29, 220, 220), cv::Scalar(30, 255, 255), mask);
  
  cv::imshow("mask", mask);
  
  cv::Mat result = cv::Mat(star.rows, star.cols, CV_8UC1, star.type());
  result.setTo(cv::Scalar(0, 0, 0));
  
  star.copyTo(result, mask);  
  
  cv::Scalar temp = mean(mask);
  
  cout &lt;&lt; "avg_R: " &lt;&lt; temp[2] &lt;&lt; " \n"; // red value
  cout &lt;&lt; "avg_G: " &lt;&lt; temp[1] &lt;&lt; " \n"; // green value
  cout &lt;&lt; "avg_B: " &lt;&lt; temp[0] &lt;&lt; " \n\n"; // blue value 
  
  cv::imshow("result", result);
  cv::waitKey(-1);
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>And I got the correct images for the result like below.</p>
  
  <p><a href="https://i.stack.imgur.com/eMFnJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eMFnJ.png" alt="enter image description here"></a></p>
  
  <p>I want to read pixel values only for yellow part, not for outside of mask.</p>
  
  <p>And I have another code for read out pixel values in the yellow parts, but it showed the same result.</p>
  
  <pre><code>int main(){
  cv::Mat star = imread("C:\\Users\\PC\\Desktop\\star\\starcircle.png");
  cv::Mat mask = cv::Mat::zeros(star.rows, star.cols, CV_8UC1);
  cv::Mat frame;
  double b, g, r = 0.0;
  
  cv::imshow("Original", star);
  
  cv::cvtColor(star, frame, CV_BGR2HSV);
  
  cv::inRange(frame, cv::Scalar(29, 220, 220), cv::Scalar(30, 255, 255), mask);
  
  cv::imshow("mask", mask);
  
  cv::Mat result = cv::Mat(star.rows, star.cols, CV_8UC1, star.type());
  result.setTo(cv::Scalar(0, 0, 0));
  
  star.copyTo(result, mask);  
  
  int hei = star.rows;
  int wid = star.cols;
  
  int corow = hei * wid;
  
  double b, g, r = 0.0;
  
  for (int x = 0; x &lt; hei; x++) {
      for (int y = 0; y &lt; wid; y++) {
          if (mask.at&lt;unsigned char&gt;(x, y) &gt; 0) {
              b += result.at&lt;Vec3b&gt;(x, y)[0];
              g += result.at&lt;Vec3b&gt;(x, y)[1];
              r += result.at&lt;Vec3b&gt;(x, y)[2];
  
          }
          else {
  
          }
  
  
      }
  }
  
  cout &lt;&lt; "$$ Red(R), Green(G), Blue(B) $$" &lt;&lt; " \n\n";
  cout &lt;&lt; "avg_R: " &lt;&lt; r / corow &lt;&lt; " \n"; // red value
  cout &lt;&lt; "avg_G: " &lt;&lt; g / corow &lt;&lt; " \n"; // green value
  cout &lt;&lt; "avg_B: " &lt;&lt; b / corow &lt;&lt; " \n\n"; // blue value
  </code></pre>
  
  <p>}    </p>
  
  <p>Please help me to revise the error.</p>
  
  <p>Thank you in advance.</p>