0

I am writing an IOS app that determines color intensity in a rectangle. My measurement is to calculate intensity value of red rectangle region when it falls in the white paper laid on the top of black background. The display is done in the iphone 6 held vertically and intensity measurement is seen in debug window of xcode. When I put a white paper in a black background, the value of intensity is around 30, which says the location of intensity calculation may be indicating black region. When I move the phone closer to the white paper, so the white paper is near the bottom of the phone and the picture is enlarged, it get a value near 200, which makes sense. Can you please tell me what I am doing wrong. The following code is written in viewcontroller, xcode 8.2.1

- (void)processImage:(cv::Mat&)image
    cv::Mat curFrame = image
    cv::rectangle(curFrame, cvPoint(180+15, 300-15),cv::Scalar(0,0,255, 255),5);
    uchar intensity = curFrame.at<uchar>(cvPoint(180, 300));
    int intensityvalue = 255;
    intensityvalue = (int) intensity;
    NSLog (@"intensity: %d ", intensityvalue);
jay
  • 9
  • 3
  • sorry the phone is held horizontally not vertically – jay Feb 25 '17 at 12:49
  • also in this example, 180 is vertical height y, 300 is horizontal height x. I know opencv inverts intensity (y,x). – jay Feb 25 '17 at 12:51
  • why do you use `uchar` in `curFrame.at(cvPoint(180, 300))`? Since your curFrame's type is BGRA, you should replace `uchar` with the corresponding type. See [this](http://stackoverflow.com/questions/7899108/opencv-get-pixel-channel-value-from-mat-image) for details. Or you can split the channels of `curFrame` and use `uchar` on the blue channel. – Quang Hoang Feb 25 '17 at 14:45
  • sorry being dumb, can you please tell me why IU should replace the following code with? – jay Feb 26 '17 at 01:12
  • uchar intensity = curFrame.at(cvPoint(180, 300)); – jay Feb 26 '17 at 01:13
  • I am able to change the code as below cv::Vec3b intensity = curFrame.at(cvPoint(300, 180)); intensityvalue = (intensity[0]*.114 +intensity[1]*.587+intensity[2]*.299); however i still have the same issue, when the white paper is farther away from the phone, and the red rectangle is on the top of the white paper, it is still detecting a value of around 30. The value changes to near 200 only when teh white paper is close to the phone. D – jay Feb 26 '17 at 01:48
  • Can you give me suggestion to fix this problem - i need it to detect the white p[aper farther away from the phone – jay Feb 26 '17 at 01:49

1 Answers1

0

If you only need intensity, you can try:

- (void)processImage:(cv::Mat&)image{
     cv::Mat curFrame = image // why don't you use image directly?
     cv::Mat gray;

     // convert to gray scale, make sure that your curFrame is RGBA or BGRA
     cv::cvtColor(curFrame,gray,cv::CV_RGBA2GRAY); 
     int intensity = gray.at<uchar>(cvPoint(180, 300));
     NSLog (@"intensity: %d ", intensity);

     // draw the rectangle after you already extract the intensity
     // otherwise it may interfere with your sample
     cv::rectangle(curFrame, cvPoint(180+15, 300-15),cv::Scalar(0,0,255, 255),5);
}
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74